Communicating clients that are connected to the same server

This is a discussion on Communicating clients that are connected to the same server within the Networking/Device Communication forums, part of the General Programming Boards category; First of all, I'm using Linux. Well, the problem is that I have an average basic server (like the one ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Communicating clients that are connected to the same server

    First of all, I'm using Linux. Well, the problem is that I have an average basic server (like the one you can get on the beej tutorial). I see that when the server receives a connection from a client, it uses fork() to generate a new process to handle the client's connection. And that repeats when it receives other client connections. The question is: how can I make those clients, that are in different processes, communicate with each other. Take as an example a chat. Two clients connect to the server, and the server generates a process for each client; but how can the clients exchange information with each other in order to be able to chat?

    Well, that's my question, hope I was clear. If there's something you don't understand about my question, please, don't hesitate to ask. Any help will be very appreciated.

    Regards
    Last edited by seforix; 05-28-2009 at 05:21 PM.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You just need to set up some mechanism where one connected user can send a message and specify a user that it goes to. Your server needs to figure out which process that user is connected to, and then send that process the message, along with specifying the user it came from.

    Set up a pipe from the server to each client, and just maintain a table of which pipe goes to which user (or use named pipes, and a consistent naming scheme based on the user).

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    If you don't mind using a thread for each client model, then that is a possibility. I've always found that the easiest model to use when dealing with multiple clients. In each thread receieve all the clients bytes and parse them into something meaningful, and report back to some main thread which will in turn respond to the proper client(s).

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2
    Thanks to both of you for your answers. Regarding to the use of threads, I'd like to avoid it if possible. I want to do it in a more elegant way.
    Right now I'm reading a lot about process and trying a lot of different stuff without success, trying to use dup2(), execl() and nothing seems to work. But at least I'm understanding a little bit more what I want to do: I want to communicate to CHILD processes. Because each time the server receives a new connection it creates a new child process with fork() and I want to communicate them.

    Nevertheless, I'm completely clueless about how to do it. So any further help will be appreciated. I'd be very glad if you can also post some code.

    Thanks in advance.
    Last edited by seforix; 05-29-2009 at 03:33 PM.

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by seforix View Post
    Thanks to both of you for your answers. Regarding to the use of threads, I'd like to avoid it if possible. I want to do it in a more elegant way.
    Right now I'm reading a lot about process and trying a lot of different stuff without success, trying to use dup2(), execl() and nothing seems to work. But at least I'm understanding a little bit more what I want to do: I want to communicate to CHILD processes. Because each time the server receives a new connection it creates a new child process with fork() and I want to communicate them.

    Nevertheless, I'm completely clueless about how to do it. So any further help will be appreciated. I'd be very glad if you can also post some code.

    Thanks in advance.
    Sure, if you call elegant more difficult, and computationally much slower. Check out beej's IPC guide for more info on IPC.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would just use a "pass it on" protocol. You append a couple of characters to all messages and the server interprets that, eg, "CALL1:" and the server will forward this on to the appropriate party. Since that initial signal is appended (and removed) by your program, the users never see or have to deal with it.

    That is how an actual chat client works. You do not send a message directly to someone else, you send a message to the server with a wrapper added by the client, and the server uses that wrapper to relay your message.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by seforix View Post
    Thanks to both of you for your answers. Regarding to the use of threads, I'd like to avoid it if possible. I want to do it in a more elegant way.
    I really don't see how running each client in a separate process is elegant, when one of the prime requirements is for the clients to communicate with each other.

    If you want elegant, you need to use select() or poll().
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple clients to "internet" server
    By Zarniwoop in forum C Programming
    Replies: 2
    Last Post: 10-11-2008, 11:04 PM
  2. TCP Sockets: multiple clients - one server
    By Printisor in forum C Programming
    Replies: 4
    Last Post: 11-01-2007, 10:34 AM
  3. MISO Soup: Multiple Clients and one Server
    By doubleanti in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-24-2007, 02:29 AM
  4. Simulation of a server and clients
    By wise_ron in forum C Programming
    Replies: 5
    Last Post: 10-10-2006, 01:49 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21