2

Recently I'm looking into the RFC 793 to understand the TCP and the socket behaviour.

One question I have is: When an SYN received, should the listen socket immediately send the ACK or wait for APP layer call accept function.

Seams those behaviour is not explained in RFC 793, so does there any RFC explain the backlog/accept behaviour of an listening socket.

Community
  • 1
  • 1
ZijingWu
  • 3,350
  • 3
  • 25
  • 40
  • 1
    "The remote port of connected TCP client socket will not the same as you passed to the connect function." - This is not true. Please show the relevant part of the RFC which makes you think that it should be this way. – Steffen Ullrich Dec 13 '15 at 15:59
  • @SteffenUllrich, I got this from the experiment on MAC platform, and I think this make sense. If there is two connection comes in and accepted, they should not using the same port. Code attached. – ZijingWu Dec 13 '15 at 16:09
  • 1
    Sorry, but your code is wrong. It just prints random addr and ports. I'm not familiar with sockaddr_storage but if I use a simple sockaddr_in and skip all this casting everything works fine. – Steffen Ullrich Dec 13 '15 at 16:29
  • @SteffenUllrich, Thank you. I will edit the question. And if there is more than one client connect to the server, does that means the server will dispatch the packet to internal tcp socket using both the remote client port and local port? – ZijingWu Dec 13 '15 at 16:45
  • Different clients of the same service have different IP and/or port numbers. Thus in case of your program that would be a different getsockname for parallel clients while getpeername is the same. – Steffen Ullrich Dec 13 '15 at 16:51
  • So the tcp stack will receive packets with same destination port and different source port. I thought which packet will handled by which socket is determinate by which socket bind to the destination port. Seams that's not true. – ZijingWu Dec 13 '15 at 17:07
  • 1
    The server socket binds to a specific IP and port on the server and then receive all data which have this IP and port as the destination. Different connections to this socket will have the same destination but different source (IP and/or port). – Steffen Ullrich Dec 13 '15 at 17:11

1 Answers1

1

One question I have is: When an SYN received, should the listen socket immediately send the ACK or wait for APP layer call accept function

Usually the OS kernel does the TCP handshake no matter if the application is currently calling accept. But it might be different with some user-space TCP/IP implementations where the full IP stack is done inside the application. I don't think any RFC is mandating a specific behavior here since this detail is not relevant for the protocol.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • Then if the SYN + ACK sent, what will happen if remote start to send data, but there is no receiver exist. – ZijingWu Dec 13 '15 at 17:09
  • @ZijingWu: data will be spooled at the socket buffer. If the buffer is full the data will be discarded and no ACK will be sent. If the server closed the socket the client will get a connection reset back. – Steffen Ullrich Dec 13 '15 at 17:14
  • 1
    No. If the buffer is full the advertised receive window will close and the sender will stop sending. If data arrives that doesn't fiti into the socket receive buffer it would be a major protocol error. – user207421 Dec 13 '15 at 18:30