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.

NDK: Socket is live or not

MCU: TM4C1294NCPDT

TI-RTOS: v2.01.00.03

NDK: v2.23.01.01

CCS: v6.0.1.0040

-

Hello,

    How do we figure out that just before we put some data in a socket (outgoing data) the socket is still connected? Which function should be or can be used for this?

-

    When we call the connect() function, the TCP socket gets connected & when we call the send() function, data is sent out through that socket. I understand that if the socket is NOT connected, then send() function will reply/return some error code, but before initiating a data sending through the socket using send() function, I want to check whether the socket connection is still live or it have been disconnected by the network terminal connected at the other end of the network. How to accomplish this?

-

Thanks

Regards

Soumyajit

  • Hi Soumyajit,

    Have you tried calling "getpeername()"?

    Steve

  • Hi Steve,

     Thanks for pointing out the function getpeername(). Now I am trying to wait until an IP address is assigned to my board by the remote DHCP server. The code follows:

    -

    wait_until_connected:

    // GET PEER NAME

    tmp1_sizeof_sockaddr = sizeof(sin1);

    peer_name_response = getpeername(s, (struct sockaddr *)&sin1, &tmp1_sizeof_sockaddr);

    System_printf("getpeername() = %d. sock_addr = %u!!\n", peer_name_response, (unsigned int)sin1.sin_addr.s_addr);

    if(!sin1.sin_addr.s_addr)

    {

    System_printf("TCP Starting Point: Peer NOT Connected!!\n");

    Task_sleep(2000);

    goto wait_until_connected;

    }

    -

     Here, I find that the loop is becoming infinite & never coming out to execute codes placed after this loop. I can see from the below console dump that the IP address have been allocated to the MCU by the remote DHCP server, still getpeername is not working as expected. Please help. I continuously find the variables "peer_name_response" & "sin1.sin_addr.s_addr" stuck to value zero.

    -

    Service Status: DHCPC : Enabled : : 000
    Service Status: HTTP : Enabled : : 000
    Service Status: DHCPC : Enabled : Running : 000

    //  Then after a while, I find the following printed to the console:

    Network Added: If-1:192.168.1.206
    Service Status: DHCPC : Enabled : Running : 017

    -

    Thanks

    -

    Regards

    Soumyajit

  • Hi Soumyajit,

    When is this code running?  Are you calling getpeername() before or after the call to connect(s)?

    if getpeername() returns 0 (success) and the struct sin1 was not populated, I believe it means that the socket "s" is not connected.

    Can you try calling getpeername() both before the call to connect(s) and after the call?  Are the results the same?

    Hunting around on the web a bit, I found a thread that discusses using recv() to detect whether or not the connection has been dropped or not.  From the thread, it seems that the reliability of this method is debatable.

    Steve

  • Hi Steven,
    My earlier code used to check for getpeername() before connect() was called & it always ended up struct sin1 not being populated. Now I am using getpeername() after connect() & its working. If the IP address have not being assigned then connect() is returning value less than 1 and I am exiting any further statement containing send() or recv().
    As of now, I am okay with the solution. The trick is getpeername() will not work before connect() is called (at least once), although in such a case getpeername() will still return zero indicating that the function is successfully running.
    -
    Thanks
    -
    Regards
    Soumyajit Das