Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

c# - ReadAsync stops code execution, no exception is thrown and Task is still running

So I'm connecting to a proxy server with my C# program using the TcpClient class. I establish the connection and just keep reading from the server into the buffer with the ReadAsync method. But when the proxys device disconnects, it looks like the ReadAsync method goes into a loop and no further code is being processed. Funny thing is, no exception is thrown and the Task still continues to work (putting an await at the Listen() call prevents further execution, even when the device is disconnected).

Code:

        public async Task Listen()
        {
            try
            {
                await server.ConnectAsync(ServerIP, ServerPort);

                if (server.Connected)
                {                    
                    using (NetworkStream stream = server.GetStream())
                    {
                        while (server.Connected)
                        {
                            try
                            {
                                byte[] buffer = new byte[server.ReceiveBufferSize];

                                int length = await stream.ReadAsync(buffer, 0, buffer.Length);
                            }
                            catch(Exception e)
                            {
                                server.Close();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                server.Close();
            }
        }

Does anybody have any idea how to detect when the device is disconnected, so I could handle it further? The proxy server does not shut down, it just stops sending data.

question from:https://stackoverflow.com/questions/65903354/readasync-stops-code-execution-no-exception-is-thrown-and-task-is-still-running

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This can happen if the proxy side of the connection is clamped shut - i.e., closed without sending your application a packet indicating it is closed. This leaves the connection in a half-open state.

You need to design your application to handle this. The best approaches are to have a "heartbeat" or "keepalive" packet that is periodically sent by each side. If your proxy protocol supports that, then that would be ideal. Otherwise, you'll probably need to use a timeout; just set a timer (one per connection), reset it each time you receive data, and close your side of the socket when the timer fires.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...