GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Nov-2007, 08:47
Printisor Printisor is offline
New Member
 
Join Date: Jun 2007
Posts: 6
Printisor is on a distinguished road

TCP Sockets: multiple clients - one server


Hi 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:
...
int clients[5], i = 0;
//int * clients = (int *)calloc(5, sizeof(int));
int sd;
... 
socket(...);
bind(...);
listen(...);
...
while (1) {
   sd = accept(...);
   clients[i] = sd;
   i++;
   ...
   if (fork() == 0) {
      ...
      for(i = 0; i < 5; i++) {
         send(clients(i), ...);
      }
      ...
   }
   ...
}

Any help is appreciated, thank you!
  #2  
Old 01-Nov-2007, 09:05
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: TCP Sockets: multiple clients - one server


Quote:
Originally Posted by Printisor
...help...

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  
Old 01-Nov-2007, 09:18
Printisor Printisor is offline
New Member
 
Join Date: Jun 2007
Posts: 6
Printisor is on a distinguished road

Re: TCP Sockets: multiple clients - one server


Hi 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  
Old 01-Nov-2007, 09:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: TCP Sockets: multiple clients - one server


Quote:
Originally Posted by Printisor
but the first child process created for the first client still has only one element
Quote:
Originally Posted by Printisor
I was trying to use the process-based approach

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  
Old 01-Nov-2007, 09:35
Printisor Printisor is offline
New Member
 
Join Date: Jun 2007
Posts: 6
Printisor is on a distinguished road

Re: TCP Sockets: multiple clients - one server


Very 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  
Old 01-Nov-2007, 10:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: TCP Sockets: multiple clients - one server


Quote:
Originally Posted by Printisor
...Stevens book (Unix Network Programming)...

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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Named virtual host not working Johnnyrotton Apache Web Server Forum 4 04-Sep-2007 20:32
Connecting multiple sockets to a server lockd C++ Forum 10 13-Aug-2007 17:24
Develop a simple chat application consisting of a client and a server. HELP MEEEEEEE elcrazy C++ Forum 1 11-Mar-2007 21:42
Multiple Clients Trouble Golmal C Programming Language 1 16-May-2006 02:43
Microsoft SQL Server :: SQL Server message 241, state 1, severity 16 lyuboe MySQL / PHP Forum 0 11-May-2005 04:31

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:18.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.