Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the accept() function work?

Tags:

c

server

sockets

I have a question about the accept() function in C.

When a server receive a connection, the accept() function creates a new socket to communicate with the client, and then let the "old socket" listening for new connections.

Then, I understand that the server can communicate with the client through the "new socket", but how can the client communicate with the "new socket" (because the client don't know about this "new socket") ?

like image 613
LogicTest Avatar asked Jan 06 '17 19:01

LogicTest


People also ask

What does accept () do in Python?

The .accept() method blocks execution and waits for an incoming connection. When a client connects, it returns a new socket object representing the connection and a tuple holding the address of the client. The tuple will contain (host, port) for IPv4 connections or (host, port, flowinfo, scopeid) for IPv6.

What is accept () method?

The accept() method of ServerSocket class is used to accept the incoming request to the socket. To complete the request, the security manager checks the host address, port number, and localport.

What is accept () in networking?

The accept() system call with the connection-based socket types(SOCK_STREAM, SOCK_SEQPACKET). It extracts the first connection request on queue of pending connections for the listening socket, sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket.

What does the socket accept () method return?

RETURN VALUE Upon successful completion, accept() shall return the non-negative file descriptor of the accepted socket. Otherwise, -1 shall be returned and errno set to indicate the error.


1 Answers

On the server side, the listening socket is associated with only a local IP and port and is in the LISTEN state.

In contrast, accepted sockets on the server (as well as connected sockets on the client) are identified by a local IP and port as well as a remote IP and port and is in the ESTABLISHED state.

On the client side, it doesn't matter that the server uses a listening socket separate from the connected socket. By the time the client returns from connect, the server has returned from accept and the socket descriptors returned from each can communicate with each other.

like image 115
dbush Avatar answered Oct 06 '22 00:10

dbush