Beispiel #1
0
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            Interlocked.Increment(ref m_numConnectedSockets); //增加连接计数
            Console.WriteLine("Client connection accepted. There are {0} clients connected to the server",
                m_numConnectedSockets);

            // Get the socket for the accepted client connection and put it into the
            //ReadEventArg object user token
            SocketAsyncEventArgs readEventArgs = m_readWritePool.Pop();
            ((AsyncUserToken)readEventArgs.UserToken).Socket = e.AcceptSocket; //设置UserToken变量

            // As soon as the client is connected, post a receive to the connection
            bool willRaiseEvent = e.AcceptSocket.ReceiveAsync(readEventArgs);
            if (!willRaiseEvent)
            {
                ProcessReceive(readEventArgs);
            }

            //分配写事件参数
            //SocketAsyncEventArgs writeEventArgs = m_readWritePool.Pop();
            //((AsyncUserToken)writeEventArgs.UserToken).Socket = e.AcceptSocket;
            //willRaiseEvent = e.AcceptSocket.SendAsync(writeEventArgs);
            //if (!willRaiseEvent)
            //{
            //    ProcessSend(writeEventArgs);
            //}

            // Accept the next connection request
            AsyncUserToken UserToken = new AsyncUserToken();
            UserToken.Socket = e.AcceptSocket;
            UserToken.Message = "天下风云出我辈, 一入江湖岁月催;";
            StartSend(UserToken);

            StartAccept(e);
        }
Beispiel #2
0
        private void ProcessConnect(SocketAsyncEventArgs e)
        {
            if (e.ConnectSocket== null)
            {
                Console.WriteLine("连接客户端失败 {0}",e.SocketError);
                return;
            }

            SocketAsyncEventArgs readEventArgs = m_readWritePool.Pop();
            ((AsyncUserToken)readEventArgs.UserToken).Socket = e.ConnectSocket; //设置UserToken变量

            // As soon as the client is connected, post a receive to the connection
            bool willRaiseEvent = e.ConnectSocket.ReceiveAsync(readEventArgs);
            if (!willRaiseEvent)
            {
                ProcessReceive(readEventArgs);
            }
            AsyncUserToken userToken = new AsyncUserToken();
            userToken.Socket = e.ConnectSocket;
            userToken.Message = "我是客户端,服务器你好!";
            StartSend(userToken);
        }
Beispiel #3
0
        //开始发送消息
        public void StartSend(AsyncUserToken userToken)
        {
            SocketAsyncEventArgs writeEventArgs = m_readWritePool.Pop();
            writeEventArgs.UserToken = userToken;

            byte[] byteArray = Encoding.Default.GetBytes(userToken.Message);
            Buffer.BlockCopy(byteArray, 0, writeEventArgs.Buffer, writeEventArgs.Offset, byteArray.Count());

            bool willRaiseEvent = userToken.Socket.SendAsync(writeEventArgs);
            if (!willRaiseEvent)
            {
                ProcessSend(writeEventArgs);
            }
        }