Esempio n. 1
0
        private void Thread_WatchConnecting() //监听客户端连接的方法
        {
            string str1;

            while (bWatchAcceptFlag)
            {
                Thread.Sleep(100);
                try
                {
                    Socket connection = g_SocketServer.Accept();
                    str1 = connection.RemoteEndPoint.ToString();

                    ListenClient client = new ListenClient(this, connection);
                    g_lstConnectClientTab.Add(client); //增加收到的连接新客户端信息

                    client.RunRecv();                  //输入监听启动事件
                    //产生连接事件给外部
                    EventArgs_Connect args = new EventArgs_Connect();

                    if (OnConnect != null)   //产生连接事件,,给具体的监听类
                    {
                        OnConnect(this, args);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                    continue;
                }
            }
        }
Esempio n. 2
0
 public short ConnectServer()//接收信息
 {
     try
     {
         ClientSocket.Connect(ServerInfo);
         sLocalPort = ClientSocket.LocalEndPoint.ToString();
         Thread th = new Thread(ReceiveMsg);
         th.Name         = ClientSocket.LocalEndPoint.ToString() + "接收服务器消息线程";
         th.IsBackground = true;
         th.Start();//
         bConnected = true;
         //产生事件
         EventArgs_Connect args = new EventArgs_Connect();
         args.sConnMsg  = "";
         args.sConnTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
         OnConnect(this, args);
         return(0);
     }
     catch (Exception ex)
     {
         Debug.WriteLine("登录服务器失败!" + ex.Message.ToString());
         return(-1);
     }
 }