How Can I share a socket between childs?


 
Thread Tools Search this Thread
Top Forums Programming How Can I share a socket between childs?
# 1  
Old 06-25-2011
How Can I share a socket between childs?

Hello guys!

I had seen some posts at this forum talking about my problem, but maybe my scenario is a little different, and I want other solutions.

I saw users of this forums saying that the way to shared sockets is using UNIX Sockets, but this is the only way in my scenario?

My Scenario:

I have a process that is the server. Server accepts clients (sockets) and then call to fork. If two clients connects to my server, parent process will have two childs, and now is my problem, one of this child need to send a message to the another child.

First, I thinked that I could save fd socket in share memory, and then using this fd in another child I'll be able to send messages to another client, but doesn't works...

Any ideas?? Thanks!

PD: Sorry for my bad english
# 2  
Old 06-27-2011
Quote:
Originally Posted by serpens11
Hello guys!

I had seen some posts at this forum talking about my problem, but maybe my scenario is a little different, and I want other solutions.

I saw users of this forums saying that the way to shared sockets is using UNIX Sockets, but this is the only way in my scenario?

My Scenario:

I have a process that is the server. Server accepts clients (sockets) and then call to fork. If two clients connects to my server, parent process will have two childs, and now is my problem, one of this child need to send a message to the another child.

First, I thinked that I could save fd socket in share memory, and then using this fd in another child I'll be able to send messages to another client, but doesn't works...

Any ideas?? Thanks!

PD: Sorry for my bad english
Hi,

you can use shared_memory IPC to share the sockets(any data) between the two processes via a unique key. Otherwise you can instead create threads when you serve a new request. Then you save the overhead of forking a new process.

Thanks,
Gaurav.
This User Gave Thanks to gaurav1086 For This Post:
# 3  
Old 06-29-2011
Hi!!

Yes, I tried this, but not worked for me. How Can I shared a socket?
I tried to share value of " fd_client = accept(sock,0,0); ". I saved value of fd_client in a shared memory, but then when I read this value, and write into it, really I'm writting in the childs socket, and not in the value that I saved (another child).

Can you give me an example to share a socket with IPC? Thanks!
# 4  
Old 06-29-2011
Hello,

Can you provide the code that you tried but it failed. ?

Thanks,
Gaurav.
# 5  
Old 06-29-2011
Hi!!
No, because I did a lot of changes these days... for example, now I'm trying to do with pipes, but I can describe.

I create a struct, and then I shared with shared memory, struct was this:

Code:
typedef struct _clients{
        char login[7];
        int fd;
        int stat;
}clients[3];

And struct and shared memory works good (tested).

My server is listening connections, when connection arrives then forks and the client go to child. Before fork, I saved value of "fd_client = accept(sock,0,0);" into struct client[0]->fd=fd_client;

Then, when another children needs to write in the socket of another child, I search in the struct the fd of the another child, because I know he's login. Then I call write(fd,"test",4);

Problem? word "test" is send to the actual child, and I can see in his screen the word "test", when I need to write this "test" in the screen of the other child.

Now, seeing this, maybe I need to save value of "sock" instead "fd_client"??

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Pipe between Childs

Hey guys, I have to make a C program that simulates this command : cat (files here) | sort > file.txt So, I start and create a pipe. Then create the first child. This first child will execute the Cat through the pipe. Then create a second child that will execute sort, with input from... (4 Replies)
Discussion started by: Poppo
4 Replies

2. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

3. Programming

waiting for multiple childs - C - waitpid

Hi gurus, I would like to fork more children and then write their return values: so far I tried: #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main(void) { pid_t pid; int rv=0, i; ... (5 Replies)
Discussion started by: wakatana
5 Replies

4. Programming

Share socket fd between two processes

Hello every one i am implementing client server program.To handle multiple fds of client in server i used poll function.But this poll not able to access socket fd from other process.How can i share socket fd between two process. Please Help me,Thanks in advance. (3 Replies)
Discussion started by: andrew.paul
3 Replies

5. Linux

To share socket fd between two process

Hello every one i am implementing client server program.To handle multiple fds of client in server i used poll function.But this poll not able to access socket fd from other process.How can i share socket fd between two process. Please Help me,Thanks in advance. (1 Reply)
Discussion started by: andrew.paul
1 Replies

6. Programming

Need help with fork, forking multiple childs and shared memory

Hi all, I m writing an application, where i need to fork multiple childs and those child should handle particular task given to them. More descriptive. For example, suppose i have 4 Network, each network has multiple nodes. Now on the basis of network child should be forked and these child... (8 Replies)
Discussion started by: helpmeforlinux
8 Replies

7. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

8. Programming

forking. sharing global data in childs

hi, i want to write a code for forking 3 4 child. n wants that every child process one of the account from global account list. i wrote a program for that, but problem is every child is processing every account in list. what can me done to avoid it. attaching code with it #include <stdio.h>... (2 Replies)
Discussion started by: anup13
2 Replies

9. Programming

share file descriptor between childs

#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/wait.h> #include <fcntl.h> #include <signal.h> #include <unistd.h> #include <string.h> #define BUFF_SIZE 256 #define CHILDS 4 #define DATAFILE "Client_Files.txt" void worker(int n);... (3 Replies)
Discussion started by: dlcpereira
3 Replies

10. Shell Programming and Scripting

How to kill a process and its childs given pid and userid

I need to write a shell script which would take 2 arguments pid , userid. Then it should kill all the child process under it. If a child process is not killed then it should wait for 1 minute and should kill. can anybody give me the idea to write it? (0 Replies)
Discussion started by: nani_g
0 Replies
Login or Register to Ask a Question