Java-Tcp程序编写(获取本机的IP和主机名)

package test;

import java.net.InetAddress;

public class Address{
	public static void main(String[] args) {
		InetAddress ip;
		try {
			ip = InetAddress.getLocalHost();			//获取本机的ip对象
			String localname = ip.getHostName();		//获取本机的机器名
			String localip = ip.getHostAddress();		//获取本机的ip
			System.out.println("本机名是:"+localname);
			System.out.println("本机ip是:"+localip);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Java-Tcp程序编写(获取本机的IP和主机名)_第1张图片

你可能感兴趣的:(java)