Exemplo n.º 1
0
        private void Receive(object iar)
        {
            lock (lockObject)
            {
                Client cli = (Client)((IAsyncResult)iar).AsyncState;
                int    rev = 0;
                try
                {
                    rev = cli.sock.EndReceive(((IAsyncResult)iar));
                }
                catch { }
                if (rev != 0)
                {
                    try
                    {
                        Data d = Data.Deserialize(cli.buffer);
                        switch (d.Type)
                        {
                        case DataType.NONE:
                            Console.WriteLine(cli.IP.ToString() + "(" + d.Info.NickName + ")" + "님이 접속하셨습니다.");
                            Data _d = new Data(DataType.NONE, cli.IP, new ClientInfo(cli.IP, d.Info.NickName));
                            SocketAsyncEventArgs _args = new SocketAsyncEventArgs();

                            Send(cli, _d);
                            foreach (Client c in clients)
                            {
                                Data data = new Data(DataType.INFO, d.Info.NickName + "님이 접속하셨습니다.", new ClientInfo("", ""));
                                Send(c, data);
                            }
                            break;

                        case DataType.STRING:
                            Console.WriteLine(cli.IP.ToString() + "(" + d.Info.NickName + ")" + " : " + d.InnerData.ToString());
                            foreach (Client c in clients)
                            {
                                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                                args.SetBuffer(cli.buffer, 0, 1024);
                                args.Completed += Args_Completed;
                                args.UserToken  = c;
                                c.sock.SendAsync(args);
                            }
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Console.WriteLine("개체 " + ex.Source + " 에서, 메서드 " + ex.TargetSite + "에서");
                    }
                    finally
                    {
                        cli.buffer = new byte[1024];
                        try
                        {
                            cli.sock.BeginReceive(cli.buffer, 0, cli.buffer.Length, SocketFlags.None, new AsyncCallback(Recevie_Callback), cli);
                        }
                        catch { }
                    }
                }
                else
                {
                    try
                    {
                        Console.WriteLine(cli.IP.ToString() + "(" + cli.NickName + ")" + "님이 접속을 종료하셨습니다.");
                        //RemoveEvent(so);
                        clients.Remove(cli);
                        cli.sock.Close();

                        foreach (Client c in clients)
                        {
                            Data data = new Data(DataType.INFO, cli.NickName + "님이 접속을 종료하셨습니다.", new ClientInfo("", ""));
                            Send(c, data);
                        }
                    }
                    catch { }
                }
            }
        }