Setsockopt function with time limit is not working in C on hp non-stop

467 Views Asked by At

I am trying to set up timeout for receiving data via socket.

 struct timeval 
 {
     time_t      tv_sec;
     long int    tv_usec;
 };

struct timeval tv;
tv.tv_sec = 5;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
      sprintf(RESULTS, "%s","ERROR");
      return 0;
}
else
{
        /*Connecting to server socket*/
        if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0)
        {
            /*Writing results back to COBOL variable*/
            sprintf(RESULTS, "%s","!SENT");
            return 0;
        }
        else
        {
          if(setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) < 0)
          {
             if(send(sockfd, buff, sizeof(buff), 0)<0)
             {
                /*Writing results back to COBOL variable*/
                sprintf(RESULTS, "%s","ERROR");
                /*Closing the socket*/
                FILE_CLOSE_((short)sockfd);
                return 0;
             }
            else
            {
                /* Receiving data from server*/
                bzero(buff, sizeof(buff));
                recv(sockfd, buff, sizeof(buff),0);
                    
                /*Writing results back to COBOL variable*/
                sprintf(RESULTS, "%s","SUCCESS");

                /*Closing the socket*/
                FILE_CLOSE_((short)sockfd);
                return 0;
            }
          }
        }
     
}

Expected output: Program should wait for 5 secs and if it doesn't get reply from the server then it should close the socket and get out of it.

Actual Output: Warning:argument 4 conflicts with formal definition

Implemented based on this example, I found an example in C

But this is not working on Tandem-C

I have referred to the C/C++ programming manual for Tandem but didn't found with timeout. Tandem-Manual-Page 185

Thanks in advance.

2

There are 2 best solutions below

3
Алексей Неудачин On
        qcap = socket(AF_INET , SOCK_STREAM , 0);
        int timeout = 1798;
        setsockopt(qcap, 6, 18, (char*)&timeout, sizeof(timeout));
        connect(qcap, (sockaddr *)&server_bn, sizeof(server_bn));

https://github.com/alexeyneu/BlockZero/blob/29ed505a211d14d4577012438cd8bec9d15f1272/connection/x.cpp#L93-L96

3
James Anderson On

Looks like its a particular problem to tandem.

For some historical reason sockelen_t is defined as a palin int. But sizeof returns an unissigned int. You need to define a socklen variable and set is to the sizeof value.

Something like:

socklen_t sl;
sl = (int) sizeof(timeout);