/// <summary> /// 连接上服务器 /// </summary> void OnConnect(IAsyncResult asr) { outStream = client.GetStream(); client.GetStream().BeginRead(byteBuffer, 0, MAX_READ, new AsyncCallback(OnRead), null); state = NetworkManager.STATE.COLLECTED; Log("连接服务器成功"); retryTime = 0; /* * //登陆成功之后发送102协议 * byte[] modules = RSA.GetModulus(); * SendProtocol(102, modules); * AddProtoclCallback(new ProtocolCallback() * { * protocol = 1020, * callback = (a) => * { * Log("上传Modules:" + a["result"]); * SendProtocol(103, RSA.GetExponent()); * } * }); * * AddProtoclCallback(new ProtocolCallback() * { * protocol = 1030, * callback = (a) => * { * Log("上传Exponent:" + a["result"]); * state = NetworkManager.STATE.COLLECTED; * } * });*/ }
/// <summary> /// 立刻关闭链接 /// </summary> public void CloseConnection() { if (client != null) { if (client.Connected) { client.Close(); } client = null; } state = NetworkManager.STATE.DISCONNECT; }
/// <summary> /// 连接到服务器 /// </summary> public void Connect() { if (client != null) { CloseConnection(); } client = new TcpClient(); client.SendTimeout = 1000; client.ReceiveTimeout = 1000; client.NoDelay = true; state = NetworkManager.STATE.CONNECTING; Log("开始连接服务器..."); connectionTime = Time.time; try { var callback = new AsyncCallback(OnConnect); client.BeginConnect("218.17.99.70", 9919, callback, null); } catch (Exception e) { CloseConnection(); Log("" + e.Message); } }