Writing a client / server program and previously was using calls like this:
TcpListener.AcceptTcpClient() NetworkStream.Read() NetworkStream.Write()
TcpListener.AcceptTcpClient() is working fine because its callback function is only called after a connection is made.
However, BeginRead() is not working as well. For some reason it executes and continues past it even when no data is sent.. Not sure what is going on. NetworkStream.Read() was blocking just fine but this isn’t..
Here is the non-working thread that is created whenever a connection is established:
private void HandleClientCommunication(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
try
{
byte[] header = new byte[4];
clientStream.BeginRead(header, 0, header.Length, Read, new StateData(clientStream, header)); // Runs even if no data is sent
int dataLength = BitConverter.ToInt32(header, 0); // Continues to this, even if no packets are sent..
byte[] data = new byte[dataLength]; // dataLength is 0 still of course because none of the code is blocking
clientStream.BeginRead(data, 0, dataLength, Read, new StateData(clientStream, data));
}
catch (IOException e)
{
Console.WriteLine(e.InnerException.Message);
}
// not relevant past here
}
Here’s the Read method too. Not sure if it’s needed to figure out the problem but I’ll include it regardless:
private void Read(IAsyncResult async)
{
StateData stateData = (StateData)async.AsyncState;
stateData.BytesRead += stateData.Stream.EndRead(async);
if (stateData.BytesRead < stateData.Bytes.Length)
{
stateData.Stream.BeginRead(stateData.Bytes, 0, stateData.Bytes.Length, Read, stateData);
}
else
{
string message = Encoding.ASCII.GetString(stateData.Bytes);
Console.WriteLine(message);
}
}
So yeah I’m just trying to set up a client server interaction with asynchronous calls and have every “packet” include a header with the length of the data as an int and then right after that the data itself. Unfortunately, things seem to not be working and I’m not sure what the problem is.
Seems like I have to include some manual form of blocking but doesn’t that defeat the purpose of using asynchronous calls? Why not just go back to regular synchronous blocking calls like Read() and AcceptTcpClient() at that point then?
I don’t think I am fully understanding things here..
I think you’ve misunderstood what
BeginReadis meant to do. It’s not meant to block – it issues an asynchronous read, executing the given callback when the read has completed. It’s meant to return immediately – so it’s no wonder your header data is empty.Basically, any work which requires the data should be in the callback – not in the same method which calls
BeginRead. Your first callback should convert the header data and thenBeginReadagain with a different callback to read the message data itself… although you should also bear in mind that anyRead/BeginReadcall may not read all the data you’ve asked for. You’re trying to account for this in your callback, but you’re not quite there – because you’re always trying to fill the buffer from the start – which will overwrite any existing data. If you know how much data you’re expecting overall and your buffer is that long, you should read into the buffer starting at how many bytes you’ve already read.Note that C# 5 will make all of this a lot simpler – but if you can’t wait until then, you may well find it easier to go back to blocking calls on multiple threads (one per client).