I write a comunication program with c/s mode in C# ,server and client program on different computer. Today after client progarm connected to server progarm ,then I pulled out cable from network adapter (after this , client and server program didn’t do anything), stranger thing happened. I found out socket of server program still keep connected status. and I use command “netstat -a -n” to retrieve network information ,and get information like following :
TCP 192.168.1.2:3645 192.168.1.3:1863 ESTABLISHED
192.168.1.2 (Server IP Address)
192.168.1.3 (Client IP Address)
Do anyone know the reason ? how to solve this problem .
I wanna how server program can receive the event and close the socket when network cable of client computer has been pulled out.
Please give me some advices or solutions.
Thanks
I think it can be solved by using TCP keepalive.
refrencehttp://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html
after connecting , set socket keepalive property . msdn say this switch default status is off , if set keepalive to on , socket will check the network status automaticly , and first check time is 2 hours after last operation on socket. but the time can be adjsut to short . then after first check , socket will servral times. if connection is down or died,
socket will throw exception .
C# Source:
I have checked. It’s running ok.