java中端口的代码
import java.net.InetSocketAddress;
/*
* 端口的作用
* 1.用来区分软件
* 2.2个字节 0-65535
* UDP和TCP是独立的,端口互不影响,但是在同一个协议下端口不可以冲突
* 3.定义端口越大越好
* new InetSocketAddress(域名|地址,端口);
*/
public class PortTest {
public static void main(String[] args) {
InetSocketAddress sockerAddress=new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress sockerAddress2=new InetSocketAddress("localhost", 8080);
System.out.println(sockerAddress.getHostName());//获取主机名,一般通过InetAddress去拿
System.out.println(sockerAddress2.getAddress());//获取地址
System.out.println(sockerAddress.getPort());//获取端口
}
}