On server side I have this code which running in new thread
static void ListenForConsultant()
{
while (true)
{
var serverSocket = new TcpListener(IPAddress.Any, 2111);
serverSocket.Start();
var clientSocket = serverSocket.AcceptTcpClient();
consultantConnected = true;
Console.WriteLine(" >> Consultant Connected");
byte[] bytesFrom = new byte[10025];
while (true)
{
if (!clientSocket.Connected)
{
break;
}
NetworkStream networkStream = clientSocket.GetStream();
bytesFrom = new byte[10025];
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
var dataFromConsultant = System.Text.Encoding.ASCII.GetString(bytesFrom);
if (dataFromConsultant.IndexOf("~") != -1 && dataFromConsultant.IndexOf("^") != -1 && dataFromConsultant.IndexOf("^") > dataFromConsultant.IndexOf("~"))
{
var lengthOfMessage = dataFromConsultant.IndexOf("^") - dataFromConsultant.IndexOf("~") - 1;
dataFromConsultant = dataFromConsultant.Substring(dataFromConsultant.IndexOf("~") + 1, lengthOfMessage);
Console.WriteLine(" >> From consultant:" + dataFromConsultant);
}
}
consultantConnected = false;
Console.WriteLine(" >> Consultant Disconnected");
serverSocket.Stop();
}
}
I connect using putty to port 2111. All works ok, but when I close putty socket doesn’t close, however I have condition
if (!clientSocket.Connected)
{
break;
}
Debug shows me that clientSocket.Connected is true even after I disconnected from server.
Why does this happen?
The
tcpClient.Connectedproperty value is not reliable, it’s value depending on the last communication; so if the last communication were succeed then it’s value is true otherwise it is false. for more information on that check this.Use this IsConnected property to check if the tcpClient is connected:
Edit: Note that checking
IsConnectedwill not prevents it from disconnect after the check. i.e its possible to happen that the socket is disconnected just after it was connected “after theIsConnectedcheck evaluated and return true”, So you should wrap all communication intry/catchortry/catch/finallyblock expecting the socket to be disconnected at any time.