network programming: how does s.accept() work?

Roy Smith roy at panix.com
Tue Feb 26 21:06:22 EST 2008


In article 
<a4fdb3cd-f6a6-4f1b-8221-f312d0ee2a23 at s19g2000prg.googlegroups.com>,
 7stud <bbxx789_05ss at yahoo.com> wrote:

> If two sockets are bound to the same host and port on the server, how
> does data sent by the client get routed?  Can both sockets recv() the
> data?

Undefined.

You certainly won't find the answer in the RFCs which define the protocol 
because sockets aren't part of the protocol.

Unfortunately, you won't find the answer in the Socket API documentation 
either because the socket API documentation is pretty vague about most 
stuff.

One possible answer is that the operating system won't let you bind two 
sockets to the same (address, port) pair.  But, another possibility is that 
it will.  And even if it won't, consider the case of a process which forks; 
the child inherits the already bound socket from the parent.

So, either way, you're left with the question, what happens with two 
sockets both bound to the same (address, port) pair?  For the sake of 
simplicity, I'm assuming UDP, so there's no connection 4-tuple to worry 
about.  The answer is, again, undefined.  One reasonable answer is that 
packets received by the operating system are doled out round-robin to all 
the sockets bound to that port.  Another is that they're duplicated and 
delivered to all sockets.  Anything is possible.

But, as other posters have said, this really isn't a Python question.  This 
is a networking API question.  Python just gives you a very thin layer on 
top of whatever the operating system gives you, and lets all the details of 
the OS implementation quirks shine through.



More information about the Python-list mailing list