"connect" between "bind" and "listen"

"connect" between "bind" and "listen"

Post by John Templ » Mon, 30 May 1994 01:54:14



What happens if a TCP server binds its well known port, and a
connection request is issued by a client between that time and the time
the server begins listening?  Will the client's connection request be
refused, or will it be held until the server starts listening?
--
John W. Temples, III       ||       Providing the only public access Internet
Gulfnet Kuwait             ||            site in the Arabian Gulf region

 
 
 

"connect" between "bind" and "listen"

Post by Chris P Murr » Wed, 01 Jun 1994 20:56:50


: What happens if a TCP server binds its well known port, and a
: connection request is issued by a client between that time and the time
: the server begins listening?  Will the client's connection request be
: refused, or will it be held until the server starts listening?

TCP uses a three-way handshake when establishing connections to synchronise
connections correctly.  You should be okay as long as the connection
doesn't take to long to complete or it will time out.  Also, make sure
you can handle reads and writes to the server failing until the
connection has established, be careful of non-blocking i/o.  If you
use blocking i/o you may not need to take any precautions.

-Chris.

 
 
 

"connect" between "bind" and "listen"

Post by W. Richard Steve » Wed, 01 Jun 1994 23:57:21


Quote:> What happens if a TCP server binds its well known port, and a
> connection request is issued by a client between that time and the time
> the server begins listening?  Will the client's connection request be
> refused, or will it be held until the server starts listening?

My first suggestion is "try it and see what happens".  Just modify any
server by putting a sleep() between the bind() and listen().  Use telnet
to try and connect to the server (since you can telnet to any port number),
and watch what happens with a free tool such as tcpdump.

What you'll see is that the connection requests from the client are
ignored by the server, because the server hasn't called listen().
Most clients retransmit their SYNs a fixed number of times, so the
client should get through eventually.

By default all sockets are created "active" and with a state of CLOSED
(use netstat -a to see this).  One part of listen() is to convert an
active socket into a passive socket, and you'll see the state change
to LISTEN with netstat.  Only in the LISTEN state will an incoming SYN
be accepted.

        Rich Stevens