Ejemplo n.º 1
0
        static void connected(object sender, SocketAsyncEventArgs args)
        {
            Socket client = sender as Socket;
            if (args.SocketError == SocketError.Success)
            {
                Console.WriteLine("접속됨.");

                MemoryStream stream = new MemoryStream();
                BinaryWriter writer = new BinaryWriter(stream);

                writer.Write(UTF8.GetBytes(NET_AUTH));
                writer.Write(UTF8.GetBytes(NET_AUTH_TOKEN).Length);
                writer.Write(UTF8.GetBytes(NET_AUTH_TOKEN));

                client.Send(stream.ToArray());

                SocketData data = new SocketData();
                data.initData(SETTING_DATA_SIZE);

                SocketAsyncEventArgs recvArgs = new SocketAsyncEventArgs();
                recvArgs.UserToken = data;
                recvArgs.SetBuffer(data.data, 0, SETTING_DATA_SIZE);
                recvArgs.Completed += new EventHandler<SocketAsyncEventArgs>(receiveCompleted);

                client.ReceiveAsync(recvArgs);
            }
            else
                Console.WriteLine(args.SocketError.ToString());
        }
Ejemplo n.º 2
0
        private void receiveCompleted(object sender, SocketAsyncEventArgs args)
        {
            Socket clientSock = sender as Socket;
            SocketData data = args.UserToken as SocketData;
            Client client = data.client;

            if (client == null)
            {
                return;
            }

            if (args.SocketError == SocketError.ConnectionReset)
            {
                disconnectClient(client, true);
            }

            client.lastConn = DateTime.Now;

            if (clientSock.Connected && args.BytesTransferred > 0)
            {
                data.data = args.Buffer;

                if (!client.authed)
                {
                    if (data.code == NET_AUTH)
                    {
                        int len = BitConverter.ToInt32(data.data, 8);
                        string token = UTF8.GetString(data.data, 12, len);

                        StreamWriter.Write(UTF8.GetBytes(NET_AUTH));

                        if (token == NET_AUTH_TOKEN)
                        {
                            client.authed = true;
                            Console.WriteLine($"클라이언트 {clientSock.RemoteEndPoint.ToString()} 인증됨.");
                            StreamWriter.Write(0x00000001);
                        }
                        else
                        {
                            Console.WriteLine($"클라이언트 {clientSock.RemoteEndPoint.ToString()} 인증 실패");
                            StreamWriter.Write(0xFFFFFFFF);
                            disconnectClient(client);
                        }

                        SocketAsyncEventArgs sendArgs = new SocketAsyncEventArgs();
                        sendArgs.SetBuffer(UtilStream.ToArray(), 0, UtilStreamLength);

                        clientSock.SendAsync(sendArgs);

                        resetSW();
                    }
                }
                else
                {
                    switch (data.code)
                    {
                        case NET_LIST:
                            {
                                SendList(client);
                                break;
                            }
                    }
                }

                data = new SocketData();
                data.client = client;
                data.initData(SETTING_DATA_SIZE);

                args.UserToken = data;
                args.SetBuffer(data.data, 0, SETTING_DATA_SIZE);

                clientSock.ReceiveAsync(args);
            }
        }
Ejemplo n.º 3
0
        static void receiveCompleted(object sender, SocketAsyncEventArgs args)
        {
            Socket client = sender as Socket;
            SocketData data = args.UserToken as SocketData;

            data.data = args.Buffer;

            if (client == null)
                return;

            if (client.Connected && args.BytesTransferred > 0)
            {
                SocketAsyncEventArgs sendArgs = new SocketAsyncEventArgs();

                switch (data.code)
                {
                    case NET_ALIVE:
                        {
                            sendArgs.SetBuffer(UTF8.GetBytes(NET_ALIVE), 0, 8);
                            client.SendAsync(sendArgs);
                            break;
                        }
                    case NET_AUTH:
                        {
                            int state = BitConverter.ToInt32(data.data, 8);

                            if (state == 1)
                            {
                                Console.WriteLine("인증 성공!");
                                authed = true;
                            }
                            else
                            {
                                Console.WriteLine("인증 실패.");
                                authed = false;
                            }

                            break;
                        }
                    case NET_LIST:
                        {
                            int len = BitConverter.ToInt32(data.data, 8);
                            XElement xml = XElement.Parse(UTF8.GetString(data.data, 12, len));

                            Console.WriteLine(xml.ToString());
                            break;
                        }
                }
            }

            data = new SocketData();
            data.initData(SETTING_DATA_SIZE);

            args.UserToken = data;
            args.SetBuffer(data.data, 0, SETTING_DATA_SIZE);

            client.ReceiveAsync(args);
        }
Ejemplo n.º 4
0
        private void acceptSocket(object sender, SocketAsyncEventArgs args)
        {
            if (args.SocketError != SocketError.Success)
            {
                return;
            }

            Socket server = sender as Socket;
            Socket clientSock = args.AcceptSocket;

            if (clientSock != null && clientSock is Socket)
            {
                Console.WriteLine($"클라이언트 {clientSock.RemoteEndPoint.ToString()} 접속");
                Client client = new Client();
                client.socket = clientSock;
                client.lastConn = DateTime.Now;

                SocketAsyncEventArgs recvArgs = new SocketAsyncEventArgs();

                SocketData data = new SocketData(client);

                data.initData(SETTING_DATA_SIZE);

                recvArgs.UserToken = data;
                recvArgs.SetBuffer(data.data, 0, SETTING_DATA_SIZE);
                recvArgs.Completed += new EventHandler<SocketAsyncEventArgs>(receiveCompleted);

                clientSock.ReceiveAsync(recvArgs);

                clientList.Add(client);
            }

            args.AcceptSocket = null;
            server.AcceptAsync(args);
        }