コード例 #1
0
        // This is the call back function, which will be invoked when a client is connected
        public void OnClientConnect(IAsyncResult asyn)
        {
            try
            {
                // Here we complete/end the BeginAccept() asynchronous call
                // by calling EndAccept() - which returns the reference to
                // a new Socket object
                Socket      clientSocket = ServerSocket.EndAccept(asyn);
                AsyncClient asyncClient  = new AsyncClient(clientSocket);
                Clients.Add(asyncClient);
                AsyncClientsDict.Add(clientSocket, asyncClient);

                ClientConnected(this, new ServerClientEventArgs(asyncClient));

                // Let the worker Socket do the further processing for the
                // just connected client
                WaitForData(clientSocket);
                // Now increment the client count

                // Since the main Socket is now free, it can go back and wait for
                // other clients who are attempting to connect
                ServerSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
            }
            catch (Exception ex)
            {
                ServerException(this, new ExceptionEventArgs(ex));
            }
        }
コード例 #2
0
        // This the call back function which will be invoked when the socket
        // detects any client writing of data on the stream
        public void OnDataReceived(IAsyncResult asyn)
        {
            SocketPacket socketData = (SocketPacket)asyn.AsyncState;

            try
            {
                // Complete the BeginReceive() asynchronous call by EndReceive() method
                // which will return the number of characters written to the stream
                // by the client
                int byteReceived = socketData.currentSocket.EndReceive(asyn);

                if (byteReceived > 0)
                {
                    string szData = Encoding.UTF8.GetString(socketData.dataBuffer, 0, byteReceived);

                    //Socket clientSocket = FindClientSocket(socketData.m_currentSocket);
                    Socket clientSocket = socketData.currentSocket;
                    if (clientSocket != null)
                    {
                        AsyncClient client = new AsyncClient(clientSocket);

                        MessageReceived(this, new ServerReceivedEventArgs(client, szData));
                    }
                }

                // Continue the waiting for data on the Socket
                WaitForData(socketData.currentSocket);
            }
            catch (Exception ex)
            {
                // Очистка списка и словаря клиентов.
                AsyncClient asyncClient = AsyncClientsDict[socketData.currentSocket];
                Clients.Remove(asyncClient);
                AsyncClientsDict.Remove(socketData.currentSocket);
                ServerException(this, new ExceptionEventArgs(ex));
            }
        }
コード例 #3
0
 public ServerClientEventArgs(AsyncClient client)
 {
     Client = client;
 }
コード例 #4
0
 public ServerReceivedEventArgs(AsyncClient client, string message)
 {
     Client  = client;
     Message = message;
 }