views:

336

answers:

3

I use java ServerSocket class for server application. Does accept() method participate in tcp/ip handshake?

As I see in tcp/ip dump and from command netstat, clients establish connections with before accept method is called and return Socket object.

Is it java issue, or I do not understand accept() semantics?

+6  A: 

Generally in Unix, if you mark the socket with listen(), the operating system starts accepting the connections. When you call the accept() function, the operating system simply hands over the already opened connection. Listen takes a parameter which allows you to specify how many 'non-accepted' open connections the OS allows (i.e. the size of queue).

ondra
A: 

Accept returns only after the client and server are connected (handshake, etc).

AlfaTeK
A: 

The accept method does not actively participate in the handshake, per se. The sending and receiving of the messages is implemented in the TCP/IP protocol stack, typically in the OS kernel space.

However, the accept() method is involved in the sense that the TCP/IP stack will only send the SYN-ACK message if some process has an accept() call active for a socket bound with the relevant IP address and port. When the three-way handshake is completed, the accept() method call will complete.

If no process calls accept() in time the incoming SYN message will be dropped by the kernel and the remote client will eventually time out the connection attempt. (On the other hand, if the IP address / port is not bound, then the kernel is likely to respond with a RST, and the remote client will see a "connection refused".)

Stephen C