TCP sockets - how do I figure out if the client is still connected?

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

TCP sockets - how do I figure out if the client is still connected?

跳至解决方案
1,901 次查看
projektecarus
Contributor II

Hey,

I am trying to turn a K60f120m board, using CW 10.3 and MQX 3.8, into a TCP server. Most of the basics are already running, however, I have trouble detecting clients that disconnect in an unusual way, for example by removing the ethernet cable. MQX just keeps sending and trying to receive data. At the moment I am using RTCS_ping() as a workaround. If a socket fails to respond too often, I shut it down. This is obviously not a viable long-term solution. Isn't there a more elegant way to detect an inactive client? Isn't TCP supposed to realize if it sends a message that doesn't arrive at the remote socket?

Thanks in advance!

标记 (3)
0 项奖励
回复
1 解答
1,441 次查看
Martin_
NXP Employee
NXP Employee

At IP level, ARP can be used to find out if a remote peer is connected to the network. Example is in dhcpclnt.c

c:\Freescale\Freescale_MQX_4_0\rtcs\source\tcpip\dhcpclnt.c

two functions DHCPCLNT_probe_address() and DHCPCLNT_verify_address().

First functions sends an ARP_request(). After some time delay you may check ARP table by ARP_is_complete().

ARP_request() and ARP_is_complete() can't be called from an application directly, because they are private functions to RTCS library, but they can be made public in the RTCS build project. Just create some public functions in RTCS library as wrappers around calls to ARP_request() and ARP_is_complete().

For TCP level please read:

https://community.freescale.com/message/317209

means return codes from send() and recv() functions may help.

在原帖中查看解决方案

0 项奖励
回复
3 回复数
1,441 次查看
DavidS
NXP Employee
NXP Employee


Hi Projektecarus,

The RTCSUG.pdf in MQX path (C:\Freescale\Freescale_MQX_4_0\doc\rtcs)  has the following that might help:

You can start the link status monitoring task (ipcfg_task_create()) to automatically rebind in case of Ethernet cable is re-attached. Another method to handle this monitoring is to call ipcfg_task_poll() periodically in an existing task.

Regards,

David

1,441 次查看
projektecarus
Contributor II

Hey David,

thanks for your reply. It is not exactly what I am looking for, though. Sorry for not being clear with the initial question, but what I need is a possibility to periodically check if a socket's remote counterpart is still existing and responding, no matter if it has been shutdown correctly, the cabel has been unplugged or something else happened. RTCS_ping achieves this, however, it is not an ideal solution as windows firewall blocks it by default, for example.

Ideally the server should do this whenever it sends a message. Check somehow if the remote socket received it.

Regards!

0 项奖励
回复
1,442 次查看
Martin_
NXP Employee
NXP Employee

At IP level, ARP can be used to find out if a remote peer is connected to the network. Example is in dhcpclnt.c

c:\Freescale\Freescale_MQX_4_0\rtcs\source\tcpip\dhcpclnt.c

two functions DHCPCLNT_probe_address() and DHCPCLNT_verify_address().

First functions sends an ARP_request(). After some time delay you may check ARP table by ARP_is_complete().

ARP_request() and ARP_is_complete() can't be called from an application directly, because they are private functions to RTCS library, but they can be made public in the RTCS build project. Just create some public functions in RTCS library as wrappers around calls to ARP_request() and ARP_is_complete().

For TCP level please read:

https://community.freescale.com/message/317209

means return codes from send() and recv() functions may help.

0 项奖励
回复