简体   繁体   English

异步套接字连接,服务器断开连接时,客户端GUI挂起TCP通信

[英]Async socket connection, Client GUI Hangs When Server is Disconnected TCP Communication

I am using Tcp Sockets For Communication Between CLR C++ (Server) to Android(Client) While using .Net For GUI. 我将Tcp套接字用于CLR C ++(服务器)与Android(客户端)之间的通信,而将.Net用于GUI。 While the data is communicated and received. 在通信和接收数据时。 Using a Background Worker in C++ Application 在C ++应用程序中使用后台工作器

if(backgroundworker1->CancellationPending)
                {
                    listenerSocket->Close(); // Listener Socket is Closed
                    netStream->Close(); 
                    serverSocket->Close();
                    serverSocket->Shutdown(SocketShutdown::Both);
                    e->Cancel;
                    break;
                }

While in Android i am using Async Class for Execution and receiving text from socket to a Handler. 在Android中,我正在使用异步类执行代码,并从套接字接收文本到处理程序。 While in Doinbackground Function i am using this code. 在Doinbackground函数中,我正在使用此代码。

        try
        {

            socket = new Socket(dstAddress, dstPort);
            BufferedReader inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            do
            {
                try
                {
                    if (!inputStream.ready())
                    {
                        if (message != null)
                            {
                                MainActivity.handler.obtainMessage(0, 0, -1,"Server: " + message).sendToTarget();
                            message = "";
                        }
                    }
                    int num = inputStream.read();
                    message += Character.toString((char) num);
                    Log.e(message,message);
                }
                catch (Exception classNot)
                {
                    Log.e("Client TASK","classnot exception");
                }
            }

            while (!message.equals("bye"));
            inputStream.close();
            socket.close();
        }

I don't understand While am sending the Bye Message from the server and (Backgroundworker1->CancellationPending) All server sockets are closed and Mobile Sockets are closed why is the UI Not Responding? 我不明白从服务器发送“再见”消息时,并且(Backgroundworker1-> CancellationPending)所有服务器套接字都已关闭,而移动套接字已关闭,为什么UI没有响应? Please Help.. 请帮忙..

The Problem was in Client in doinbackground Which calls the while loop again hence causing an exception because no data was received in the sockets and causing an exception. 问题出在doinbackground的Client中,该客户端再次调用while循环,因此导致异常,因为套接字中未接收到任何数据并导致异常。 Finally added some sleep to the client that after some time the client query the server while if there is no message from the server the client shutdowns and shifted to postexecution function. 最后,为客户端增加了一些睡眠,一段时间后,客户端会查询服务器,而如果没有来自服务器的消息,则客户端将关闭并转移到执行后功能。

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

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