tags:

views:

33

answers:

0

I have a serious problem with the asynchronous Receive method of System.Net.Sockets.Socket.

Here is the code I use to open the connection:

_socket = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
_socket.Connect(endpoint);

byte[] buffer = new byte[1024];
_socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, buffer);

and here is the callback method:

private void ReceiveCallback(IAsyncResult result)
{
     byte[] buffer = (byte[])result.AsyncState;
     int count = _socket.EndReceive(result);

     if (count > 0)
     {
          // Do something
     }

     buffer = new byte[1024];
     _socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, buffer);
}

The problem is that the ReceiveCallback is never being called, despite the socket being connected.

Can anyone help me out on this one?