I have written a TCP Server, in which number of bytes to read is prefixed in two-bytes header. After reading from the stream and sending the response back to the client, both NetworkStream and TcpClient are disposed. The problem is that the client doesn't seem to receive my response unless I uncomment Thread.Sleep() line. Here is the code:
using (var tcpClient = await tcpServer.AcceptTcpClientAsync())
{
tcpClient.NoDelay = true;
using (var stream = tcpClient.GetStream())
{
var twoBytesHeader = new TwoByteHeader();
var headerBuffer = new byte[twoBytesHeader.HeaderLength];
using (var binaryReader = new BinaryReader(stream, Encoding.ASCII, true))
{
headerBuffer = binaryReader.ReadBytes(twoBytesHeader.HeaderLength);
int newOffset;
var msgLength = twoBytesHeader.GetMessageLength(headerBuffer, 0, out newOffset);
var buffer = binaryReader.ReadBytes(msgLength);
string msgASCII = Encoding.ASCII.GetString(buffer);
var bufferToSend = await ProcessMessage(msgASCII);
using (var binaryWriter = new BinaryWriter(stream, Encoding.ASCII, true))
{
binaryWriter.Write(bufferToSend);
binaryWriter.Flush();
}
//Thread.Sleep(1000);
}
}
}
When Sleep is uncommented, the client receives response and then indicates that client has disconected. I can't figure out the reason of this behaviour