accept() is defined to always create another file descriptor to accept new connections from the client, but if it is known beforehand that we are only going to be accepting one client and one connection, why bother with creating a new file descriptor? Are there any descriptions of why this is the case in any defined standards?
accept() is defined to always create another file descriptor to accept new connections from
Share
The TCP protocol described in RFC 793 describes the terms socket and connection. A socket is an IP address and port number pair. A connection is a pair of sockets. In this sense, the same socket can be used for multiple connections. It is in this sense that the
socketbeing passed toaccept()is being used. Since a socket can be used for multiple connections, and thesocketpassed toaccept()represents that socket, the API creates a newsocketto represent the connection.If you just want an easy way to make sure the one
socketthataccept()creates for you is the same socket you used to do theaccept()call on, then use a wrapper FTW:If you are wanting a way for a client and server to connect to each other, without creating any more than just one
socketon each side, such an API does exist. The API isconnect(), and it succeeds when you achieve a simultaneous open.If instead you are worried about performance issues, I believe those worries are misguided. Using the TCP protocol, you already have to wait at least one full round trip on the network between the client and the server, so the extra overhead of dealing with another socket is negligible. A possible case where you might care about that overhead is if the client and server are on the same machine, but even then, it is only an issue if the connections are very short lived. If the connections are so short lived, then it would probably be better to redesign your solution to either use a cheaper communication medium (e.g., shared memory), or apply framing on your data and use a persistent connection.