'TCPClient Reconnect to Local Server
I am using TcpClient to talk to a device that connects directly to the computer's Ethernet connector via a crossover cable. I connect to the server in the device by calling TcpClient.ConnectAsync and I read data from the device using TcpClient.GetStream().ReadAsync(). It all works well.
Occasionally the device loses power and I need to reconnect. As soon as the device loses power I get an IOException which I catch and in response I call TcpClient.Close(). I then try to reconnect by creating a new instance of TcpClient and calling TcpClient.ConnectAsync again. The connection attempt always times out. I have to restart the program and then I'm able to connect.
The device also has DHCP. When I connect it to my LAN and use the exact same code, it works. I am able to reconnect after detecting a power loss.
My question is why is this happening and what can be done to fix it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|