Ejemplo n.º 1
0
        private void Accept_Completed(object sender, SocketAsyncEventArgs e)
        {
            Socket _client = e.AcceptSocket;

            Message packet = new Message();
            SocketAsyncEventArgs _receiveArgs = new SocketAsyncEventArgs();
            _receiveArgs.UserToken = packet;
            _receiveArgs.SetBuffer(packet.GetBuffer(), 0, 4);
            _receiveArgs.Completed += new EventHandler<SocketAsyncEventArgs>(Recieve_Completed);
            _client.ReceiveAsync(_receiveArgs);
            clientSet.Add(_client, new User("", ClientState.ONACCESS));

            IPEndPoint clientIP = _client.RemoteEndPoint as IPEndPoint;
            Console.WriteLine("{0} is connected", clientIP);

            Socket _server = (Socket)sender;
            e.AcceptSocket = null;
            _server.AcceptAsync(e);
        }
Ejemplo n.º 2
0
        public void SendDataToClient(byte type, byte[] data, Socket client)
        {
            //data += Encoding.Unicode.GetBytes("s");
            Message packet = new Message();
            if (!client.Connected)
            {
                Console.WriteLine(client.Handle + " is disconnected");
                DisconnectProc(client);
            }
            else
            {
                //byte[] compressedData = CompressToBytes(data);
                packet.InitSendPacket(type, data);
                SocketAsyncEventArgs _sendArgs = new SocketAsyncEventArgs();
                _sendArgs.SetBuffer(BitConverter.GetBytes(packet.Length), 0, 4);
                _sendArgs.Completed += new EventHandler<SocketAsyncEventArgs>(Send_Completed);
                _sendArgs.UserToken = packet;

                client.SendAsync(_sendArgs);
            }
        }