old-www/LDP/LG/issue86/tag/4.html

188 lines
6.9 KiB
HTML

<!--startcut ==============================================-->
<!-- *** BEGIN HTML header *** -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD>
<META NAME="generator" CONTENT="lgazmail v1.4G.a">
<TITLE>The Answer Gang 86: Question on writing Server program for multiple client connections.</TITLE>
</HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
ALINK="#FF0000">
<!-- *** END HTML header *** -->
<!-- begin 4 -->
<H3 align="left"><img src="../../gx/dennis/qbubble.gif"
height="50" width="60" alt="(?) " border="0"
>Question on writing Server program for multiple client connections.</H3>
<p><strong>From vidya srinivasan
</strong></p>
<p></strong></p>
<!-- sig -->
<!-- sig -->
<p align="right"><strong>Answered By Faber Fedor, Kapil Hari Paranjape, Jim Dennis
</strong></p>
<P><STRONG>
Hi,
</STRONG></P>
<P><STRONG>
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?
</STRONG></P>
<P><STRONG>
Thanks for replying in advance.
</STRONG></P>
<P><STRONG>
Sincerely,
Vidya.
</STRONG></P>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [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.
</blockQuote>
<blockQuote>
I have no idea what happens on the really big pipes.
</blockQuote>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [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.
</blockQuote>
<blockQuote>
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.
</blockQuote>
<blockQuote>
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
<EM>treat</EM> like a file descriptor for reading from and writing to.
</blockQuote>
<blockQuote>
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
<IMG SRC="../../gx/dennis/smily.gif" ALT=":-)"
height="24" width="20" align="middle">
</blockQuote>
<blockQuote>
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
</blockQuote>
<blockQuote><ol>
<LI>a socket is needed via "socket"
<LI>is to be bound to LOCALIP:PORT via "bind"
<LI>to listen for (and queue) incoming connections via "listen"
<LI>to accept a queued incoming (completed with REMOTEIP:PORT
connection via "accept"
</ol></blockQuote>
<blockQuote>
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
<IMG SRC="../../gx/dennis/smily.gif" ALT=":-)"
height="24" width="20" align="middle"> the process
forks or creates a new thread which then interacts with this
particular file descriptor.
</blockQuote>
<blockQuote>
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".
</blockQuote>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [JimD]
I think the gist of his question was: How does the system disambiguate
among multiple connection <EM>to</EM> the same TCP port?
</blockQuote>
<blockQuote>
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.
</blockQuote>
<blockQuote>
I hope that's sufficient.
</blockQuote>
<!-- end 4 -->
<!-- *** BEGIN copyright *** -->
<hr>
<CENTER><SMALL><STRONG>
<h5>
<br>Copyright &copy; 2003
<br>Copying license <A HREF="">http://www.linuxgazette.com/copying.html</A>
<BR>Published in Issue 86 of <i>Linux Gazette</i>, January 2003</H5>
</STRONG></SMALL></CENTER>
<!-- *** END copyright *** -->
<SMALL><CENTER><H6 ALIGN="center">HTML script maintained by
<A HREF="mailto:star@starshine.org">Heather Stern</a> of
Starshine Technical Services,
<A HREF="http://www.starshine.org/">http://www.starshine.org/</A>
</H6></SMALL></CENTER>
<HR>
<!--startcut ======================================================= -->
<P> <hr>
<!-- begin tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::-->
<p align="center">
<table width="100%" border="0"><tr>
<td align="right" valign="center"
><IMG ALT="" SRC="../../gx/navbar/left.jpg"
WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="middle" border="0"
><A HREF="../index.html"
><IMG SRC="../../gx/navbar/toc.jpg" align="middle"
ALT="[ Table Of Contents ]" border="0"></A
><A HREF="../lg_answer.html"
><IMG SRC="../../gx/dennis/answertoc.jpg" align="middle"
ALT="[ Answer Guy Current Index ]" border="0"></A></td>
<td align="center" valign="center"><A HREF="../lg_answer.html#greeting"><img align="middle"
src="../../gx/dennis/smily.gif" alt="greetings" border="0"></A> &nbsp;
<A HREF="../../tag/bios.html">Meet&nbsp;the&nbsp;Gang</A> &nbsp;
<A HREF="1.html">1</A> &nbsp;
<A HREF="2.html">2</A> &nbsp;
<A HREF="3.html">3</A> &nbsp;
<A HREF="4.html">4</A>
</td>
<td align="left" valign="center"><A HREF="../../tag/kb.html"
><IMG SRC="../../gx/dennis/answerpast.jpg" align="middle"
ALT="[ Index of Past Answers ]" border="0"></A
><IMG ALT="" SRC="../../gx/navbar/right.jpg" align="middle"
WIDTH="14" HEIGHT="45" BORDER="0"></td></tr></table>
</p>
<!-- end tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::::-->
<!--endcut ========================================================= -->
<P> <hr>
<!--startcut ======================================================= -->
<CENTER>
<!-- *** BEGIN navbar *** -->
<!-- *** END navbar *** -->
</CENTER>
</p>
<!--endcut ========================================================= -->
<!--startcut ======================================================= -->
</BODY></HTML>
<!--endcut ========================================================= -->