WifiP2pInfo.groupOwnerAddress.getHostAddress()错误的IP

时间:2014-11-25 20:22:02

标签: android sockets android-wifi wifi-direct

我有两个设备 - 注3(N9005)和G Pad 8.3(也用Nexus 4而不是注3测试,结果相同)。
当Note 3是组所有者时创建Wifi P2P组时,在G Pad(客户端)上,方法WifiP2pInfo.groupOwnerAddress.getHostAddress()返回正确的IP。 但是当我在G Pad是组所有者时创建Wifi P2P组时,在Note 3(客户端)上该方法返回错误的IP地址!

这是来自G Pad的ipconfig作为具有正确IP地址的组所有者:

ipconfig on the tablet while being group owner

以及尝试连接给定IP地址时注释3上的ECONNREFUSED异常:

11-25 21:04:53.264    4817-4986/com.example.wifidirect.app W/System.err﹕ java.net.ConnectException: failed to connect to /192.168.49.1 (port 7958) after 5000ms: isConnected failed: ECONNREFUSED (Connection refused)
11-25 21:04:53.264    4817-4986/com.example.wifidirect.app W/System.err﹕ at libcore.io.IoBridge.isConnected(IoBridge.java:223)
11-25 21:04:53.264    4817-4986/com.example.wifidirect.app W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
11-25 21:04:53.264    4817-4986/com.example.wifidirect.app W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:112)
11-25 21:04:53.264    4817-4986/com.example.wifidirect.app W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-25 21:04:53.264    4817-4986/com.example.wifidirect.app W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
11-25 21:04:53.264    4817-4986/com.example.wifidirect.app W/System.err﹕ at java.net.Socket.connect(Socket.java:833)
11-25 21:04:53.264    4817-4986/com.example.wifidirect.app W/System.err﹕ at com.yoavst.wifidirectlib.WifiP2P$5.run(WifiP2P.java:237)
11-25 21:04:53.264    4817-4986/com.example.wifidirect.app W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
11-25 21:04:53.264    4817-4986/com.example.wifidirect.app W/System.err﹕ Caused by: libcore.io.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
11-25 21:04:53.264    4817-4986/com.example.wifidirect.app W/System.err﹕ at libcore.io.IoBridge.isConnected(IoBridge.java:208)
11-25 21:04:53.264    4817-4986/com.example.wifidirect.app W/System.err﹕ ... 7 more

192.168.49.1不是192.168.49.129

这也是代码,但我不认为这是问题:

客户端

Socket socket = new Socket();
                    socket.setReuseAddress(true);
                    socket.connect((new InetSocketAddress(info.groupOwnerAddress.getHostAddress(), PORT_GET_IP)), 5000);
                    OutputStream os = socket.getOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(os);
                    oos.writeObject(HANDSHAKE_STRING);
                    oos.close();
                    os.close();
                    socket.close();

服务器

ServerSocket serverSocket = new ServerSocket(PORT_GET_IP);
                            Socket client = serverSocket.accept();
                            ObjectInputStream objectInputStream = new ObjectInputStream(client.getInputStream());
                            Object object = objectInputStream.readObject();
                            if (object.getClass().equals(String.class) && object.equals(HANDSHAKE_STRING)) {
                                otherDeviceINetAddress = client.getInetAddress();
                                initClient(client.getInetAddress().toString().substring(1));
                            }

1 个答案:

答案 0 :(得分:2)

根据您的上一条评论,您无法将所需设备设置为群组所有者。到目前为止你采取了哪些步骤?根据{{​​3}},您可以尝试在首次尝试通过设置android documentation的WifiP2pConfig类进行连接时设置组所有者但是,这不是100%可靠,系统最终可能会决定谁小组所有者将是,特别是如果您过去已经建立了连接并且组信息已被保留。