Please take a look on following code:
s = socket(PF_INET, SOCK_STREAM, 0); //socket
fcntl(s, F_SETFL, O_NONBLOCK); //set socket to nonblock
retry_conn:
ret = connect(s, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr_in)); // try to connect, for sure there no connection to "serv_addr"
printf("connect, status=%d, errno=%d \n", status, errno);
if (errno == EINPROGRESS) //no connection
{
if (poll(fds, 1, delay_time / 1000) > 0)
{
ret = getsockopt(s, SOL_SOCKET, SO_ERROR, &error_s, &len_s)
printf("getsockopt, status: %d, error_s=%d, errno=%d\n", status, error_s, errno);
}
if (error_s)
{
printf("try_connect_again\n");
goto: try_connect_again;
}
else
{
connected!!!!!!!!!
}
}
And now, what is the problem. When "connect" is executed for the first time (return code = 150), "getsockopt" in "error_s" return code "146", but after "goto"
when "getsockopt" is executed second time "error_s" is equal "0" (which means connection is established successfully).
Example output:
connect, status=-1, errno=150
getsockopt, status: 0, error_s=146, errno=150
try_connect_again
connect, status=-1, errno=150
getsockopt, status: 0, error_s=0, errno=0
The same code executed on i.e. Linux CentOS or HPUX, when getsockopt is executed second time, return error codes and inform that there is no connection but in case of Solaris
"getsockopt" works in a strange way.
Please explain what is the problem or how to deal with such behavior on Solaris.
Thanks in advance.