简体   繁体   English

C#异步服务器/客户端。 套接字(在客户端)“随机”断开连接

[英]c# asynchronous server/client. Socket (on client side) disconnects “randomly”

this is my first post here so please be gentle =) 这是我在这里的第一篇文章,所以请保持柔和=)

I'm writing an asynchronuos server/client application for the management of a DB for my office, i tested it locally on my computer and it worked fine. 我正在编写用于管理办公室DB的异步服务器/客户端应用程序,我在计算机上本地对其进行了测试,并且工作正常。 When I uploaded the server tool on another machine I found something that is driving me crazy: the server/client handle only the first query, when I send another query both server and client crash. 当我将服务器工具上传到另一台机器上时,发现使我发疯的事情:服务器/客户端仅处理第一个查询,而当我发送另一个查询时,服务器和客户端崩溃。

After going through lines in debug mode I found the problem: after the beginconnect() (during the second query) the socket is connected but when the debugger reaches the beginsend() line the socket is disconnected (this happens only on client side, the socket on server side seems connected). 在调试模式下浏览行之后,我发现了问题:在beginconnect()之后(在第二个查询期间),套接字已连接,但是当调试器到达beginsend()行时,套接字已断开连接(这仅发生在客户端,服务器端的套接字似乎已连接)。

I can't figure how it can happen, the socket is re-instantiated every time I send a new query and of course is shutted down and disposed after the query process (send/receive) is completed. 我不知道怎么发生,每次发送新查询时都会重新初始化套接字,并且在查询过程(发送/接收)完成后当然会关闭并丢弃该套接字。 My idea is that the disposal of the socket doesn't happen immediately but after a while (but why always between the new beginconnect() and beginsend()?). 我的想法是,套接字的处置不会立即发生,而是要过一会儿(但是为什么总是在新的beginconnect()和beginsend()之间?)。 Do you have any clue on this? 您对此有任何线索吗? Or I should reserve a room in a mental hospital? 还是我应该在精神病院保留房间?

* NEW PART (EDITED) * *新零件(已编辑)*

well..here again! 好..再来一次! This time the socket on client side remains open for the entire session (socket closure occurs only on client tool closure) but i receive another kind of error: "System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)" After some googling I discovered that this kind of error is generally triggered by the server on the other side of the connection so I tryed to "mine" server code lines to figure where this connection closure command is. 这次,客户端的套接字在整个会话中保持打开状态(套接字关闭仅在客户端工具关闭时发生),但我收到另一种错误:“ System.Net.Sockets.SocketException(0x80004005):现有连接被强制关闭在System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)处的远程主机“”行以显示此连接关闭命令的位置。 I found that the problem may be in the following piece of code: 我发现问题可能出在以下代码中:

    private static void SendCallback(IAsyncResult ar)
    {
        Console.WriteLine("send callback");
        try
        {
            // Retrieve the socket from the state object.
            Socket handler = (Socket)ar.AsyncState;

            // Complete sending the data to the remote device.
            int bytesSent = handler.EndSend(ar);
            Console.WriteLine("\nQuery answer sent to client");

            handler.Shutdown(SocketShutdown.Both);
            //handler.Disconnect(true);
            //handler.Dispose();
            state.content = new List<byte[]>();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
            Console.ReadKey();
        }
    }

deleting the handler.shutdown line the client is no more able to detect the end of transmission (not a real problem, is the last part of the sent packet so it is possible to use that as trigger..at least I think). 删除handler.shutdown行,客户端不再能够检测到传输结束(不是真正的问题,它是发送数据包的最后一部分,因此可以将其用作触发器。至少我认为)。 Any suggestion? 有什么建议吗? I read something about the keepalive but I can't understand how to set it and if it is really necessary in this situation. 我阅读了一些有关keepalive的内容,但我不知道如何设置它以及在这种情况下是否确实需要它。 Thanks in advance for all your effort. 预先感谢您的所有努力。

I would recommend not to open and close sockets like you do. 我建议不要像您一样打开和关闭插座。
Keep a socket open if possible, because it needs to get freed by your program and the OS. 如果可能,请保持套接字处于打开状态,因为您的程序和操作系统需要释放该套接字。

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

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