Skip to content

Commit 4725184

Browse files
committed
Merge pull request nasa#909 from jphickey/fix-862-socket-connect-select
Fix nasa#862, comments describing select after connect
2 parents 58c172c + ef94a3d commit 4725184

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/os/portable/os-impl-bsd-sockets.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,24 @@ int32 OS_SocketConnect_Impl(const OS_object_token_t *token, const OS_SockAddr_t
265265
{
266266
if (errno != EINPROGRESS)
267267
{
268+
OS_DEBUG("connect: %s\n", strerror(errno));
268269
return_code = OS_ERROR;
269270
}
270271
else
271272
{
273+
/*
274+
* If the socket was created in nonblocking mode (O_NONBLOCK flag) then the connect
275+
* runs in the background and connect() returns EINPROGRESS. In this case we still
276+
* want to provide the "normal" (blocking) semantics to the calling app, such that
277+
* when OS_SocketConnect() returns, the socket is ready for use.
278+
*
279+
* To provide consistent behavior to calling apps, this does a select() to wait
280+
* for the socket to become writable, meaning that the remote side is connected.
281+
*
282+
* An important point here is that the calling app can control the timeout. If the
283+
* normal/blocking connect() was used, the OS/IP stack controls the timeout, and it
284+
* can be quite long.
285+
*/
272286
operation = OS_STREAM_STATE_WRITABLE;
273287
if (impl->selectable)
274288
{
@@ -282,6 +296,10 @@ int32 OS_SocketConnect_Impl(const OS_object_token_t *token, const OS_SockAddr_t
282296
}
283297
else
284298
{
299+
/*
300+
* The SO_ERROR socket flag should also read back zero.
301+
* If not zero, something went wrong during connect
302+
*/
285303
sockopt = 0;
286304
slen = sizeof(sockopt);
287305
os_status = getsockopt(impl->fd, SOL_SOCKET, SO_ERROR, &sockopt, &slen);

0 commit comments

Comments
 (0)