3

I am developing an Android multiplayer game using Wi-Fi direct. Using Wi-Fi direct, I am able to establish connection between peer devices. Also the code is able to send the data from client device to server device. (According to Android documentation, the Wi-Fi direct uses client-server model)

Problem:

I am not able to share the data from server device to client device using Wi-Fi direct.

I have following questions:

  1. Is there any other way to transfer data (bi-directional) between two Android devices which are connected through Wi-Fi Direct?
  2. During my online research I understood that to send data from server device to client device, server needs to know the client’s IP address. How to use this client’s IP address to send data from server device to client device? (I am able to fetch the client’s IP address)

I'd appreciate any suggestions on these queries. Thank you in advance.

Code: Server Side

    public  class DataTransferAsyncTask extends AsyncTask<Void,Void,String>{
    ServerSocket serverSocket;

    Socket client;
    InputStream inputstream;
    Context context = mActivity.getApplicationContext();
    String s;
    InetAddress client_add;

    @Override
    protected String doInBackground(Void... voids) {

        try{

             serverSocket = new ServerSocket(9999);
            Log.e("hello", "Server: Socket opened");
            client = serverSocket.accept();
            Log.e("hello", "Server: connection done");

            inputstream = client.getInputStream();
        //  OutputStream outputStream = serverSocket.getO

            //getting data from client
            byte[] address = new byte[12];
            if(client.isConnected())
            inputstream.read(address);

             s = new String(address);
            String only_add = new String();
            only_add = s.substring(0,12);

             client_add = InetAddress.getByName(only_add);

            Log.e("hello", "Server: clients ip 1 " + only_add);
            Log.e("hello", "Server: converted address 1 " + client_add + " \n is connected"+
                    client.isConnected());




            //send data to client

            OutputStream stream = client.getOutputStream();

             stream.write(s.getBytes());
            Log.e("hello","context value "+context);




        //  cancel(true);



        }catch (IOException e){

        }
        return null;
    }

}

Client Side:

@override
protected void onHandleIntent(Intent intent) {
    Log.e("hello","client socket");
    Toast.makeText(this,"client socket",Toast.LENGTH_LONG).show();
    Context context = getApplicationContext();
    if(intent.getAction().equals(action_send_data)){
        String host = intent.getStringExtra(group_owner_address);
        Socket socket = new Socket();
        int port = intent.getIntExtra(group_owner_port,9999);


        //binding connection
        try{

            String x="hello";
            Log.e("hello","opening client socket");
            byte[] address = getLocalAddress();
            String ipAdd = getDottedDecimalIP(address);

            socket.bind(null);
            socket.connect(new InetSocketAddress(host,port),socket_timeout);

            Log.e("hello","device socket address "+ socket.getInetAddress() );



            Log.e("hello","client socket is connected"+socket.isConnected());
            Log.e("hello","device address  :"+ipAdd + "  byte "+ address);

            //sending data to server
            OutputStream stream = socket.getOutputStream();

            stream.write(ipAdd.getBytes());


            //getting data from the server(supposed to)

            InputStream inputstream = socket.getInputStream();

            byte[] address_to_sto_fr_ser = new byte[15] ;
            inputstream.read(address_to_sto_fr_ser);

            String s = new String(address_to_sto_fr_ser);
            Log.e("msg from server","msg from server "+s);



          //  stream.close();
          //  is.close();


        }catch (IOException e){

        }
    }

}
Naresh
  • 433
  • 5
  • 14
  • The server can only send info to a client if the client is connected to the server. A server can only wait for clients to connect. – greenapps Dec 09 '16 at 08:53
  • No. The server does not need to know the ip of a client. If it knows the ip and the client is not connected then the server cannot use that ip to connect with that client. As servers cant connect. Only a client can connect to a server. – greenapps Dec 09 '16 at 08:56
  • But was is the problem? As soon as a client connects data can be send in two directions. – greenapps Dec 09 '16 at 08:57
  • `Thank you in advance.`. I hope you do better if you really appreciate comments. – greenapps Dec 09 '16 at 08:58
  • `not able to share the data from server device to client device`. Wrong problem description. You can. Once the client connected. – greenapps Dec 09 '16 at 09:00
  • @greenapps Thanks for the reply. Yes I know, once the client connected to the server they can share data, but is it applicable to Wi-Fi direct? – Naresh Dec 09 '16 at 09:43
  • Why do you think it is not? Of course they can exchange data. Your server-client communication code is the same for wifi and wifi-direct. Well it should be. – greenapps Dec 09 '16 at 10:03
  • @greenapps I have tried to transfer data from server to client device, but it is not sharing data. Can you please provide few links related to this? – Naresh Dec 09 '16 at 10:07
  • The code to send data from server to client is exactly the same as sending data from client to server. So what is your exact problem? `but it is not sharing data` ??? What do you mean? Cannot the server send the data? Or cannot the client receive the data? – greenapps Dec 09 '16 at 10:10
  • @greenapps Client device is not receiving the data. – Naresh Dec 09 '16 at 10:13
  • You have any idea what to do so this site can help you? Just telling that data is not received will not do i think. What do you think? And i wonder for what all the fuss of this post with all those difficult considerations. If only you should directly have said that your client could not receive data sent by server.... – greenapps Dec 09 '16 at 10:15
  • Ok. You posted code. Good. Now please tell your protocol. What should all happen? What happens ok and what goes wrong? Exceptions? Errors? – greenapps Dec 09 '16 at 10:29
  • @greenapps I haven't use any protocol. I have use the socket which (client)created in server side to accept data. There were no errors or exceptions, I have written a log statement right after client received data from the server, it is not printing on log screen. – Naresh Dec 09 '16 at 10:54
  • Of couse you have a protocol. What i see in your code is that when a client connects the client is the first one to send something. So the server waits for the client to send something. I see that the server expects 12 bytes exactly. If the server has received something it will reply to the client. At start the client sends its ip address in textual form. Now please continue with telling what happens exactly. Dont let us decode your code to find out. You should tell us first. After that we will check. – greenapps Dec 09 '16 at 11:40
  • @greenapps Yes server received the IP address of client, then I have used the socket which is at server end and send string to client device, but at the client end it is not receiving anything. – Naresh Dec 09 '16 at 12:10
  • We all knew that before. You have nothing added to what should happen. You did not explain the 12 bytes in `byte[] address = new byte[12];` on server side nor the 15 bytes in `byte[] address_to_sto_fr_ser = new byte[15] ;` on client side. You also did not tell that the server ends its life after writing something to the output stream as doInBouckground finishes. Please start explaining what should happen. And why you end doInBackground the way you do. I see all kind of problems but if you do not even manage to tell what you want or what happens i will not do that any more either. – greenapps Dec 09 '16 at 13:39
  • @greenapps no reason for you to be as rude as you were in this comment thread – trynacode Jul 21 '17 at 17:01

1 Answers1

7

The communication between client and WiFi Direct Group Owner is based on Socket server running on the Group Owner and clients connected to that server.

Once WiFi Direct group is formed(you can know that by checking "onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo)"), check if the current device is the Group Owner and if so, start the sockets server (similar to your code):

mServerSocket = new ServerSocket(mPort);
Log.e(getClass().getSimpleName(), "Running on port: " + mServerSocket.getLocalPort());

Then next, add this line to accept connections and store a reference of the client socket:

Socket mSocket = mServerSocket.accept();

Now, you have a reference of the client socket, you can use it to send data / messages to it. Next, the other device (client) should initiate a connection to the socket server:

mSocket = new Socket();
mSocket.bind(null);
mSocket.connect(new InetSocketAddress(mAddress, mPort), 500);

To send message from server to client:

DataOutputStream mDataOutputStream = new DataOutputStream(mSocket.getOutputStream());

Send simple message:

mDataOutputStream.writeUTF(message);
mDataOutputStream.flush();

Hope this helps.

JAD
  • 1,096
  • 12
  • 13