Exemplo n.º 1
0
        private static void asyncReceiveCallback(IAsyncResult asyncResult)
        {
            ASyncStateData rcvData = asyncResult.AsyncState as ASyncStateData;
            int            nRecv   = rcvData.Socket.EndReceive(asyncResult);
            string         txt     = Encoding.UTF8.GetString(rcvData.Buffer, 0, nRecv);

            byte[] sendBytes = Encoding.UTF8.GetBytes("Hello: " + txt);
            rcvData.Socket.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, asyncSendCallback, rcvData.Socket);
        }
Exemplo n.º 2
0
        private static void serverFunc(object obj)
        {
            using (Socket srvSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 11200);
                srvSocket.Bind(endPoint);
                srvSocket.Listen(10);

                while (true)
                {
                    Socket         clntSocket = srvSocket.Accept();
                    ASyncStateData data       = new ASyncStateData();
                    data.Buffer = new byte[1024];
                    data.Socket = clntSocket;

                    clntSocket.BeginReceive(data.Buffer, 0, data.Buffer.Length, SocketFlags.None, asyncReceiveCallback, data);
                }
            }
        }