我希望有人能告诉我为什么在tcplistener/tcpclient代码中会出现对象释放异常。
在acceptconnections和connecttoserver方法中,我使用keepalive方法来告诉我何时断开连接,它工作得很好。
但是,如果我取消对sendmsg方法的for循环的注释,我将在服务器上得到一个objectdisposedexception,在客户端得到一个ioexception。
sendmsg中tcpclient.get stream()的networkstream似乎是问题所在,但我不确定它为什么会得到已释放的流。我需要2个线程来处理它吗?

static void Main(string[] args)
{
    server.Listen();
    server.AcceptConnections();

    client.ConnectToServer();

    //for (int i = 0; i < 5; i++) {
    //    Thread.Sleep(3000);
    //    server.SendMsg("SENT MSG");
    //}

    Console.ReadLine();
}

public async void SendMsg(String message) {
        try {
            NetworkStream networkStream = tcpClient.GetStream();
            using (var writer = new StreamWriter(networkStream)) {
                await writer.WriteLineAsync(message);
                Console.WriteLine("msg sent");
            };
        } catch (Exception e) {
        }

    }

private async void KeepAlive(TcpClient tcpClient) {
        bool clientConnected = true;
        using (NetworkStream networkStream = tcpClient.GetStream())
        using (var reader = new StreamReader(networkStream))
        using (var writer = new StreamWriter(networkStream)) {
            writer.AutoFlush = true;
            char keepalive = '0';
            while (clientConnected) {
                try {
                    await writer.WriteLineAsync(keepalive);
                    string dataFromClient = await reader.ReadLineAsync();
                    Console.WriteLine("Server: " + dataFromClient);
                    Thread.Sleep(500);
                } catch (IOException e){

                } catch(ObjectDisposedException e) {
                    clientConnected = false;
                    clientsConnected--;
                } catch (Exception e){
                }
            }
        }
    }

编辑:同时发布我的AcceptConnections方法
public async void AcceptConnections() {
        while (true) {
            while (clientsConnected <= maxConnections) {
                try {
                    tcpClient = await tcpListener.AcceptTcpClientAsync();
                    KeepAlive(tcpClient);
                } catch (Exception e) {
                    Console.WriteLine("TOP EXCEPTION :: " + e);
                }
                clientsConnected++;
                Console.WriteLine("SERVER Clients connected: " + clientsConnected);
            }
        }
    }

最佳答案

您的SendMsg方法在using上使用StreamWriterStreamWriter的默认设置是级联释放,因此这将关闭NetworkStream。如果这不是您的意图,您需要将leaveOpen: true传递给constructor overload
不过,坦率地说,在这里没有理由使用StreamWriter——我建议直接处理StreamEncodingapi。StreamWriter的一个优点是,它可以在内部为byte[]工作重新使用一个缓冲区,但是如果在处理它之前只对一个Write使用缓冲区,那么“优势”是没有意义的,并且可以通过缓冲池轻松实现。

关于c# - C#NetworkStream配置在tcp/ip上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48095486/

10-12 04:36