How to test if socket connection is still there?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

How to test if socket connection is still there?

907 次查看
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 回复数

475 次查看
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 项奖励

475 次查看
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 项奖励

475 次查看
monXii
Contributor III

same issue here ..

.. some new ideas? =)

0 项奖励

475 次查看
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 项奖励

475 次查看
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 项奖励