When running this code:
private async void StartChat(Object obj)
{
TcpClient me = (TcpClient)obj;
UpdateChatBox("Attempting read from server.");
myBuffer = new byte[BUFFER_SIZE];
while (true)
{
var myStream = me.GetStream();
myStream.BeginRead(myBuffer, 0, BUFFER_SIZE, new AsyncCallback(UpdateChatBoxAsync), myStream);
if (messageToSend)
{
await myStream.WriteAsync(myMessage, 0, myMessage.Length);
}
}
}
I get the following IO exception from BeginRead:
Unable to read data from the transport connection: the operation on the socket cannot be performed because the system does not have enough space for the buffer or because the queue is full.
Here is the callback method for BeginRead:
private void UpdateChatBoxAsync(IAsyncResult result)
{
var stream = result.AsyncState as NetworkStream;
int bytesRead = stream.EndRead(result);
if (bytesRead > 0)
{
String newMessage = NetworkUtils.GetString(myBuffer);
UpdateChatBox(newMessage);
}
}
Can someone shed some light on why this exception occurs? I tried to recreate the buffer every time at the beginning of the while loop, but until this improved the exception, I would not receive messages from the server.
I also tried resetting myBuffer to an empty array at the end of UpdateChatBoxAsync, this didn't work either.
Any help would be appreciated.
Tgreg source
share