Saturday, 31 August 2013

c# asynchronous server with beginReceive

c# asynchronous server with beginReceive

Hi stack overflow members. I'm struggling with some simple code but I
can't get it done. I have this asynchronous server which waits for
connections.
while (clientSocket.Connected)
{
try
{
clientSocket.BeginReceive(so.buffer, 0, 200, SocketFlags.None
, new AsyncCallback(wait),so);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
where so(shorted StateObject) it's my class:
internal class StateObject
{
public TcpClient client;
public byte[] buffer;
public StateObject()
{
buffer = new byte[200];
client = new TcpClient();
}
}
I use this class to put out the information on the callback function.
However I get the system lacked sufficient buffer space or because a queue
was full. I posted a short piece from the actual program. One interesting
issue, is that if I write:
while (clientSocket.Connected)
{
try
{
byte[] buffer = new byte[200];
clientSocket.BeginReceive(buffer, 0, 200, SocketFlags.None
, new AsyncCallback(wait),so);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
it will work, but I will not be able to pull out the buffer from the
asynchronous function(wait). I'm struggling with this and I can't find
answers.

No comments:

Post a Comment