Detect connection lost using tcpip class ?!
3 次查看(过去 30 天)
显示 更早的评论
Hello everybody,
within my project I am using the tcpip class (networkrole = server) to communicate with another device over the network. However, is there anyway to detect in matlab when the connection of the client is lost ? As far as I know, there are no "Events/Callbacka" for this. Is there any workarround ?
Thanks !
Best regards,
tugrul
2 个评论
Mohammad Sami
2020-9-27
编辑:Walter Roberson
2020-9-27
You can try setting the ErrorOccurredFcn callback for tcpclient. https://www.mathworks.com/help/instrument/tcpclient.html#mw_5a853e8e-509f-4e25-9ae1-39d15735f1c3
采纳的回答
Walter Roberson
2020-9-27
编辑:Walter Roberson
2020-9-27
Mohammad Sami's suggestion is probably about the best you are going to be able to get.
The original TCP architecture does not have any way of detecting that a connection is lost, because the original architecture was designed to survive network outages, delivering the packet as soon as practical after the connectivity resumed. There are state variables involved, but the original model was either that it was not possible for the endpoints to go down or else the original model was that the endpoints would preserve all connection state when they went down and would resume it afterwards.
The original TCP archicture cannot distinguish between "I sent a packet to the other end and the packet got lost and I do not know it was lost and the other end does not know it was expecting a packet", compared to "I sent a packet to the other end which received it, but the other end has not chosen to reply yet." Accurately synchronizing state so that both sides are not only sure that a packet went through, but also that they are sure that the other end knows for sure that the packet went through, turns out to be a very hard problem in asychronous networks.
TCP also does not natively provide notification that the connection closed down smoothly, so unless you are acting as the TCP driver yourself, the way to detect that a connection has closed is to attempt an I/O operation and get told by the driver that the connection is closed.
But notice that that is about the connection being closed: the original architecture cannot tell whether the connection is "down" or if the other end just doesn't have anything to say to you. What does it mean for a connection to be "down" anyhow, when the entire IP architecture expects that there are multiple paths and expects that not all routes are available all the time? In a sufficiently connected network with appropriate routing protocols, you can turn off one of the intermediate routers and the network will route around the problem, finding the next best way to get packets to the destination. The loss of one router along the way does not mean that the connection is down; the loss of one packet along the way is defined as being acceptable, part of the architecture, routers are allowed to deliberately drop packets if they cannot keep up with the load. As far as TCP is concerned, the only sense in which a connection could be said to be "down" is if the endpoints go down (though in practice anything between the endpoints and the first redundant route is point of failure as well.)
Later add-ons to TCP added "keep-alives", which are periodic messages exchanged with no data; a pair of keep-alives exchanged between the endpoints gives information that the link is still alive. The convention is then that if too many of your keep-alives in a row go un-answered within a reasonable time, that you unilaterally declare the connection to be dead and attempt to close the connection; user I/O attempts would receive an error in this situation.
So... as a MATLAB programmer, you cannot get notified that a connection has gone down: all you can do is attempt an I/O operation and get told there was an error. You can then check the state of the connection to see if it indicates the connection is closed; if so then the link has been abandoned (rather than the other end simply being busy, for example.)
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!