连接wifi热点电话错误:ENETUNREACH

时间:2014-12-02 04:08:51

标签: java android android-wifi

我有一个应用程序,应用程序连接两个手机使用热点发送/ recv文件。一部手机创建热点(Wifi热点),我使用以下代码连接另一个热点手机:

            mSocket = new Socket();

            try {

                mSocket.setReuseAddress(true);
                mSocket.bind(null);
                mSocket.setPerformancePreferences(0, 1, 2);
                mSocket.setTcpNoDelay(true);
                mSocket.setSoLinger(true, 0);
                mSocket.setKeepAlive(true);

                mSocket.connect(new InetSocketAddress(mConnection.getServerAddress(),
                        mConnection.getServerPort()), SOCKET_CONNECT_TIMEOUT);

           //operation here....
            } catch (SocketException e) {
                ((HotSpotConnection)mConnection).getlocalip();
                LogUtils.LOGE(LOG_TAG, "init exception:" + e.toString());
            } catch (IOException e) {
                LogUtils.LOGE(LOG_TAG, "init exception:" + e.toString());
            }

我得到了以下错误:

E/MyTestWifiThread(18918): init exception:java.net.ConnectException: 
failed to connect to /192.168.43.1 (port 9876) after 30000ms: 
isConnected failed: ENETUNREACH (Network is unreachable)

为什么无法连接热点手机,我确定热点手机地址默认为192.168.43.1?因为我已经ping了192.168.43.1并且热点电话回复了。

1 个答案:

答案 0 :(得分:0)

之前我遇到过这个问题。这对我有用。

更改套接字的通信方式。

尝试在连接到热点的客户端上打开服务器套接字,并在网络共享设备上创建客户端套接字以连接到服务器套接字。

Android connect to tethering socket

这可能会有所帮助!!