Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 thread exit but socket is still bounded to port. Now if client does a 'connect' to this server, it is succeeding. How is it possible? If I understand correctly, 'connect' returns only after server does 'accept' on the listening socket. Am I missing something here?

like image 305
kumar Avatar asked Mar 09 '10 13:03

kumar


People also ask

Why does the accept () return a new socket for each accepted connection?

Because the initial socket is used to wait for communication while the second is used to communicate.

What is socket Connect return?

RETURN VALUES. If successful, connect() returns a value of zero. On failure, it returns -1 and sets errno to one of the following values. For a blocking socket, the return value indicates success or failure of the connection attempt.

What is the purpose of socket connect () method?

This method is typically used immediately after a call to GetHostAddresses, which can return multiple IP addresses for a single host. If you are using a connection-oriented protocol such as TCP, the Connect method synchronously establishes a network connection between LocalEndPoint and the specified remote endpoint.

Is socket connect call blocking?

connect is a blocking call by default, but you can make it non blocking by passing to socket the SOCK_NONBLOCK flag.


1 Answers

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.

like image 147
Duck Avatar answered Sep 20 '22 06:09

Duck