多个客户端服务器C# [英] Multiple client server C#

查看:176
本文介绍了多个客户端服务器C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们我有一个简单的问题。我是套接字编程的新手,所以请不要生气。

我正在编写一个简单的多客户端 - 服务器应用程序,问题是:当我尝试时总是收到错误断开客户端。

服务器和客户端之间的所有操作(例如接收,发送或连接新客户端)都是异步的 - 我认为这可能会导致此问题。当服务器断开客户端时,我立即收到错误,因为例如EndReceive方法仍在工作。

如何避免这些错误?我应该尝试使用异步断开方法吗?或者这样吗?



提前致谢。



PS抱歉我的英语不好 - 它还在改进。

Guys i've got a simple problem. I am totally new in socket programming, so please don't get angry.
I'm writing a simple multiple client-server app and the problem is: i'm always getting an error when i try to disconnect a client.
All actions between server and client (such a receiving, sending or connecting a new client) are asynchronous - and i think it might cause this problem. When the server disconnects a client I get
immediately an error, because for example EndReceive method is still working.
How to avoid these errors? Should I try to use an asynchronously disconnecting method or sth like this?

Thanks in advance.

p.s. Sorry for my bad English - it's still improving.

推荐答案

如果服务器以任何方式断开Tcp套接字而不告诉客户端,那么您的客户端没有其他办法检测到这一点,然后尝试写入套接字并捕获后面的异常。如果您不想这样,则应使用或开发协议来警告您的客户端应用程序。但您仍应在客户端和服务器中编写该过程,以检测由于网络问题而导致的连接丢失。
If the server disconnects the Tcp socket in any way without telling the client, there is no other way for your client to detect this then to try to write to the socket and catch the exception that follows. If you do not want this you should use or develop a protocol to warn your client application. But you should still write the procedure in your client and also in your server to detect loss of connection because of network problems.


这篇关于多个客户端服务器C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆