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() for SO_LINGER

Other Parts Discussed in Thread: TM4C1294NCPDT

Using CCS 6.0.x, a TIVA CPU TM4C1294NCPDT, RTOS 2.1.0.03 and ndk_2_23_01_01, I want to set and verify the socket options on a socket.  I am using a TCP/IP socket where I want to disable “linger” and “Nagle”.  Before I change the options, I want to check the options with getsockopt(), change them with setsockopt() and then check the new value with getsockopt().

 

The Code is below for the "pre check" to see the initial values and the TCP_NODELAY works as expected but the SO_LINGER does not work.  The comments in the code show the results but the function call getsockopt(lSocket, SOL_SOCKET, SO_LINGER, &optval, &optlen);  returns 0xffffffff and when fdError() is checked it returns a 22 indicating invalid argument. I do not know why the SO_LINGER check fails.

 

Thanks,

Doug   

 

 

void setSocketOption(void)

{

    int optval;

    int optlen = sizeof(optval);

    unsigned long rc;

 

    rc = getsockopt(lSocket, SOL_SOCKET, TCP_NODELAY, &optval, &optlen);

    if(rc) // rc is 0 here, so this works

    {

       printf ("Error, getsockopt 'no delay' error Code %d\n", fdError());

       // see serrno.h \\tirtos_tivac_2_01_00_03\products\ndk_2_23_01_01\packages\ti\ndk\inc

    }

    else

    {

       printf ("nodelay value = %d\n", optval); // this works OK

    }

 

 

    rc = getsockopt(lSocket, SOL_SOCKET, SO_LINGER, &optval, &optlen); //

    if(rc) // this returns 0xffffffff

    {  // see serrno.h \\tirtos_tivac_2_01_00_03\products\ndk_2_23_01_01\packages\ti\ndk\inc for fdError() return values

       printf ("Error getsockopt 'linger' error Code %d\n", fdError());  // fdError indicates (22), -> /* Invalid argument */

    }

    else

    {

       printf ("linger value = %d\n", optval);

    }

 

 

 

 

 

 

 

  • Doug,

    The set/getsockopt functions take a different type of argument for the pbuf parameter when SO_LINGER is specified.  You must pass a linger struct in this case.

    Here's some example code:

        struct linger lingerConfig;

        System_printf("before SO_LINGER ...\n");
        optlen = sizeof(struct linger);
        memset(&lingerConfig, 0, optlen);

        getsockopt(lSocket, SOL_SOCKET, SO_LINGER , &lingerConfig, &optlen);

        optlen = sizeof(struct linger);
        memset(&lingerConfig, 0, optlen);
        lingerConfig.l_onoff = 1;
        lingerConfig.l_linger = 100;

        setsockopt(lSocket, SOL_SOCKET, SO_LINGER , &lingerConfig, optlen);

    Steve

  • That was it,

    Thanks,


    Doug