简体   繁体   English

NetworkStream上的StreamReader没有读取行

[英]StreamReader on NetworkStream not reading line

I'm trying to make a class for easy Client/Server connections. 我正在尝试创建一个简单的客户端/服务器连接类。 Inside this class is a Server-Side Client, but it won't pick up things written to the server NetworkStream. 在这个类中是一个服务器端客户端,但它不会拾取写入服务器NetworkStream的东西。 In case you're wondering, I intend to have all data go through the server program before sending it out to the Client, so I need something to process all the data on the NetworkStream (Hence Server-Side Client). 如果你想知道,我打算让所有数据通过服务器程序,然后再发送给客户端,所以我需要一些东西来处理NetworkStream上的所有数据(因此服务器端客户端)。 Here's my Class: 这是我的班级:

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();
    }

}

It doesn't write anything on Console.WriteLine(ServerInput.ReadLine()); 它不会在Console.WriteLine(ServerInput.ReadLine());上写任何内容Console.WriteLine(ServerInput.ReadLine()); . I can't figure out why it won't pick up the message off the stream. 我无法弄清楚为什么它不会从流中获取消息。 Help please? 请帮助?

Your immediate problem is that you're trying to use the same stream for reading and writing. 您当前的问题是您尝试使用相同的流进行读写。 NetStream is the input stream--the data from the client. NetStream是输入流 - 来自客户端的数据。 If you want to write to the socket, you need to create a stream from the socket. 如果要写入套接字,则需要从套接字创建流。

As it's written, you're writing to the client stream. 在编写时,您正在写入客户端流。 You need to write to the server stream. 您需要写入服务器流。

What I don't understand is why you're using TcpClient on the client side, and raw sockets on the server side after creating a TcpListener . 我不明白为什么你在客户端使用TcpClient ,在创建TcpListener后在服务器端使用原始套接字。 You'd be better off using AcceptTcpClient . 你最好使用AcceptTcpClient Then you could write to that client's stream and read from the other stream. 然后,您可以写入该客户端的流并从另一个流中读取。

To create a NetworkStream from the socket, use the constructor . 要从套接字创建NetworkStream ,请使用构造函数 I strongly suggest, though, that you switch to AcceptTcpClient instead. 不过,我强烈建议您切换到AcceptTcpClient

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM