コード例 #1
0
        private void Send(Client client, String data)
        {
            if (data == null)
                return;

            var byteData = Encoding.ASCII.GetBytes(data);
            try
            {
                client.socket.BeginSend(byteData, 0, byteData.Length, 0,
                                        new AsyncCallback(SendCallback), client);
            }
            catch (Exception)
            {
                ClientRemove(client);
            }
        }
コード例 #2
0
        private void ClientRemove(Client client)
        {
            lock (lockClients)
                clients.Remove(client);

            try
            {
                client.socket.Shutdown(SocketShutdown.Both);
                client.socket.Close();
            }
            catch (SocketException) { }
            catch (ObjectDisposedException) { }
        }
コード例 #3
0
        private void ConnectCallback(IAsyncResult ar)
        {
            _signal.Set();

            try
            {

                Socket socketServer = (Socket)ar.AsyncState;
                Socket socketClient = socketServer.EndAccept(ar);
                Client client = new Client(socketClient);

                if (clients.Count < MAX_CLIENTS)
                    ClientAdd(client);
                else
                    ClientRemove(client);
            }
            catch (ObjectDisposedException)
            {
            }
        }
コード例 #4
0
        private void ClientAdd(Client client)
        {
            lock (lockClients)
                clients.Add(client);

            Send(client, _parser.Motd());
            try
            {
                client.socket.BeginReceive(client.buffer, 0, Client.BUFFER_SIZE, 0,
                                           new AsyncCallback(ReadCallback),
                                           client);
            }
            catch (Exception)
            {
                ClientRemove(client);
            }
        }