كيف تحصل على عنوان IP الخاص بالعميل باستخدام Java؟
-
27-09-2019 - |
سؤال
كيف يمكنني الحصول على محرك IP الخاص بـ LAN IP لجهاز كمبيوتر باستخدام Java؟ أريد أن يكون IP-Address متصلاً بجهاز التوجيه وبقية الشبكة.
لقد جربت شيئًا كهذا:
Socket s = new Socket("www.google.com", 80);
String ip = s.getLocalAddress().getHostAddress();
s.close();
يبدو أن هذا يعمل في بعض الحالات ، ولكنه في بعض الأحيان يعيد راكب الاسترجاع أو شيء مختلف تمامًا. أيضا ، يتطلب اتصال الإنترنت.
هل حصل أي شخص على طريقة أكثر دقة للقيام بذلك؟
تعديل: اعتقد أنه سيكون من الأفضل أن نسأل هنا أكثر من تعليق ..
ماذا لو حصلت على العديد من الواجهات؟ على سبيل المثال ، واحد للكابل ، واحد ل wifi وواحد للمربع الافتراضي أو نحو ذلك. هل من المستحيل رؤية أي واحد متصل بالشبكة؟
المحلول
جرب java.net.networkinterface
import java.net.NetworkInterface;
...
for (
final Enumeration< NetworkInterface > interfaces =
NetworkInterface.getNetworkInterfaces( );
interfaces.hasMoreElements( );
)
{
final NetworkInterface cur = interfaces.nextElement( );
if ( cur.isLoopback( ) )
{
continue;
}
System.out.println( "interface " + cur.getName( ) );
for ( final InterfaceAddress addr : cur.getInterfaceAddresses( ) )
{
final InetAddress inet_addr = addr.getAddress( );
if ( !( inet_addr instanceof Inet4Address ) )
{
continue;
}
System.out.println(
" address: " + inet_addr.getHostAddress( ) +
"/" + addr.getNetworkPrefixLength( )
);
System.out.println(
" broadcast address: " +
addr.getBroadcast( ).getHostAddress( )
);
}
}
نصائح أخرى
في البداية: لا يوجد عنوان واحد. يحتوي جهازك على اثنين على الأقل من العوامل (127.0.0.1 على "LO" وربما 192.168.1.1 على "eth1").
انت تريد هذا: إدراج واجهات الشبكة
كما قد تتوقع ، لا يمكنك اكتشاف أي شخص متصل بأي من أجهزة التوجيه الخاصة بك تلقائيًا ، نظرًا لأن هذا يحتاج إلى تحليل معقد لجداول التوجيه الخاصة بك. ولكن إذا كنت تريد فقط أي عنوان غير محلي ، فيجب أن يكون ذلك كافيًا. من المؤكد ، حاول استخدام هذا مرة واحدة على الأقل على Vista أو Windows 7 ، لأنها تضيف عناوين IPv6.
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;
public class ListNets
{
public static void main(String args[]) throws SocketException {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
displayInterfaceInformation(netint);
}
static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
out.printf("Display name: %s\n", netint.getDisplayName());
out.printf("Name: %s\n", netint.getName());
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
out.printf("InetAddress: %s\n", inetAddress);
}
out.printf("\n");
}
}
فيما يلي عينة إخراج من برنامج المثال:
Display name: bge0
Name: bge0
InetAddress: /fe80:0:0:0:203:baff:fef2:e99d%2
InetAddress: /121.153.225.59
Display name: lo0
Name: lo0
InetAddress: /0:0:0:0:0:0:0:1%1
InetAddress: /127.0.0.1
هذه طريقة استخدمتها لفترة من الوقت. ويشمل القليل من الاختراق لمعرفة address مرئية خارجيا كذلك.
private List<String> getLocalHostAddresses() {
List<String> addresses = new ArrayList<String>();
try {
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface ni = e.nextElement();
Enumeration<InetAddress> e2 = ni.getInetAddresses();
while (e2.hasMoreElements())
addresses.add(e2.nextElement().getHostAddress());
}
URL u = new URL("http://whatismyip.org");
BufferedReader in = new BufferedReader(new InputStreamReader(
u.openStream()));
addresses.add(in.readLine());
in.close();
} catch (Exception ignore) {
}
return addresses;
}
try {
InetAddress addr = InetAddress.getLocalHost();
// Get IP Address
byte[] ipAddr = addr.getAddress();
// Get hostname
String hostname = addr.getHostName();
} catch (UnknownHostException e) {
}
كما أشار دانيال بالفعل ، لا يمكنك معرفة الواجهة التي "متصلة". ماذا لو ، على سبيل المثال ، يحتوي الكمبيوتر على بطاقات واجهة شبكة متعددة متصلة على شبكات LAN المادية المنفصلة؟
دع المستخدم يقرر واجهة لاستخدامها أو تجربتها جميعًا ، اعتمادًا على حالة الاستخدام الخاصة بك.