TcpClient / NetworkStream does not detect disconnect

I created a simple persistent socket connection for our game using TcpClientand NetworkStream. There are no problems connecting, sending messages and disconnecting normally (exiting the application / server is disabled / etc).

However, I have some problems when in some cases the client does not detect a disconnect from the server. The easiest way to check this is to pull out the network cable on Wi-Fi or set the phone to flight mode, but in the middle of the game what happened should be stable wifi.

Looking through documents for NetworkStream, etc., it is said that the only way to detect a trip is to try to write to the socket. Fairly enough, except that when I try, the recording goes as if nothing had happened. I can write some posts like this and everything seems fine. This is only when I reconnect the cable so that it sees that it is disconnected (are all messages buffered?).

TcpClientset to NoDelay, and there it Flush()is called after each record.

What I tried:

  • Writing a message NetworkStream- no joy
  • CanWrite, Connectedetc. all return true
  • TcpClient.Client.Poll( 1000, SelectMode.SelectWrite ); - returns true
  • TcpClient.Client.Poll( 1000, SelectMode.SelectRead ) && TcpClient.Client.Available == 0 - returns true
  • TcpClient.Client.Receive(buffer, SocketFlags.Peek) == 0- when connected, it blocks about 10-20 s, and then returns true. When the server is not blocked forever (?)
  • NetworkStream.Write() - does not give an error
  • NetworkStream.BeginWrite()- does not give an error (even when called EndWrite())
  • WriteTimeout -
  • , ( ), , , - .. ( 10-20 )

- ? NetworkStream (, ) , ?

keep-alive ( - , - , ), , NetworkStream -.

, ( , , , ).

Unity, .Net 2.0

+4
1

- wifi

. , . ? .

, ,

() . -... . - -.

- ?

, , , . .

+2

All Articles