示例#1
0
        public static void SendData(IAsyncResult result)
        {
            Console.WriteLine("Data Sending Thread : "
                              + Thread.CurrentThread.GetHashCode());
            AsyncStateInfo stateInfo = result.AsyncState as AsyncStateInfo;

            //Completes asynchronous send
            stateInfo.socket.EndSend(result);
        }
示例#2
0
        //Method invoked to accept an incoming connection attempt
        public static void AcceptConnection(IAsyncResult result)
        {
            Console.WriteLine("Connection Request Thread : " + Thread.CurrentThread.GetHashCode());
            AsyncStateInfo stateInfo = result.AsyncState as AsyncStateInfo;
            //Accepts client connection
            Socket mdcSocket = stateInfo.socket.EndAccept(result);

            //Starts listening to client connection
            stateInfo.socket.BeginAccept(new AsyncCallback(AcceptConnection),
                                         new AsyncStateInfo(stateInfo.socket));
            AsyncStateInfo mdcStateInfo = new AsyncStateInfo(mdcSocket);
            string         mktPrice     = "MSFT;25,IBM;24";

            byte[] dataBuf = Encoding.ASCII.GetBytes(mktPrice);

            //copy data buffer
            Buffer.BlockCopy(dataBuf, 0, mdcStateInfo.dataBuffer, 0, dataBuf.Length);

            //Sends data asynchronously
            mdcSocket.BeginSend(mdcStateInfo.dataBuffer, 0, 512, SocketFlags.None,
                                new AsyncCallback(SendData), mdcStateInfo);
        }