protected void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
AsyncObject state = (AsyncObject)ar.AsyncState;
Socket handlersocket = state.Socket;
try
{
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
state.Received.AddRange(state.Buffer);
content = B2S(state.Buffer);
if ((content.Length > 0) && content.Contains(ENDKOMMANDO))
{
HandleMessages(state, state.Received);
}
else
{
// Nicht alle Daten wurden empfangen
handlersocket.BeginReceive(state.Buffer, 0, AsyncObject.BufferSize, 0, new AsyncCallback(this.ReadCallback), state);
}
}
else
{
// Disconnected
}
}
catch (System.Net.Sockets.SocketException es)
{
if (es.ErrorCode != 64)
{
Console.WriteLine(string.Format("Socket
Exception: {0}, {1}.", es.ErrorCode, es.ToString()));
}
}
catch (
Exception e)
{
if (e.GetType().FullName != "System.ObjectDisposedException")
{
Console.WriteLine(string.Format("
Exception: {0}.", e.ToString()));
}
}
}