When you debug your project code, you find a strange problem that is recorded as follows:
In non-blocking mode, connect initiates a chain and returns-1 (this is a normal phenomenon in non-blocking mode). Then listen to the write event for the socket, and when the getsockopt function gets the error (SO_ERROR) when the write event is triggered, no error is detected (the fourth parameter returns 0), and the last error occurs in the write operation.
Reason: Due to a problem with the profile, the resulting IP address to the end is an empty string "":
struct sockaddr_in saddr;
SADDR.SIN_ADDR.S_ADDR = inet_addr ("");
saddr.sin_family = af_inet; /* "Default" Family * *
Saddr.sin_port = htons (22);
However, when Connect returns-1, the error code is not detected as einprogress:
if (Connect (M_hsocket, paddr, naddrlen) = = 0)
{return
true;
}
else
{return
false;
}
Then you start listening for write events directly. The write event is triggered immediately because the write connection on the socket socket has been turned off because of a connect error.
When getsockopt is invoked, the error code obtained is 0, and no error is considered.
In the next write operation, write writes to a closed connection, causing the sigpipe signal to be triggered.
Summarize:
Although the non-blocking mode of connect, generally are returned to 1, but forgot to detect whether errno is einprogress, like this problem, because the address is wrong, connect return-1, but at this time the error is "Network is Unreachable ", in which case you can no longer listen for write events on the socket and call getsockopt in the callback function because getsockopt cannot get the error code and will only return 0.