accept()¶
Purpose¶
accept() accepts a pending incoming connection and returns a new connected socket fd.
The listening socket remains open for future connections.
Header¶
Prototype¶
Minimal Example¶
struct sockaddr_in peer;
socklen_t peer_len = sizeof(peer);
int client_fd = accept(server_fd, (struct sockaddr *)&peer, &peer_len);
if (client_fd < 0) {
perror("accept");
return -1;
}
Common Pitfalls¶
- Forgetting to set the accepted client socket to non-blocking mode.
- Not handling
EAGAINwhen the listening socket is non-blocking. - Confusing the listening socket with the connected client socket.