RICK BOURGEOIS

How to test if socket connection is still there?

Discussion created by RICK BOURGEOIS on Apr 26, 2012
Latest reply on May 22, 2012 by Sascha Ehlers

I am using MQX v3.6 with colfire 7.2. I have searched the board and can't find any answer so I'm putting it out there.

How can I check if my socket connection is still there?

I am using accept() and once inside it doesn't return unless the other connection has been aborted.

But what if the cable is diconnected or the connection is lost?

How can I tell?

In my code example I am checking each time that the server on the other side is always the original to prevent some other location from stealing my connection.

 

   while(socket_flag == TRUE)    {    sock2 = accept(listensock, &raddr, &addr_length);    return_error_if(sock2 == RTCS_SOCKET_ERROR);    if(addr.sin_addr.s_addr == raddr.sin_addr.s_addr)    {     shutdown(sock, FLAG_CLOSE_TX);     fclose(sockfd);     sockfd = fopen("socket:", (char_ptr)sock2);     socket_flag = TRUE;    }    else    {     diag_led_red();     RTCS_time_delay(100);     continue;    }    sock = accept(listensock, &addr, &addr_length);    return_error_if(sock == RTCS_SOCKET_ERROR);    if(addr.sin_addr.s_addr == raddr.sin_addr.s_addr)    {     shutdown(sock2, FLAG_CLOSE_TX);     fclose(sockfd);     sockfd = fopen("socket:", (char_ptr)sock);     socket_flag = TRUE;    }    else    {     diag_led_grn();     RTCS_time_delay(100);     continue;    }           }//while(socket_flag == TRUE)

 

Outcomes