Ejemplo n.º 1
0
 public static void Connect(IPEndPoint ip)
 {
     if (!CheckNet())
     {
         return;
     }
     if (Status != NetStatus.Null)
     {
         Debug.LogError("请先退出Net状态:" + Status);
         return;
     }
     if (info != null)
     {
         Debug.LogWarning("请勿重复连接tcp服务器");
     }
     else
     {
         socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         socket.Bind(new IPEndPoint(IP, Port));
         socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
         listen = new Thread(() =>
         {
             try
             {
                 Debug.Log("开始连接tcp服务器……");
                 socket.Connect(ip);
                 info             = new TcpInfo(socket);
                 info.CloseEvent += () =>
                 {
                     info       = null;
                     Net.Status = NetStatus.Null;
                     Debug.Log("断开tcp服务器");
                 };
                 info.Receive();
                 Net.Status = NetStatus.Client;
                 Debug.Log("成功连接tcp服务器:" + socket.RemoteEndPoint.ToString());
             }
             catch (Exception e)
             {
                 Debug.LogError("连接tcp服务器失败,提示:" + e.Message.ToString());
             }
         });
         listen.Start();
     }
 }
Ejemplo n.º 2
0
 public static void Listen(int listenNumber = 12)
 {
     if (!CheckNet())
     {
         return;
     }
     if (Status != NetStatus.Null)
     {
         Debug.LogError("请先退出Net状态:" + Status);
         return;
     }
     if (socket != null)
     {
         Debug.LogWarning("请勿重复创建tcp服务器");
     }
     try
     {
         socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         socket.Bind(new IPEndPoint(IP, Port));
         socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
         socket.Listen(listenNumber);
         ClientMax = listenNumber;
         listen    = new Thread(() =>
         {
             while (socket != null)
             {
                 Socket s      = socket.Accept();
                 IPEndPoint ip = (IPEndPoint)s.RemoteEndPoint;
                 if (Clients.ContainsIP(ip))
                 {
                     if (!clients.ContainsKey(ip))
                     {
                         TcpInfo n = new TcpInfo(s);
                         clients.Add(ip, n);
                         if (AddClientEvent != null)
                         {
                             AddClientEvent(Clients.GetClient(ip));
                         }
                         n.Receive();
                         n.CloseEvent += () =>
                         {
                             clients.Remove(ip);
                             if (Clients.ContainsIP(ip))
                             {
                                 if (RemoveClientEvent != null)
                                 {
                                     RemoveClientEvent(Clients.GetClient(ip));
                                 }
                                 Clients.Remove(ip);
                             }
                             Debug.Log(ip + "断开连接");
                         };
                         Debug.Log("新用户登入:" + ip);
                     }
                     else
                     {
                         s.Close();
                         Debug.Log("用户重复登入:" + ip);
                     }
                 }
                 else
                 {
                     s.Close();
                     string i = "";
                     foreach (var c in Clients)
                     {
                         i += c.PlayerName + " :" + c.IP + "\n";
                     }
                     Debug.Log("拒绝用户:" + ip + "\nClients:\n" + i);
                 }
             }
         });
         listen.Start();
         Net.Status = NetStatus.Server;
         Debug.Log("tcp服务器开启,开始监听:" + socket.LocalEndPoint);
     }
     catch (Exception e)
     {
         socket = null;
         Debug.LogError("创建tcp服务器失败,错误:" + e.Message.ToString());
     }
 }