Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

android - Peer to peer data sharing using Wi-Fi direct

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 + " 
 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){

        }
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...