重新连接到使用c ++服务器的wifi网络后,Android Socket网络无法访问

时间:2017-10-19 14:10:12

标签: android c sockets networking android-wifi

我遇到了问题。我创建了一个连接到wifi网络的应用程序,并通过套接字从该网络检索数据。

我可以在第一次连接到此网络时轻松检索数据。但是,当我手动禁用wifi并以编程方式重新启动与网络和套接字创建的连接时,它无法说套接字无法连接(网络无法访问)。

这是我的主要代码 Main.java

C ++服务器类 Server.cpp

服务器在线程中运行 myServer.cpp

stackTrace:

com.maxence.appwifiosselet D/com.maxence.appwifiosselet.MainFragment: Reconnect
com.maxence.appwifiosselet W/com.maxence.appwifiosselet.MainFragment: Wi-Fi not enabled
com.maxence.appwifiosselet I/WifiManager: isAllowWifiWarning() -> isCscWifiEnableWarning : false ChinaNalSecurityType : 
com.maxence.appwifiosselet W/com.maxence.appwifiosselet.MainFragment: Wi-Fi succesfully enabled
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.MainFragment: SSID of the connected network during connecting : eduroam
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.MainFragment: Connection to wrong network, disconnect
com.maxence.appwifiosselet W/com.maxence.appwifiosselet.MainFragment: Disconnected succesfully from connection receiver
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.MainFragment: Connect to the osselet wifi
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.MainFragment: Connect to the Wi-Fi network Osselet...
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.MainFragment: Conf created
com.maxence.appwifiosselet W/com.maxence.appwifiosselet.MainFragment: Disconnected succesfully in enableConf
com.maxence.appwifiosselet D/ConnectivityManager: requestNetwork; getAppId(CallingUid) : 10167, CallingPid : 26040
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.MainFragment: Disconnected from reconnect
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.MainFragment: Disconnected from reconnect
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.MainFragment: SSID of the connected network during connecting : osselet
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.MainFragment: SSID of the connected network during connecting : osselet
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.MainFragment: Network connection has been established
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.MainFragment: SSID of the connected network : osselet
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.WifiDataAsyncTask: Creating socket on IP 10.3.141.1:1500
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.WifiDataAsyncTask: Error creating socket
com.maxence.appwifiosselet W/System.err: java.net.ConnectException: failed to connect to /10.3.141.1 (port 1500): connect failed: ENETUNREACH (Network is unreachable)
com.maxence.appwifiosselet W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:124)
com.maxence.appwifiosselet W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
com.maxence.appwifiosselet W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
com.maxence.appwifiosselet W/System.err:     at java.net.Socket.connect(Socket.java:884)
com.maxence.appwifiosselet W/System.err:     at java.net.Socket.connect(Socket.java:827)
com.maxence.appwifiosselet W/System.err:     at com.maxence.appwifiosselet.WifiDataAsyncTask.doInBackground(WifiDataAsyncTask.java:47)
com.maxence.appwifiosselet W/System.err:     at com.maxence.appwifiosselet.WifiDataAsyncTask.doInBackground(WifiDataAsyncTask.java:25)
com.maxence.appwifiosselet W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
com.maxence.appwifiosselet W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
com.maxence.appwifiosselet W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
com.maxence.appwifiosselet W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
com.maxence.appwifiosselet W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
com.maxence.appwifiosselet W/System.err:     at java.lang.Thread.run(Thread.java:818)
com.maxence.appwifiosselet W/System.err: Caused by: android.system.ErrnoException: connect failed: ENETUNREACH (Network is unreachable)
com.maxence.appwifiosselet W/System.err:     at libcore.io.Posix.connect(Native Method)
com.maxence.appwifiosselet W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
com.maxence.appwifiosselet W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
com.maxence.appwifiosselet W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)
com.maxence.appwifiosselet W/System.err:    ... 12 more
com.maxence.appwifiosselet W/System.err: java.net.SocketException: Socket is not connected
com.maxence.appwifiosselet W/System.err:     at java.net.Socket.checkOpenAndCreate(Socket.java:671)
com.maxence.appwifiosselet W/System.err:     at java.net.Socket.getInputStream(Socket.java:361)
com.maxence.appwifiosselet W/System.err:     at com.maxence.appwifiosselet.WifiDataAsyncTask.doInBackground(WifiDataAsyncTask.java:56)
com.maxence.appwifiosselet W/System.err:     at com.maxence.appwifiosselet.WifiDataAsyncTask.doInBackground(WifiDataAsyncTask.java:25)
com.maxence.appwifiosselet W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
com.maxence.appwifiosselet W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
com.maxence.appwifiosselet W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
com.maxence.appwifiosselet W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
com.maxence.appwifiosselet W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
com.maxence.appwifiosselet W/System.err:     at java.lang.Thread.run(Thread.java:818)
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.WifiDataAsyncTask: Error read InputStream
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.WifiDataAsyncTask: Socket closed because InputStream stopped
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.MainFragment: CloseSocketReceiver
com.maxence.appwifiosselet D/com.maxence.appwifiosselet.WifiDataAsyncTask: Start the server first

编辑:我设法处理了一个C ++客户端关闭套接字。它适用于客户端client.cpp但是,当我关闭Android套接字时,c ++服务器没有检测到它...请帮助,它真的很烦人:))

EDIT2:我设法让它发挥作用!出于某种原因,当触发NETWORK_STATE_CHANGED上的BroadcastReceiver并返回CONNECTED时,网络似乎无法访问几毫秒。我在启动活动之前添加了一个Thread.sleep(200)。

 class WifiConnectedStateChangedReceiver extends BroadcastReceiver {
        public void onReceive(Context c, Intent intent) {
            if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                NetworkInfo.State state = networkInfo.getState();

                if (state == NetworkInfo.State.CONNECTED) {
                    changeUIToConnected();

                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // Start retrieving data from server
                    startWifiDataAsyncTask();

                } else if (state == NetworkInfo.State.DISCONNECTED) {
                    if (!tryReconnect) {
                        Log.d(TAG, "Network disconnected");
                        changeUIToDisconnected();
                        // Stop AsyncTask
                        stopWifiDataAsyncTask();

                    } else {
                        Log.d(TAG, "Disconnected from reconnect");
                    }
                }
                // Check if connection to the right network (osselet) during connection
                else if (state == NetworkInfo.State.DISCONNECTING) {
                    Log.d(TAG, "Disconnecting to " + networkInfo.toString());
                }
            }
        }
    }

感谢您的帮助!

0 个答案:

没有答案