0

我有简单的客户端服务器应用程序。

客户端代码:

public class Client
{
    private TcpClient client;

    private NetworkStream stream;

    ...

    public Client(string clientName)
    {
        this.client = new TcpClient(); 
        this.client.Connect(this.Address, 13465);
        this.stream = this.client.GetStream();
    }

    public void Listening()
    {
        while (true)
        {
             BinaryFormatter formatter = new BinaryFormatter();
             string data = (string)formatter.Deserialize(stream);

             // do something with data
        }
    }

    ...
}

监听方法是一种接收数据的方法,你可以看到它有无限循环。

问题是,我只能从服务器收到一条消息。这意味着,该服务器向我发送字符串,我收到此字符串。服务器向我发送另一个字符串,而我的客户端什么也不做。

我 100% 确定,该服务器工作正常,因为我检查了几次(即使使用 Wireshark)。有人有什么理想,如何改进我的应用程序以接收来自服务器的所有消息?

4

0 回答 0