1

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.

Lavi
  • 19
  • 2
  • 2
    Define "not working". Please show the code you've tried, along with any input, expected output, and actual output. – dbush Nov 09 '21 at 13:44
  • 2
    The silly library uses `char *optval` as the 4th argument. Newer (Posix) versions (such as Linux) use a `void *optval` , which can be passed without a cast. – wildplasser Nov 11 '21 at 12:14
  • Thank you! @wildplasser That solves my Warning, Currently, No warning but setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,&timeout,sizeof(timeout)) is giving -1 So, It's not setting the timeout. – Lavi Nov 11 '21 at 12:29
  • You should check `errno` on a `-1` return. (maybe this IOCTL is not implemented) – wildplasser Nov 11 '21 at 12:48
  • 1
    Sorry @wildplasser , I misread my program! Error no is : 330 – Lavi Nov 11 '21 at 13:03
  • I don't see SO_RCVTIMEO mentioned in hp-nonstop manuals, however its defined in the sys/socket.h, you need to check with hp-nonstop support to confirm if SO_RCVTIMEO is implemented or not – Pras Nov 12 '21 at 09:18
  • It is there in tandem libraries, `#define SO_SNDTIMEO 0x1005 /* send timeout */ #define SO_RCVTIMEO 0x1006 /* receive timeout */` – Lavi Nov 19 '21 at 12:33
  • 1
    Read my earlier comment again. SO_RCVTIMEO is there in the header file that's why your code compiled. But hp-nonstop might not have implemented it as it(SO_RCVTIMEO) is not mentioned in the manual of hp-nonstop programming. I guess only hp-nonstop support could confirm that – Pras Nov 22 '21 at 04:37

2 Answers2

0

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);
James Anderson
  • 27,109
  • 7
  • 50
  • 78
  • `int timeout=5; int error; socklen_t sl; sl = (int) sizeof(timeout); error=setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO,(char*)&timeout,sl);` I have tried like this and there is no timeout and error no is 330!!! – Lavi Nov 13 '21 at 19:00
  • timeout should be a timeval struct not a plain int! – James Anderson Nov 14 '21 at 11:07
  • I added as struct but still no result!! – Lavi Nov 16 '21 at 12:18
-1
        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