/// <summary>
 /// 连接服务器
 /// </summary>
 /// <param name="ip">服务器IP</param>
 /// <param name="port">服务器端口</param>
 public void Connect(string ip, int port)
 {
     if (!Connected)
     {
         if (_socket == null)
         {
             _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         }
         _socket.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
         Connected = true;
         TCPEndPoint end = new TCPEndPoint();
         end.Socket = _socket;
         end.Socket.BeginReceive(end.Buffer, 0, 1024, SocketFlags.None, new AsyncCallback(OnReceive), end);
         if (TCPClientConnected != null)  //通知新客户端连入
         {
             TCPClientConnectedEventArgs args = new TCPClientConnectedEventArgs();
             args.End  = end;
             args.Time = DateTime.Now;
             TCPClientConnected(_client_id, args);
         }
     }
 }
 /// <summary>
 /// 客户端连入回调方法
 /// </summary>
 /// <param name="ar"></param>
 private void OnAccept(IAsyncResult ar)
 {
     try
     {
         Socket      new_socket = _socket.EndAccept(ar);
         TCPEndPoint end        = new TCPEndPoint();
         end.Socket = new_socket;
         end.UID    = _client_index++;
         end.Socket.BeginReceive(end.Buffer, 0, 1024, SocketFlags.None, new AsyncCallback(OnReceive), end); //开始第一次数据接收
         _socket.BeginAccept(new AsyncCallback(OnAccept), null);                                            //开始接受下一次客户端连接
         _pulse_time.TryAdd(end.UID, _pulse);                                                               //加入心跳检测
         if (TCPClientConnected != null)                                                                    //通知新客户端连入
         {
             TCPClientConnectedEventArgs args = new TCPClientConnectedEventArgs();
             args.End  = end;
             args.Time = DateTime.Now;
             TCPClientConnected(_server_id, args);
         }
     }
     catch
     {
     }
 }
 /// <summary>
 /// 连接服务器
 /// </summary>
 /// <param name="ip">服务器IP</param>
 /// <param name="port">服务器端口</param>
 public void Connect(string ip, int port)
 {
     if (!Connected)
     {
         if (_socket == null)
         {
             _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         }
         _socket.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
         Connected = true;
         TCPEndPoint end = new TCPEndPoint();
         end.Socket = _socket;
         end.Socket.BeginReceive(end.Buffer, 0, 1024, SocketFlags.None, new AsyncCallback(OnReceive), end);
         if (TCPClientConnected != null)  //通知新客户端连入
         {
             TCPClientConnectedEventArgs args = new TCPClientConnectedEventArgs();
             args.End = end;
             args.Time = DateTime.Now;
             TCPClientConnected(_client_id, args);
         }
     }
 }
 //用户上线 这里简单的使用datagridview.tag属性维护在线用户列表
 void manager_TCPClientConnected(string csID, TCPClientConnectedEventArgs args)
 {
     this.Invoke((Action)delegate()
     {
         textBox1.AppendText(args.Time.ToLongTimeString() + " " + args.End.RemoteIP + ":" + args.End.RemotePort + "连入\n");
         dataGridView1.Rows.Add(args.End.UID, args.End.RemoteIP, args.End.RemotePort, args.Time.ToLongTimeString());
         dataGridView1.Rows[dataGridView1.Rows.Count - 1].Tag = args.End;  //可以自定义数据结构来存储在线列表
     });
 }
        /// <summary>
        /// 客户端连入回调方法
        /// </summary>
        /// <param name="ar"></param>
        private void OnAccept(IAsyncResult ar)
        {
            try
            {
                Socket new_socket = _socket.EndAccept(ar);
                TCPEndPoint end = new TCPEndPoint();
                end.Socket = new_socket;
                end.UID = _client_index++;
                end.Socket.BeginReceive(end.Buffer, 0, 1024, SocketFlags.None, new AsyncCallback(OnReceive), end);  //开始第一次数据接收
                _socket.BeginAccept(new AsyncCallback(OnAccept), null);  //开始接受下一次客户端连接
                _pulse_time.TryAdd(end.UID, _pulse); //加入心跳检测
                if (TCPClientConnected != null)  //通知新客户端连入
                {
                    TCPClientConnectedEventArgs args = new TCPClientConnectedEventArgs();
                    args.End = end;
                    args.Time = DateTime.Now;
                    TCPClientConnected(_server_id, args);
                }
            }
            catch
            {

            }
        }