Java学习之路--网络编程相关01

package com.kuang.lesson01;

import java.net.InetAddress;
import java.net.UnknownHostException;

//2023.2.28/3.1 Java 狂神说-网络编程实战-IP地址
public class TestnetAddress {
public static void main(String[] args) {
//测试ip
try {
//查询本机地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
InetAddress inetAddress2 = InetAddress.getByName("localhost");
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println(inetAddress1);
System.out.println(inetAddress2);
System.out.println(inetAddress3);

//查询网站ip地址
InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");
InetAddress inetAddress5 = InetAddress.getByName("www.alibaba.com");
InetAddress inetAddress6 = InetAddress.getByName("www.tencent.com");
System.out.println(inetAddress4);
System.out.println(inetAddress5);
System.out.println(inetAddress6);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}

//
package com.kuang.lesson01;

import java.net.InetSocketAddress;

//2023.3.2/3/4 狂神说java--网络编程--端口Port
public class TestSocketAddress {
public static void main(String[] args) {
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",8080);
InetSocketAddress socketAddress1 = new InetSocketAddress("localhost",8080);
System.out.println(socketAddress);
System.out.println(socketAddress1);

System.out.println(socketAddress.getAddress());
System.out.println(socketAddress1.getAddress());//地址
System.out.println(socketAddress.getPort());//端口
}
}
posted @ 2023-09-20 16:14  寂灭无言  阅读(10)  评论(0)    收藏  举报