我正在尝试为简单的客户端/服务器连接创建一个类。此类内部是一个服务器端客户端,但它不会接收写入服务器 NetworkStream 的内容。如果您想知道,我打算让所有数据在发送到客户端之前通过服务器程序,所以我需要一些东西来处理 NetworkStream(因此是服务器端客户端)上的所有数据。这是我的课:
public class ConnectionServer
{
public readonly IPAddress ServerIP;
public readonly IPAddress LocalIP;
public readonly int Port;
private TcpListener ServerIn;
private Socket ServerSocket;
private TcpClient ServerSideClient;
private NetworkStream NetStream;
public StreamReader ServerInput;
public StreamWriter ServerOutput;
public ConnectionServer(int port)
{
try
{
Port = port;
ServerIP = IPAddress.Parse(NetHelper.GetPublicIP());
LocalIP = IPAddress.Parse(NetHelper.GetLocalIP());
ServerIn = new TcpListener(IPAddress.Any, Port);
ServerIn.Start();
Console.WriteLine("Server Started At: " + ServerIP.ToString() + ":" + Convert.ToString(Port));
ServerSideClient = new TcpClient("localhost", Port);
ServerSocket = ServerIn.AcceptSocket();
Console.WriteLine("Server-Side Client Socket Accepted.");
NetStream = ServerSideClient.GetStream();
ServerInput = new StreamReader(NetStream);
ServerOutput = new StreamWriter(NetStream);
ServerOutput.AutoFlush = true;
Console.WriteLine("All Streams Initialized.");
ServerOutput.WriteLine("Testing Server-Side Client.");
Console.WriteLine("Test Message Sent.");
Console.WriteLine(ServerInput.ReadLine());
}
finally
{
}
}
~ConnectionServer()
{
ServerInput.Dispose();
ServerSideClient.Close();
}
}
它没有写任何东西Console.WriteLine(ServerInput.ReadLine());
。我不明白为什么它不会从流中接收消息。请帮忙?