public Server(TcpClient client) { this.client = client; // 打印连接的客户端信息 Console.WriteLine($"Client Connected! Local:{client.Client.LocalEndPoint} <-- Client:{client.Client.RemoteEndPoint}"); // 获取流 streamToClient = client.GetStream(); buffer = new byte[bufferSize]; // 设置RequestHanlder hanlder = new RequestHanlder(); // 在构造函数中就准备开始读取 var callback = new AsyncCallback(ReadComplete); streamToClient.BeginRead(buffer, 0, bufferSize, callback, null); }
public Client() { try { client = new TcpClient(); client.Connect(new IPAddress(new byte[] { 127, 0, 0, 1 }), 8500); } catch (Exception e) { Console.WriteLine(e.Message); return; } buffer = new byte[bufferSize]; // 设置RequestHanlder hanlder = new RequestHanlder(); // 打印连接到的服务器信息 Console.WriteLine($"Server connected! Local:{client.Client.LocalEndPoint} <-- Server:{client.Client.RemoteEndPoint}"); streamToServer = client.GetStream(); }