Can 'connect' call on socket return successfully without server calling 'accept'?

前端 未结 2 1032
青春惊慌失措
青春惊慌失措 2020-12-15 20:05

Server has created a socket and bound to a port and started a thread which is in loop to accept the connection. Sometime later loop exited due to an exception resulting in t

相关标签:
2条回答
  • 2020-12-15 20:38

    The connected sockets go into a queue waiting for the receiving process to accept() them. There is a limited backlog of these, once it is reached the OS will start either rejecting connections or ignoring them.

    0 讨论(0)
  • 2020-12-15 20:44

    If I understand correctly, 'connect' returns only after server does 'accept' on the listening socket. Am I missing something here?

    Yes. TCP establishes the connection - the 3-way handshake - under the covers and puts it in a completed connection queue when it is ready. Accept() returns the next waiting connection from the front of this queue.

    From the client's perspective it is "connected" but it won't be talking to anyone until the server accepts and begins processing. Sort of like when you call a company and are immediately put in the hold queue. You are "connected" but no business is going to be done until someone actually picks up and starts talking.

    Your individual thread may have died but the process is still alive and the file descriptor still open so TCP doesn't know what is going on at the application level.

    0 讨论(0)
提交回复
热议问题