Good day. I am writing a client TCP class. There is a problem closing the connection to the server. When trying to break the connection from the server side, the client simply does not react at all (although it worked until recently). When trying to break the connection from the client - the program hangs on the disconnect method (connection test cycle) The code for checking incoming data and connection status (separate stream):

void waitData() { try { while ( flagNeedDisconnect==false && verifyConnected())//flagNeedDisconnect - флаг необходимости отключиться { if (CLNT_STREAM.CanRead && CLIENT.Available>0) { byte[] data = new byte[CLIENT.ReceiveBufferSize]; CLNT_STREAM.Read(data, 0, data.Length); if (NewData != null) { TCPClientNewDataEventArgs e = new TCPClientNewDataEventArgs(data); object[] prms = { this, e }; if (formInvoke != null) formInvoke.Invoke(NewData, prms); else NewData(this, e); } } } } catch { } closeSocket(); } 

Connection verification code:

 bool verifyConnected() { try { if (isConnected && CLIENT.Connected)//isConnected - флаг устанавливаемый вручную { if ((CLIENT.Client.Poll(0, SelectMode.SelectWrite)) && (!CLIENT.Client.Poll(0, SelectMode.SelectError))) { byte[] buffer = new byte[1]; if (CLIENT.Client.Receive(buffer, SocketFlags.Peek) == 0) { return false; } else { return true; } } else { return false; } } else { return false; } } catch (Exception e) { resetConnect(true,e.ToString()); return false; } } 

Disconnect Method Code

 public void disconnect() { flagNeedDisconnect = true; CLIENT.Close(); while (CLIENT != null) { } flagNeedDisconnect = false; } public void closeSocket() { int result = 0; if (CLNT_STREAM != null) { CLNT_STREAM.Close(); CLNT_STREAM = null; result++; } if (CLIENT != null) { //CLIENT.Client.Disconnect(true); CLIENT.Close(); CLIENT = null; result++; } resetConnect(false, "close socket"); } 

I am assuming that the problem lies in the flow of waiting for incoming data. The logic here is such that when the flagNeedDisconnect flag is flagNeedDisconnect to true or the connection is lost, the loop will end and the socket will close. But the following happens - I try to disconnect from the client side, the server starts to eat 100 percent CPU (a third-party library is used for the server, this happens when the client disconnects, but the server does not respond to it), and the client itself freezes for a long time. add CLIENT = null; , then the thread will respond only in time, and the test connection will throw the expected exception.

  • I can guess what to do through the stream. - And
  • Disconnect through the stream? I assign him a flag that he reads. Just the fact is that now it hangs. - Matvey
  • Why is getting data so hard to write? Why it was impossible to just call Receive ? - Pavel Mayorov
  • I am learning c # for the third day. I do not know about this approach. - Matvey
  • one
    3 day, why did you immediately rush to write server applications without studying how it works? I advise you to study multi-threaded applications, as well as to study cryptographic algorithms like RSA. - And

0