Reputation: 1639
From what I understood from all the documentations of select()
, it seems that select()
ing on a write fd_set could be used to check for socket (descriptor) availibility for send()
ing so that could be used to detect a successful non-blocking connect()
attempt, but what I don't get is when does a socket ever become unavailable after a successful connect()
or accept()
?
And does that in theory mean that the socket is always available for send()
ing?
As a last question, is it practical to keep select()
ing connected sockets in for write operations for the whole session?
Thanks.
Upvotes: 1
Views: 1056
Reputation: 310911
'Available for sending' just means that there is room in the socket's send buffer. This is true most of the time, starting from when the connection completes and the buffer is allocated. It is only ever untrue when the buffer fills, which in turn only happens when the target socket's receive buffer fills, which only happens if the reading application is slower than the writer.
Upvotes: 2
Reputation: 596307
When using a non-blocking socket, any operation can fail with an WSAEWOULDBLOCK error. In the case of connect() and send(), select() can be used to determine when a pending connect() call has successfully connected to the server, or when the socket is writable so it can accept new data without blocking.
Upvotes: 2
Reputation: 86333
The most common case of a socket being unavailable for writing is when the connection goes through a relatively slow network link that your application can saturate. The operating system will buffer a limited amount of data, so your application will have to restrain itself by checking if the socket is available (indicating that some data has been sent and the buffer has free space) before sending more.
As for your other question, if you have multiple sockets of any kind in use by a single thread, as is the case with e.g. a web server, it certainly makes sense to use select() in order to manage them efficiently.
Upvotes: 4