【问题标题】:how to send data from Group owner to client with wifi p2p如何使用wifi p2p将数据从组所有者发送到客户端
【发布时间】:2015-09-26 19:34:07
【问题描述】:

我测试了很多方法,最后问了这个问题。正如wifi-direct中提到的许多文章一样,所有客户端都知道组所有者的IP,并且可以使用该IP发送消息,组所有者将保存客户端IP地址。但我不能像第一次发送的客户端那样从组所有者向客户端发送消息。我遇到了这个错误: 第一:

failed to connect to /192.168.49.24 (port 8988) after 5000ms: isConnected failed: 
EHOSTUNREACH (No route to host).

更改代码后: 第一个错误 + bind failed: EADDRINUSE (Address already in use). 我要检索的 AsyncTask :

    @Override
    protected String doInBackground(Void... params) {
        ServerSocket serverSocket = null;
        Socket client = null;
        DataInputStream inputstream = null;
        try {
            serverSocket = new ServerSocket(8988);
            client = serverSocket.accept();
            inputstream = new DataInputStream(client.getInputStream());
            String str = inputstream.readUTF();
            String IP = client.getInetAddress().toString();
            serverSocket.close();
            return IP+"+"+str;
        } catch (IOException e) {
            Log.e(WiFiDirectActivity.TAG, e.getMessage());
            return null;
        }finally{
            if(inputstream != null){
                try{
                    inputstream.close();
                } catch (IOException e) {
                    Log.e(WiFiDirectActivity.TAG, e.getMessage());
                }
            }
            if(client != null){
                try{
                    client.close();
                } catch (IOException e) {
                    Log.e(WiFiDirectActivity.TAG, e.getMessage());
                }
            }
            if(serverSocket != null){
                try{
                    serverSocket.close();
                } catch (IOException e) {
                    Log.e(WiFiDirectActivity.TAG, e.getMessage());
                }
            }
        }
}

和我的 IntentService 发送消息:

@Override
    protected void onHandleIntent(Intent intent) {

        Context context = getApplicationContext();
if (intent.getAction().equals(ACTION_SEND_IP)) {
            String host = intent.getExtras().getString(EXTRAS_GROUP_OWNER_ADDRESS);
            Log.e("DAVUD","Host:"+ host);

            Socket socket = new Socket();
            int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);
            Log.e("DAVUD","Port:"+ port);

            DataOutputStream stream = null;
            try {
                socket.connect((new InetSocketAddress(host, port)), SOCKET_TIMEOUT);
                stream = new DataOutputStream(socket.getOutputStream());
                String str = intent.getStringExtra("message");
                stream.writeUTF(str);
            } catch (IOException e) {
                Log.e(WiFiDirectActivity.TAG, e.getMessage());
            } finally {
                if (stream != null) {
                    try {
                        stream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (socket != null) {
                    if (socket.isConnected()) {
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

        }
    }

以及我测试的其他一些代码...还有另一个问题与此相同但没有回答(android-wifi-direct-how-to-send-data-from-group-owner-to-the-clients)这个项目基于 wifiDirectDemo Simple。请帮助我真的需要它。

【问题讨论】:

    标签: android sockets client wifi-direct


    【解决方案1】:

    一年后,我再次提出了我的问题。问题不在于 wifi 或连接。这是关于字符串解析的。 doInBackground 中的一行是:

    return IP+"+"+str
    

    onPostExecute 中,我从返回的字符串中解析并获取了 ip;但解析代码不正确。所以返回:

    192.168.49.24

    代替:

    192.168.49.241

    其中两个是有效的 ip 我不认为解析逻辑有问题。我更改了代码并使用String[] 而不是String

    【讨论】:

    • 我想你也会因为client.getInetAddress().toString()而遇到解析问题。 InetAddress.toString() 返回一个字符串“形式为:主机名/文字 IP 地址”(docs.oracle.com/javase/7/docs/api/java/net/…)。这就是错误消息failed to connect to /192.168.49.24 中有斜线的原因。最好使用client.getInetAddress().getHostAddress()
    • 至少那个“/”是可见的,不是大问题,最后不像这个“1”。
    猜你喜欢
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    相关资源
    最近更新 更多