Esempio n. 1
0
        private static void asyncReceiveCallback(IAsyncResult _async)
        {
            AsyncStateData data     = _async.AsyncState as AsyncStateData;
            int            nRecv    = data.socket.EndReceive(_async);
            string         contents = Encoding.UTF8.GetString(data.buffer, 0, nRecv);

            byte[] sendBytes = Encoding.UTF8.GetBytes("SEND:" + contents);
            data.socket.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, asyncSendCallback, data.socket);
        }
Esempio n. 2
0
        private static void Server(object _obj)
        {
            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, NetBase.Constant.TCP_PORT);
                socket.Bind(endPoint);
                socket.Listen(10);


                while (true)
                {
                    Socket         clientSocket = socket.Accept();
                    AsyncStateData data         = new AsyncStateData();
                    data.buffer = new byte[NetBase.Constant.BUFFER];
                    data.socket = clientSocket;

                    clientSocket.BeginReceive(data.buffer, 0, data.buffer.Length, SocketFlags.None, asyncReceiveCallback, data);
                }
            }
        }