Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); byte[] buffer = new byte[1024]; socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, OnReceiveCallback, socket);
private void OnReceiveCallback(IAsyncResult ar) { Socket socket = (Socket)ar.AsyncState; int bytesReceived = socket.EndReceive(ar); byte[] buffer = new byte[bytesReceived]; Array.Copy(buffer, buffer.Length, buffer); // process the received data }This example is the callback method specified in Example 1. It uses the EndReceive method to retrieve the number of bytes received and the data itself, which is then processed as necessary. Package library: This method is part of the System.Net.Sockets namespace in the .NET Framework.