7

I have created a socket in C++ and I needed it to have certain connection timeout. So that's what is happening:

  • Create socket
  • Make it NON_BLOCKING
  • Call connect
  • It returns -1 and errno EINPROGRESS as expected
  • Call select
  • Returns >0, so connection has been made
  • Make the socket BLOCKING again

Code for this part is the following:

bool mastControl::prepareSocket(char * address, int port, int * sockfd) {

    struct sockaddr_in serv_addr;
    struct timeval timeout = {0,100000};
    struct timeval connTimeout;
    struct hostent * server = NULL;
    fd_set socketSet;
    socklen_t lon;
    int sockOpt = 0;
    long socketFlags = 0;
    int buffersize = 8;
    int res = 0;
    int connectReturn = 0;
    const int WAIT_TO_RECONN = 15;

    server = gethostbyname(address);
    *sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (*sockfd < 0) {
        qDebug()<<"Impossible to open socket: "<<strerror(errno);
        return false;
    }

    if (server == NULL) {
        qDebug()<<"No such host: "<<strerror(h_errno);
        return false;
    }

    // Initializating server direction struct:
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr,
          (char *)&serv_addr.sin_addr.s_addr,
          server->h_length);
    serv_addr.sin_port = htons(port);

    // Making socked non-blocking in order to set a timeout value for connection:
    if((socketFlags = fcntl(*sockfd, F_GETFL, NULL)) < 0){
        qDebug()<<"Impossible to retrieve sockets descriptor flags "<<strerror(errno);
        return false;
    }
    socketFlags |= O_NONBLOCK;
    if(fcntl(*sockfd, F_SETFL, socketFlags) <0){
        qDebug()<<"Impossible to update sockets descriptor flags: "<<strerror(errno);
        return false;
    }

    connectReturn = connect(*sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr));
    if(connectReturn < 0){
        if(errno == EINPROGRESS){
            do{
                // Establishing a 15 seconds timeout:
                connTimeout.tv_sec = 15;
                connTimeout.tv_usec = 0;
                FD_ZERO(&socketSet);    // Initialising set of sockets as an empty set
                FD_SET(*sockfd, &socketSet);    // Adding socket to set
                connectReturn = select(*sockfd+1, NULL, &socketSet, NULL, &connTimeout);
                if(connectReturn<0 && errno!=EINTR){ // Error in select
                    qDebug()<<"Connection error in select function: "<<strerror(errno);
                    return false;
                }
                else if(connectReturn>0){ // Socket selected for writing
                    lon = sizeof(int);
                    if(getsockopt(*sockfd, SOL_SOCKET, SO_ERROR, (void*)(&sockOpt), &lon) <0){
                        qDebug()<<"Unnable to get socket options: "<<strerror(errno);
                        return false;
                    }
                    // Checking the value returned:
                    if(sockOpt){
                        qDebug()<<"Error in delayed connection: "<<strerror(errno);
                        return false;
                    }
                    break;
                }
                else{ // Timeout
                    qDebug()<<"Connection timeout exceeded: "<<strerror(errno);
                    return false;
                }
            } while (1);
        }
        else{
            qDebug()<<"Connection error: "<<strerror(errno);
            sleep(WAIT_TO_RECONN); // Wait 15 seconds
            return false;
        }
    }

    //Connected

    // Must set the socket as blocking again:
    if((socketFlags = fcntl(*sockfd, F_GETFL, NULL)) < 0){
        qDebug()<<"Impossible to retrieve sockets descriptor flags "<<strerror(errno);
        return false;
    }
    socketFlags &= (~O_NONBLOCK);
    if(fcntl(*sockfd, F_SETFL, socketFlags) <0){
        qDebug()<<"Impossible to update sockets descriptor flags "<<strerror(errno);
        return false;
    }

    if (setsockopt (*sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
                    sizeof(timeout)) < 0) {
        qDebug()<< "ERR  - setsockopt failed";
        return false;
    }

    if (setsockopt (*sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
                    sizeof(timeout)) < 0) {
        qDebug()<< "ERR  - setsockopt failed";
        return false;
    }
    if ((res = setsockopt (*sockfd, SOL_SOCKET, SO_SNDBUF, &buffersize,
                           sizeof(buffersize))) == -1) {
        qDebug()<< "ERR  - setsockopt failed (SO_SNDBUF) = " << res;
        return false;
    }
    //Socket Ready

    return true;
}

That works ok. But then I have a loop where I'm calling a function which checks if there is a new packet received to read:

bool mastControl::findPacket(int sockfd, st_messageMastToPc * messageReceived, bool * connected) {

    int n = 0;
    bool messageFound = false;
    char * buffer = (char *) messageReceived;
    unsigned int pos = 0;

    while ( ((n = read(sockfd, &(buffer[pos]), 1)) > 0) and not messageFound) {

        //qDebug()  << "read output " << n;
        if (n == 1) {
            pos++;

            if ( (pos == 1) && (buffer[0] == 2)) {
                // Some stuff...
            } else if ( (pos == 2) && (buffer[1] == 2) ) {
                // Some stuff...
            } else if (pos >= uiMessageMastToPcSize) {
                messageFound = true;
                //Complete message received
            } else if (pos < 2) {
                // Reseting pos
                pos = 0;
            }
        }
    }

    if (n < 0){
        qDebug()<< "Disconnected. Reason #" << errno << ": " << strerror(errno);
        *connected = false;
    }

    return messageFound;
}

Read function is giving EAGAIN as errno, which means "Resource temporarily unavailable". Then I'm supposing I am disconnected, and since boolean connected is now false, in the loop I will be trying to reconnect creating the socket again, ans so on.

So, in the first place I don't know why I'm receiving this error.

In the second place, I don't know if I am disconnected at all, or it's me the one who is disconnecting when creating the new socket after this error.

Any help?

Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207

1 Answers1

17

EAGAIN does not mean you're disconnected, it just means "there's nothing to read now; try again later".

You could either unset O_NONBLOCK with fcntl(2) (making read wait until there's something available), or just wait on the socket with something like select(2) before calling read.

EDIT: Now that you've added more code, I can see that you're setting SO_RCVTIMEO for the socket. This can cause a blocking read to return EAGAIN (so if you don't want that to happen, simply leave SO_RCVTIMEO alone).

James M
  • 18,506
  • 3
  • 48
  • 56
  • 1
    Ok, that was what I was expecting. But as I said, I set the socket to BLOCKING again after the select. Is that practice wrong? Do I need to call select before calling read every time then? Isn't there any other way of making read block the execution until has something to read? – Roman Rdgz Apr 25 '12 at 15:00
  • How did you set it to blocking mode? Are you sure it happens before you call `read`? Is there a `SO_RCVTIMEO` set? – James M Apr 25 '12 at 15:00
  • I have edited including the code creating the socket and configuring it to BLOCKING again. It happens before read for sure. And yes, as you can see at the end of the function, I'm setting SO_RCVTIMEO (I have inherited it in that way, but I think it doesn't work). Is that wrong? – Roman Rdgz Apr 25 '12 at 15:11
  • @RomanRdgz Ah, now I know what the problem is. Updated my answer. – James M Apr 25 '12 at 15:19
  • 1
    So, I shall erase SO_RCVTIMEO and that's it? I can't understand the problem, since it looks like the timeval set for RCV timeout is 10 seconds, isn't it? – Roman Rdgz Apr 25 '12 at 15:27
  • 1
    Would SO_SNDTIMEO affect to write function too? What about SO_SNDBUF? – Roman Rdgz Apr 25 '12 at 15:55