Example #1
0
        // 作为客户端,开始异步连接服务器
        public void Connect(string ip, int port)
        {
            IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(ip), port);

            try
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                // 开始连接
                socket.BeginConnect(ipe, new AsyncCallback(ConnectionCallback), socket);
            }
            catch (Exception ex)
            {
                handler.OnConnectFailed(ex);
            }
        }