1

リモートホストのIPアドレスを取得する必要があります。私は次のことを試しましたが、正常に動作します。

socket = factory.createSocket(hostName, port);  
InetAddress remoteIP = socket.getInetAddress();
String[] remoteIPOnly = remoteIP.toString().split("\\/");
System.out.println("Remote IP is: "+remoteIPOnly[1]);

ただし、ポート番号を指定する必要がない方法が必要です。つまり、ポート番号に関係なく、リモートホストのIPが必要です。これは可能ですか?そもそもソケットを作成せずにIPを取得することは可能ですか?

4

2 に答える 2

2

これを試して:

InetAddress inetAddress = InetAddress.getByName("www.google.com");
byte[] raw = inetAddress.getAddress();

バイト配列には、IPアドレスバイトが含まれるようになりました。

于 2012-11-19T06:43:06.320 に答える
0

以下のように使用getHostAddress()します。

    InetAddress inetAddress = InetAddress.getByName("www.google.com");
    String ipAddress = inetAddress.getHostAddress();
    System.out.println(ipAddress );//prints 66.152.109.61
于 2012-11-19T06:56:19.390 に答える