Beispiel #1
0
 public SocketData(Client client)
 {
     this.client = client;
 }
Beispiel #2
0
 public static void removeClient(Client client, List<Client>clientList)
 {
     clientList.Remove(client);
 }
Beispiel #3
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);
        }
Beispiel #4
0
        private void SendList(Client client)
        {
            Socket clientSock = client.socket;

            XElement xml = new XElement("CLIENTLIST");

            foreach (Client c in clientList)
            {
                xml.Add(new XElement("CLIENT",
                    new XElement("IP", c.socket.RemoteEndPoint.ToString())));
            }

            byte[] xmlData = UTF8.GetBytes(xml.ToString());
            StreamWriter.Write(UTF8.GetBytes(NET_LIST));
            StreamWriter.Write(xmlData.Length);
            StreamWriter.Write(xmlData);

            SocketAsyncEventArgs args = new SocketAsyncEventArgs();
            args.SetBuffer(UtilStream.ToArray(), 0, UtilStreamLength);
            clientSock.SendAsync(args);

            resetSW();
        }
Beispiel #5
0
        public void disconnectClient(Client client, bool remove = false)
        {
            client.socket.Disconnect(false);
            client.socket.Shutdown(SocketShutdown.Both);
            string clientInfo = client.socket.RemoteEndPoint.ToString();
            client.socket.Close();
            client.socket.Dispose();
            Console.WriteLine($"클라이언트 {clientInfo} 연결 끊김");

            if (remove)
                removeClient(client, clientList);
        }