精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
I have this same issue. Not sure how or why it does it. (as I'm still learning sockets and such).我有同样的问题。 不知道为什么它。 (我仍然学习套接字等)。
I'm trying to sort this out and will get back as and when it happens. Thanks,我想重现此问题,谢谢
I figured it out! The problem is in the SGServerform.cs, OnReceive(IAsynchresult ar) function.我想出来! 问题是在SGServerform.cs,OnReceive(IAsynchresult ar)函数
The concept is that the server has to be listening for more messages from all its clients. If you noticed, after going through the switch statement the serverSocket.BeginReceiveFrom() is called, EXCEPT when a user sends the logout message.这个概念是服务器必须倾听更多的来自客户的消息。 如果你注意到,经过switch语句后调用了serverSocket.BeginReceiveFrom(),除非用户发送退出消息。
So how do we start get it to listen again (i.e. any new messages)?. Look at when the server starts in the Load function: it starts the serverSocket.BeginReceiveFrom(). This is the behavior we copy from. So in the case statement for the logout message:那么我们如何开始让它再听一遍(即任何新消息)?。 看服务器启动时加载功能:它开始serverSocket.BeginReceiveFrom()。 这是我们复制的行为。 所以在注销的case语句的信息:
Do this:这样做:
case Command.Logout:案例Command.Logout:
//When a user wants to log out of the server then we search for her
//in the list of clients and close the corresponding connection
int nIndex = 0;
foreach (ClientInfo client in clientList)
{
if (client.endpoint == epSender)
{
EndPoint testsender = (EndPoint)ipeSender;
serverSocket.BeginReceiveFrom(ReceivedData, 0, ReceivedData.Length,
SocketFlags.None, ref testsender, new AsyncCallback(OnReceive), testsender);
clientList.RemoveAt(nIndex);
break;
}
++nIndex;
}
Notice all the beginreceivefrom in the app use the endpoint of the incoming message, here we use the server's endpoint to start listening as it was done when first starting it.Hope that helps! 注意所有的beginreceivefrom应用使用传入消息的端点,这里我们使用服务器的端点开始听,因为它是第一个开始的时候完成的。 希望会有帮助!
no it doesnt work, cant find var "receivedData"idea is right, but the problem is still there.不,它不工作,不能找到var“receivedData” 想法是对的,但是问题仍然存在
Hi, try this:嗨,试试这个:
case Command.Logout:
foreach (ClientInfo client in clientList) {
if (client.endpoint.Equals(epSender)) {
clientList.Remove(client);
break;
}
}
//if (msgReceived.cmdCommand != Command.Logout) {
serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length,
SocketFlags.None, ref epSender,
new AsyncCallback(OnReceive), epSender);
//}
your two methods fixes the problem that the server is not responding any more when a user leaves the chat and the problem that the user isn't removed from the array. But there is another problem. Try to kill the chat client with the Task Manager after logging in to the server and connect again to the server. You will get the error:你的两种方法修复问题,服务器没有响应了当用户离开聊天和用户的问题不是从数组中删除。 但还有另一个问题。 试图杀死的聊天客户端任务管理器后再次登录服务器和连接到服务器。 你会得到错误:
ErrorCode: 10054
InnerException: Nothing
Message: "An existing connection was forcibly closed by the remote host"
错误代码:10054
InnerException:没有
信息:“强行关闭了一个现有的连接远程主机”
I got this problem in an application I'm trying to develop and I'm searching for a good solution to the problem. I found out that when the dead client is removed from the array and the BeginReceiveFrom is called again then the problem seems to be solved. But how do I find out which client is dead?
我有这个问题在一个应用程序试图开发,我寻找一个好的解决问题的办法。 我发现当死者端从数组中移除并再次调用BeginReceiveFrom然后问题似乎解决了。 但我怎么找出哪些客户是死了吗?
problem solved to remove dead client from the list but when this dead client second time login at that time we remove this client but this dead client not login second time at that time any other person which in network fire the messae at that time we face that same error问题解决了从列表中删除死客户,但当这死端第二次登录当时我们删除这个客户但这死客户不是登录第二次当时他人在网络火messae那时我们面临同样的错误
Then you modify SGSserverForm.cs file 然后你修改SGSserverForm。 cs文件
case Command.Logout:案例Command.Logout:
//When a user wants to log out of the server then we search for her
//in the list of clients and close the corresponding connection
/ /当用户想要退出服务器然后我们在客户列表中寻找她并关闭对应她的连接
int nIndex = 0;
foreach (ClientInfo client in clientList)
{
if (client.endpoint == epSender)
{
//epSender.
clientList.RemoveAt(nIndex);
break;
}
++nIndex;
}
msgToSend.strMessage = "<<<" + msgReceived.strName + " has logout>>>";
break;