C# TCPClient - Send and Receive Overlapping
I am working on a client-server application.
I use TCPClient for both server and client.
Server listens and responds and the client will send and receive. Each
respond from the server contains different message length.
The client has 2 request. One is ping request and the other is sending
data request.
I created a thread to ping the server every 10 seconds.
The data request is send by the client when a user presses the send button.
The server responds to the ping request by sending 2 bytes back to client.
The server responds to the data request by sending back 8 bytes.
Here's the problem that I am running into.
The ping thread would at times send the request to the server at the same
time that a user presses the send button.
Now the data request method is reading from the socket and expecting 8
bytes but only getting 2 bytes thus hang for a long time.
here's the snippet from data request method:
byteBuffer = new byte[8];
remainingBytes = byteBuffer.Length;
numberOfBytesRead = netStream.Read(byteBuffer, index, remainingBytes)
here's the snippet from ping thread:
byteBuffer = new byte[2];
remainingBytes = byteBuffer.Length;
numberOfBytesRead = netStream.Read(byteBuffer, index, remainingBytes)
What can I do so that the client knows which response goes to which method?
Thanks
No comments:
Post a Comment