Exemplo n.º 1
0
 public void StartClient(IPEndPoint ipEndPoint)
 {
     DetailLog.InitSettings();
     try
     {
         socket.BeginConnect(ipEndPoint, ServerConnectCallBack, socket);
         DetailLog.ColorLog(LogColor.Blue, "Client Start Success!\nConnecting To Server......");
     }
     catch (Exception e)
     {
         DetailLog.Error("StartClient:" + e.Message);
     }
 }
Exemplo n.º 2
0
        public void StartServer(IPEndPoint ipEndPoint)
        {
            DetailLog.InitSettings();
            try
            {
                socket.Bind(ipEndPoint);
                socket.Listen(50000);
                DetailLog.ColorLog(LogColor.Blue, "Server Start Success!\nWaiting for Connecting......");
                while (true)
                {
                    ResetCheckRead();
                    try
                    {
                        //检查当前Socket,返回可读Socket数组
                        Socket.Select(checkReadList, null, null, 5000);
                    }
                    catch (SocketException e)
                    {
                        DetailLog.Error("StartReceiveError:" + e.Message);
                    }

                    for (int i = checkReadList.Count - 1; i >= 0; i--)
                    {
                        Socket s = checkReadList[i];
                        if (s == socket)
                        {
                            //监听Socket可读,有新的客户端连接到服务器
                            Socket client        = socket.Accept();
                            T      clientSession = new T();
                            clientSession.InitNetSession(client, () =>
                            {
                                if (clientSocketDic.ContainsKey(client))
                                {
                                    clientSocketDic.Remove(client);
                                }
                            }, false);
                            clientSocketDic.Add(client, clientSession);
                        }
                        else
                        {
                            //连接客户端的Socket可读,客户端向服务端发送消息
                            clientSocketDic[s].ServerReceiveData();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                DetailLog.Error("StartServerError:" + e.Message);
            }
        }
Exemplo n.º 3
0
 public void StartServer(IPEndPoint ipEndPoint)
 {
     DetailLog.InitSettings();
     try
     {
         socket.Bind(ipEndPoint);
         socket.Listen(50000);
         socket.BeginAccept(ClientConnectCallBack, socket);
         DetailLog.ColorLog(LogColor.Blue, "Server Start Success!\nWaiting for Connecting......");
     }
     catch (Exception e)
     {
         DetailLog.Error("StartServer:" + e.Message);
     }
 }