In my program i'm creating socket and whes client connects i'm creating new thread and trying to read and write from/to connected to this socket, but alway get an Read Error because host-computer closed connection for the second command, first command from client worfs fine, and third works fine. i try to check if clientSocket is connected and if networkStream is readable and they alway returns true. how can i solve this? and how can it be - networkStream.CanRead returns true but networkStreamRead raises error? I am new to C# and socket programming so sorry for dumb question.
clientSocket
is an TcpClient
networkStream
is an NetworkStream
if (clientSocket.Connected == true)
{
requestCount = requestCount + 1;
networkStream = clientSocket.GetStream();
if (networkStream.CanRead == true)
{
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
}
}