socket: getsockopt

I am trying to make a non-blocking socket and determine if the connect
completed by using the select function to see if it is writable and then
checking the
SO_ERROR from the getsockopt function. (QNX4.25)

I am having problems with the following:

  1. first the function call to set the flags seem to be working because I get
    the EINPROGRESS error returned. But when I use the function call to get the
    flags it isn’t different the the first set. ie: I printed out the flags
    before and after and get the same thing.

  2. gettsockopt gives me a SIGSEG fault am I doing the getsockopt correct


    struct timeval selectTimeout;
    fd_set fdSet;
    int rc;
    int sock;
    int on;
    struct sockaddr_in server;
    struct hostent *hp;

sock = socket(AF_INET,SOCK_STREAM,0);
if (sock < 0)
{ …}

server.sin_family = AF_INET;
hp = gethostbyname(“123.12.12.12”); // my ip address
if (hp == 0)
{ …}

bcopy(hp->h_addr_list[0], &server.sin_addr.s_addr,hp->h_length);
server.sin_port = htons(2000);

flags = fcntl(sock,F_GETFL,0);
fcntl(sock,F_SETFL,flags | O_NONBLOCK);

rc = connect(sock,(struct sockaddr *)&server, sizeof(server));
if (rc < 0)
{
if (errno != EINPROGRESS)
{ // cannot connect at this time
close(sock);
exit(1);
}
selectTimeout.tv_sec = 10;
selectTimeout.tv_sec = 0;

FD_ZERO(&fdSet);
FD_SET(sock,&fdSet);

rc = select(sock + 1,
NULL, // readable
&fdSet, // writable
NULL,
&selectTimeout);
if (rc < 0)
{ // select error
}
else if (rc == 0)
{ // none of the fd’s within fdSet are writable
}
else
{
if (FD_ISSET(sock,&fdSet))
{
// sock in fdSet is writable - just because it is writable
doesn’t mean that
// it is connected check the socket error flags
// problem is here ??? I get a segment
fault
x = getsockopt(sock,SOL_SOCKET,SO_ERROR,(int *)&on,sizeof(int));
}
else
{ // not writable
}
}
}

Thanks for the help!

Hello,

I figured out the problem with getsockopt. (it was stupid user error) I was
sending an int for the length instead of the pointer. I am still having
problems with the function call.

Thanks for the help.