4

Android WifiP2p API broadcast the group Owner address in WifiP2pInfo.groupOwnerAddress. Client can send data to group owner using this address. How can group owner send data back to clients? Where to assign or find out a client device's address?

windchime
  • 1,253
  • 16
  • 37
  • take a look at [this](http://stackoverflow.com/questions/16372724/wifi-direct-android) – Saeid Farivar Sep 25 '13 at 15:35
  • That is almost the same question as mine. But how can client find out its own IP address or GO assigns an ip address to a client. I can not find any means in WifiP2p API to do that. – windchime Sep 27 '13 at 13:10
  • There are different ways you can get that. after you connect to a group, your device will be assigned an ip. then you can get your ip same way as you get wifi ip address. you might not have an ip before that connecting to the group. also you can go to a harder way and let the GO send you back your ip address after you joined the group. – Saeid Farivar Sep 27 '13 at 17:09
  • which methods should I call to accomplish the ways you suggested? – windchime Sep 28 '13 at 22:24

1 Answers1

4

When a client sends data to Group Owner and Group Owner reads it through ServerSocket.serverSocket.accept() gives the socket of the client whose connection GroupOwner has accepted. From That socket, it gets Ip address of the client through s.getInetAddress().

ServerSocket serverSocket = new ServerSocket(8988);
Socket s = serverSocket.accept();  
InputStream is = s.getInputStream();  
Log.d("Client's InetAddress",""+s.getInetAddress());
ObjectInputStream ois = new ObjectInputStream(is);  
TestObject to = (TestObject)ois.readObject();  
is.close();  
s.close();  
serverSocket.close();
pioneerBhawna
  • 578
  • 7
  • 15