Because of the holidays, TI E2E™ design support forum responses will be delayed from Dec. 25 through Jan. 2. Thank you for your patience.

This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

getsockopt and setsockopt not working properly

Hi

I am creating a socket first and then trying to read SO_SNDTIMEO using getsockopt function. But it always returns -1. After that i want to change the SO_SNDTIMEO to 1 seconds. I have updated the structure timeval and put the corresponding numbers in the structure and then calling setsockopt with SO_SNDTIMEO. setsockopt returns 0 which is successfull.Then i am calling connect to connect to a server and the server is off. Since connect is a blocking call, it is coming out of connect around 70 to 80 seconds.

My code:

stcp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if( stcp == INVALID_SOCKET )
    {
        printf("failed TCP socket create (%d)\n",fdError());
     printf("TCP server Fatal Error\n");
     if( stcp != INVALID_SOCKET )
         fdClose( stcp );
     return(-1);
    }
   
    bzero( &sin1, sizeof(struct sockaddr_in) );
    sin1.sin_family = AF_INET;

sin1.sin_len    = sizeof( sin1 )

sin1.sin_addr.s_addr = htonl(ipAddress);
 sin1.sin_port = htons(portNum);

size = sizeof(size);
error = getsockopt(stcp, SOL_SOCKET, SO_SNDTIMEO, &status, &size);

 timeout.tv_sec  = 0;
 timeout.tv_usec = 1000;
 sucess = setsockopt( stcp, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof( timeout ) );
   
    result = connect( stcp, &sin1, sizeof(sin1) );

How do we set the number of retries? What i want to do is when the server is off, i want to come out of connect with in 2 or 3 seconds. What are the parameters to be changed in the stack?

 

Regards

Raju

 

 

 

  • Raju,

    Another customer just had this same question today in a different post.  Here's the answer:

    You can reduce the connect timeout via the cfgAddEntry with the CFGITEM_IP_SOCKTIMECONNECT option

    For the full post, here's the link:

    http://e2e.ti.com/support/embedded/f/355/p/122385/441049.aspx#441049

    Steve

  • I am learning NDK stack. I have a question. How do i add cfgAddEntry to the existing configuration or should i create a new configuration and need to add to that configuration? but if i create a new configuration does the existing one works?

    Do I need to add cfgAddEntry  in the NDK stack thread? or can i add it in my application?

    Raju

  • Raju,

     

    Which version of the NDK are you using?

    You can add the following in your NDK stackThread (where all the NC_SystemOpen, NC_NetStart, etc. are done).

    Int timeout = 5;
    CfgAddEntry(hCfg, CFGTAG_IP, CFGITEM_IP_SOCKTIMECONNECT,CFG_ADDMODE_UNIQUE,

    sizeof(uint), (UINT8 *)&timeout, 0);

    Alternatively, if you are using NDK version 2.20.x *and* SYS/BIOS 6.x, you can generate the code to do this in the stackThread function via ti.ndk.config by adding the following into your *.cfg file:

    var Ip = xdc.useModule('ti.ndk.config.Ip');
    Ip.socketConnectTimeout = 5;

    Steve