How to test if socket connection is still there?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

How to test if socket connection is still there?

909件の閲覧回数
drummer
Contributor IV

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)

 

ラベル(1)
タグ(1)
0 件の賞賛
5 返答(返信)

477件の閲覧回数
Tgomes
Contributor III

Hi drummer,

 

 

I do not know if this resolves your case, but you can at least know if the ethernet cable is connected using the function ipcfg_get_link_active.

 

 

 

Best Regards.

0 件の賞賛

477件の閲覧回数
drummer
Contributor IV

thanks for your input.

I tried several things:

ipcfg_get_link_active(DEFAULT_ENET_DEVICE)

ipcfg_get_state(DEFAULT_ENET_DEVICE)

prior to initialization ipcfg_get_link_active returns 0

afterwords it returns 1 always.

ipcfg_get_state looked more promising because it returned 0,1,2,3 depending on its state but couldn't tell if I had disconnected.

One of
• IPCFG_STATE_INIT
• IPCFG_STATE_UNBOUND
• IPCFG_STATE_BUSY
• IPCFG_STATE_STATIC_IP
• IPCFG_STATE_DHCP_IP
• IPCFG_STATE_AUTO_IP
• IPCFG_STATE_DHCPAUTO_IP
• IPCFG_STATE_BOOT

 

 

0 件の賞賛

477件の閲覧回数
monXii
Contributor III

same issue here ..

.. some new ideas? =)

0 件の賞賛

477件の閲覧回数
Tgomes
Contributor III

Hi,

 

Have you checked the hardware connections? I ask this because I had a problem very similar to this, and the cause was the connection between the connector and the transceiver (DP83640). Two wires were reversed and some resistors in the wrong place.

 

Best regards.

0 件の賞賛

477件の閲覧回数
monXii
Contributor III

mh.. can't believe that the wiring is incorrect ..

.. cause ethernet works fine! but only the cable detection ..

i'll try it on the tower ..

0 件の賞賛