![]() |
|
#1
|
|||
|
|||
TCP Sockets: multiple clients - one serverHi all,
I am trying to create an application that uses TCP sockets under Unix, where multiple clients connect to one server. Each message sent by one client is forwarded to all other clients. I am using on the server side an array of int's declared at the beginning of the application, that will contain the socket descriptor returned by the "accept" function for each client that connects. After this, I am fork-ing a new child that will handle that client. All child processes need to access the array of int's in order to send to everyone the client messages. My problem is that for the first client (child process), the array of int's has only one element, namely its own socket (file) descriptor, for the second client, the array of int's has two elements, like it is supposed to have, but the first child process created for the first client still has only one element, instead of two. I have no idea why and how to change the code so that the first client will also have 2 elements when the second client connects to the server. I have tried declaring the array as static, using pointers, etc, but nothing works. Server code snippet: CPP / C++ / C Code:
Any help is appreciated, thank you! |
|
#2
|
|||
|
|||
Re: TCP Sockets: multiple clients - one serverQuote:
I suggest that you get Beej's Guide to Network Programming In section 6.2 there is an example of a server that can handle multiple clients in such a way that the clients talk to each other. Make sure you understand the previous material, and the illustration can be enlightening. There may be other ways of doing the deed, but, in my opinion, starting with beej is not a bad way to go. Regards, Dave |
|
#3
|
|||
|
|||
Re: TCP Sockets: multiple clients - one serverHi Dave,
The approach specified there, using the select() function, I have used on the client side. I was trying to use the process-based approach, instead of select(), or threads, or other approaches. Maybe I have to reconsider this. Thank you! |
|
#4
|
|||
|
|||
Re: TCP Sockets: multiple clients - one serverQuote:
Quote:
From Beej's Guide to UNIX Interprocess Communications "Another interesting thing to note from the above example is that both parent and child use the rv variable. Does this mean that it is shared between the processes? NO! If it was, I wouldn't have written all this IPC stuff. Each process has its own copy of all variables. There is a lot of other stuff that is copied, too, but you'll have to read the man page to see what." Regards, Dave Footnote: Also from the section on fork() in bgipc: "Now you know all about the mighty fork() function! It's more useful that a wet bag of worms in most computationally intensive situations, and you can amaze your friends at parties. Additionally, it can help make you more attractive to members of the opposite sex, unless you're male." So, even if you don't use everything you learn from Beej, it's still a Good Experience to check it out. In case you haven't figured it out, I am a huge Beej fan. (Thanks, Beej, for being there.) |
|
#5
|
|||
|
|||
Re: TCP Sockets: multiple clients - one serverVery true! I noticed that everything is copied, not referenced unfortunately, hence my trying with pointers, but those pointers are also copied. I tried static, which it means one single copy that is shared (or so I thought), still it does not work.
The example you mentioned goes hand in hand with the one in Stevens book (Unix Network Programming). I am actually changing the code right now. Thanks for your replies Dave! |
|
#6
|
|||
|
|||
Re: TCP Sockets: multiple clients - one serverQuote:
I didn't realize that you were working from Stevens. Volume 1 has been a mainstay for many sockets programmers since its first edition all those years ago. I recommend Beej for people with less experience because it's shorter and chattier (sometimes downright fun), has the "chat" server example, and even has some information about using Windows winsock programming. Regards, Dave |
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Named virtual host not working | Johnnyrotton | Apache Web Server Forum | 4 | 04-Sep-2007 21:32 |
| Connecting multiple sockets to a server | lockd | C++ Forum | 10 | 13-Aug-2007 18:24 |
| Develop a simple chat application consisting of a client and a server. HELP MEEEEEEE | elcrazy | C++ Forum | 1 | 11-Mar-2007 22:42 |
| Multiple Clients Trouble | Golmal | C Programming Language | 1 | 16-May-2006 03:43 |
| Microsoft SQL Server :: SQL Server message 241, state 1, severity 16 | lyuboe | MySQL / PHP Forum | 0 | 11-May-2005 05:31 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The