1. Download our Official Android App: Forums for Android!

How to get peer ip address on p2p chat

Discussion in 'Android Development' started by Setnov, Oct 4, 2017.

  1. Setnov

    Setnov Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Oct 4, 2017

    Oct 4, 2017
    1
    0
    5
    Hi guys

    I am creating p2p chat app. i want to get the peer ip addresses if it's available. I manage to get local ip address on Fig 1, However i dont know how to get other devices ip address. Could anyone help me what's wrong with Fig 2. Or any idea how to do it?

    Thanks

    Fig 1
    Code (Text):
    1.  
    2. public  String getLocalIpAddress() {
    3.     WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(android.content.Context.WIFI_SERVICE);
    4.     WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    5.     int ipAddress = wifiInfo.getIpAddress();
    6.     try {
    7.         return InetAddress.getByName(String.format("%d.%d.%d.%d",
    8.                 (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
    9.                 (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff))).toString();
    10.     } catch (UnknownHostException e) {
    11.         e.printStackTrace();
    12.     }
    13.     return null;
    14. }
    15.  


    Fig 2
    Code (Text):
    1.  
    2.  public String getClientIpAddress() {
    3.  
    4. WifiManager wifiManager = (WifiManager) connectionSocket.getRemoteSocketAddress(android.content.Context.WIFI_SERVICE);
    5. WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    6. int ipAddress = wifiInfo.getIpAddress();
    7. try {
    8. return InetAddress.getByName(String.format("%d.%d.%d.%d",
    9.                     (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
    10. (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff))).toString();
    11. } catch (UnknownHostException e) {
    12. e.printStackTrace();
    13. }
    14. return null;
    15.  
     

    Advertisement

    #1 Setnov, Oct 4, 2017
    Last edited by a moderator: Oct 4, 2017
  2. Unforgiven

    Unforgiven OK Google
    Moderator
    Rank:
     #1
    Points:
    4,238
    Posts:
    36,013
    Joined:
    Jun 23, 2010

    Jun 23, 2010
    36,013
    41,596
    4,238
    Male
    Douglas, MA
    Development thread moved to the dev area for better exposure.:)
     
  3. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,417
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,417
    10,688
    1,988
    Male
    Software developer
    South West of England
    Code tags added to improve readability of code
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,417
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,417
    10,688
    1,988
    Male
    Software developer
    South West of England
    What happens when this code runs?
     

Share This Page

Loading...