如何阻止所有客户端与服务器断开连接。 [英] How to stop all clients from disconnecting from server.

查看:199
本文介绍了如何阻止所有客户端与服务器断开连接。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,每当我运行多个客户端时,我就创建了一个客户端和一个服务器,然后关闭其中一个,其他客户端在尝试向其他活动客户端发送消息时立即断开连接。这可能是由于我的代码中存在错误,但我仍无法找到问题所在。



每当我关闭客户端时,我都会调用此代码:

So I have created a client and a server howerver whenever I run multiple clients and then close 1 of them the other clients disconnect as soon as they try to message other active clients. This may be due to a mistake inside my code however I still cant manage to find where the problem lies.

Whenever I close a client I call this code:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
  Socket_Client.Close();
}





下面是另一篇帖子的链接,其中我显示了我目前使用的整个代码。我在这个网站上创建了一个帖子,提供了有关我的问题的更多信息。任何帮助将不胜感激。



c# - 当我尝试向连接的用户发送消息时,断开与服务器的连接关闭所有其他连接 - Stack Overflow [ ^ ]



我尝试过:



我试图找到问题并发现使用方法



Below is a link to another post where I have displayed the entire code which I currently use. I have created a post on this site giving more information regarding my problem. Any help will be greatly appreciated.

c# - Disconnecting from server close all other connections when I try to send message to a connected user - Stack Overflow[^]

What I have tried:

I have tried to locate the problem and found that withing the method

private void doChat()





方法:



Both the method :

networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

                        dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                        dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                        Console.WriteLine("From client - " + clNo + " : " + dataFromClient);
                        rCount = Convert.ToString(requestCount);
                        //ConnUsers.Add(clNo);
                        Program.broadcast(dataFromClient, clNo, true);





并且catch方法被触发。



我在服务器上获得的输出是一个基于控制台的应用程序,例如



服务器是倾听

client1加入

client2加入

client3加入

client1已断开

client2说嘿

client2已断开



如果一个客户端断开连接显示的示例,每当我尝试发送一个客户端时它似乎关闭所有客户端给连接的客户发送消息。



任何帮助将不胜感激。由于我不熟悉使用服务器/客户端应用程序进行编码,因此我一直坚持这个问题。



And the catch method is Fired.

The output I get on server which is a console based application is for example

The server is listening
client1 joined
client2 joined
client3 joined
client1 disconnected
client2 says hey
client2 disconnected

As the example displayed once 1 client is disconnected it seems to close all the clients whenever I try to send a message to the connected clients.

Any help will be greatly appreciated. I have been stuck on this problem for quite some time as I am new to coding with server / client application.

推荐答案

));

Console.WriteLine(来自客户端 - + clNo +:+ dataFromClient);
rCount = Convert.ToString(requestCount);
//ConnUsers.Add(clNo);
Program.broadcast(dataFromClient,clNo,true);
")); Console.WriteLine("From client - " + clNo + " : " + dataFromClient); rCount = Convert.ToString(requestCount); //ConnUsers.Add(clNo); Program.broadcast(dataFromClient, clNo, true);





并且捕获方法被触发。



我在服务器上获得的输出是一个基于控制台的应用程序,例如



服务器正在监听

client1加入

client2加入

client3加入

client1已断开

client2表示嘿

client2已断开连接



如果一个客户端断开连接时显示的示例,每当我尝试发送消息时,它似乎都会关闭所有客户端联系客户。



任何帮助将不胜感激。我一直坚持这个问题很长一段时间,因为我不熟悉服务器/客户端应用程序的编码。



And the catch method is Fired.

The output I get on server which is a console based application is for example

The server is listening
client1 joined
client2 joined
client3 joined
client1 disconnected
client2 says hey
client2 disconnected

As the example displayed once 1 client is disconnected it seems to close all the clients whenever I try to send a message to the connected clients.

Any help will be greatly appreciated. I have been stuck on this problem for quite some time as I am new to coding with server / client application.


我没有查看所有代码,但查看了异常处理 doChat 看起来当发生异常时,您认为客户端已断开连接。但是,有很多原因可能会发生异常。



所以我的建议是,修改异常处理,以便您首先处理您准备的异常至。之后,通过记录或以其他方式收集和发布有关异常的信息来处理其他异常。 Console.WriteLine是一种可能性,如果你愿意,你可以使用日志文件。
I didn't go through all the code but looking at the exception handling in doChat it looks like when an exception occurs you assume that the client has disconnected. However, there are tons of reasons why an exception might occur.

So my suggestion is, modify the exception handling so that you first handle the exceptions that you're prepared to. After that handle other exceptions by logging or otherwise collecting and publish information about the exception. Console.WriteLine is one possibility or you could use a log file if you like.


这篇关于如何阻止所有客户端与服务器断开连接。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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