(?) Question on writing Server program for multiple client connections.

From vidya srinivasan

Answered By Faber Fedor, Kapil Hari Paranjape, Jim Dennis

Hi,

I had a question regarding servers. I want to know what happens when a server listening for multiple connections at a single port receives packets from two clients at the same time on the same port? Will the server discard one packet or accept both? Is there an order with which it accepts/discards them?

Thanks for replying in advance.

Sincerely, Vidya.

(!) [Faber] Two packets will never arrive at the port at the same time. For LAN technologies that I know of (Ethernet, Token Ring, ATM), there is only one packet on the wire at a time. Look into "Carrier Sense, Multiple Access/Collision Detection" to see how Ethernet does it.
I have no idea what happens on the really big pipes.
(!) [Kapil] I don't think the question is well phrased. Do you mean this from the kernel programmer's point of view or the socket programmer's point of view. I only understand something of the latter, which is explained below.
The man pages for socket(2), bind(2), listen(2), accept(2) and select(2) explain things quite well. The "info" pages for glibc are also a good source.
The final (presumably desirable) outcome of socket programming is to create a "socket connection" of the form (LOCALIP:PORT::REMOTEIP:PORT) which each of the processes (the local one and the remote one) can treat like a file descriptor for reading from and writing to.
If a program wishes to handle multiple connections of this type, then it must do this exactly in the way it would handle many files at the same time---by forking, threading or running on thousands of processors if possible :-)
The actual creation of the socket is handled by the kernel (in Linux) or the network daemon (in the Hurd). What the socket programmer does is to tell the kernel that
  1. a socket is needed via "socket"
  2. is to be bound to LOCALIP:PORT via "bind"
  3. to listen for (and queue) incoming connections via "listen"
  4. to accept a queued incoming (completed with REMOTEIP:PORT connection via "accept"
A file handle/descriptor is created in step (d). At this point the programmer must decide whether the program will handle multiple calls or not; in case the program(mer) feels energetic enough :-) the process forks or creates a new thread which then interacts with this particular file descriptor.
The "select" call is a mechanism by which the program notifies the kernel that it is waiting to "accept" and the kernel notifies the program when there is something to "accept".
(!) [JimD] I think the gist of his question was: How does the system disambiguate among multiple connection to the same TCP port?
The (short) answer is: All sockets (at any given point in time) are a unique combination of the following: source IP address, source port, destination IP address and destination port (and protocol, UDP or TCP). Thus the kernel internally routes data to the correct socket by looking at the source port as well as the the destination port.
I hope that's sufficient.


Copyright © 2003
Copying license http://www.linuxgazette.com/copying.html
Published in Issue 86 of Linux Gazette, January 2003
HTML script maintained by Heather Stern of Starshine Technical Services, http://www.starshine.org/


[ Table Of Contents ][ Answer Guy Current Index ] greetings   Meet the Gang   1   2   3   4 [ Index of Past Answers ]