In this example, we retrieve the name and address of the machine in a local wifi network provided by the router's DHCP server:
import java.net.InetAddress;Output:
import java.net.UnknownHostException;
public class Clientaddress {
public static void main(String[] args) {
String hostname;
String addressIPLocale;
try{
InetAddress inetadr = InetAddress.getLocalHost();
//machinename
hostname = (String) inetadr.getHostName();
System.out.println("Machine Name = "+HostName);
//ip address on the network
IPLocale address = (String) inetadr.getHostAddress();
System.out.println("Local IP address = "+IPlOcale)
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
Machine Name = VAIO-VAIOReferences:
Local IP Address = 192.168.1.101
Oracle Documentation: Method InetAddress.getLocalHost
Retrieve web server address in Java
TCP Socket: Create a Client/Server chat in Java
Commentaires (0)
Laisser un commentaire
Connectez-vous pour commenter
Rejoignez la discussion et partagez vos connaissances avec la communauté
Chargement des commentaires...