David
David

Reputation: 2179

how to send data from Group owner to client with wifi p2p

I tested many ways and finally asked this question. as many of articles mentioned in wifi-direct all clients know group owner's IP and can use this ip to send a message and group owner will save clients ip address. but I can't send a message from group owner to client like that client sent first time. I faced with this error's: first:

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

after change code: first error + bind failed: EADDRINUSE (Address already in use). My AsyncTask to retrieve :

    @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());
                }
            }
        }
}

and my IntentService to send messages:

@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();
                        }
                    }
                }
            }

        }
    }

and some other codes I tested... There is another question asked same this but not answered(android-wifi-direct-how-to-send-data-from-group-owner-to-the-clients) this project based on wifiDirectDemo Simple. Please help I really need it.

Upvotes: 3

Views: 1291

Answers (1)

David
David

Reputation: 2179

After one year I sew my question again. the problem was not about wifi or connection. it's about string parsing. where a line in doInBackground is:

return IP+"+"+str

and in onPostExecute I parsed and get ip from returned string; but parse code was not correct. so returns:

192.168.49.24

instead of:

192.168.49.241

where two of them is valid ips I am not thought parse logic had problem. I changed code and used String[] instead of String.

Upvotes: 1

Related Questions