简体   繁体   English

如何处理NetworkStream.Write失败

[英]How to handle NetworkStream.Write failure

I have two applications a server and a client. 我有两个应用程序:服务器和客户端。 I can send messages between the two as long as they are both running. 只要两者都在运行,我就可以在两者之间发送消息。

When I close the server and try to send a message with NetworkStream.Write i get an exception. 当我关闭服务器并尝试使用NetworkStream.Write发送消息时,出现异常。

What would be the best way to handle this and notify the client that the server is down? 处理此问题并通知客户端服务器已关闭的最佳方法是什么?

您可以在发送消息之前对服务器执行ping操作,如果ping操作失败,则通知用户并且不发送消息等。

Ping is a bad idea. Ping是个坏主意。 The connection can go down between the ping and the actual message too. ping和实际消息之间的连接也可能断开。

As you yourself pointed out: 正如您自己指出的那样:

wrap the write in a try/catch and display a nice little message to the user. 将写入内容包装在try / catch中,并向用户显示一条不错的消息。

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

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