构造方法:
1.使用构造方法 public ServerSocket(int port) 和public ServerSocket(int port,int backlog)
创建ServerSocket对象,则客户端可以使用服务器任意的IP连接到ServerSocket对象中。
2. 使用public ServeSocket(int port,int backlog,InetAddress bindAddr)构造方法中的参数bindAddr创建ServerSocket对象后,
客户端想要连接到服务端,则客户端Socket的构造方法的参数要写上与ServerSocket构造方法的参数bindAddr相同的IP地址,
不然就会出现异常。
绑定指定的Socket地址:
public void bind(SocketAddress endpoint)方法的主要作用是将ServerSocket绑定到特定的Socket地址(IP地址和端口号),
使用这个地址与客户端进行通信。如果地址为null,则系统将挑选一个临时的端口和一个有效的本地地址来绑定套接字。
SocketAddress本身是一个抽象类,代表Socket地址;而InetAdress类代表IP地址。
InetAddress构造方法:
1.public InetSocketAddress(int port)的作用是创建套接字地址,其中IP地址为通配符地址,端口号为指定值。有效端口0~65535之间;
端口号传入0代表bind操作中随机挑选空闲的端口。
2:public InetSocketAddress(String hostname,int port的作用是根据主机名和端口号创建套接字地址。
3:public InetSocketAddress(InetAddress addr,int port)的作用根据IP地址和端口号创建套接字地址。
Server.java
package com.company.s8; import java.io.IOException; import java.net.InetSocketAddress; import java.net.ServerSocket; public class Server { public static void main(String[] args) { try { ServerSocket serverSocket=new ServerSocket(); serverSocket.bind(new InetSocketAddress(8888)); System.out.println("server begin accept"); serverSocket.accept(); System.out.println("server end accept"); }catch (IOException e){ e.printStackTrace(); System.out.println("catch "+System.currentTimeMillis()); } } }
Client.java
package com.company.s8; import java.net.Socket; public class Client { public static void main(String[] args) { try { System.out.println("client request begin"); Socket socket=new Socket("localhost",8888); System.out.println("client request end"); }catch (Exception e){ e.printStackTrace(); } } }
getLocalSocketAddress()获取bending的SocketAddress对象,返回此Socket绑定的端点的地址,如果尚未绑定,则返回null.
getLocalPort()方法用来获取Socket绑定到本地的端口。
package com.company.s12; import java.io.IOException; import java.net.InetSocketAddress; import java.net.ServerSocket; public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket=new ServerSocket(); System.out.println("new ServerSocket 无参构造的端口是:"+serverSocket.getLocalPort()); //windows再dos窗口ipconfig获取 serverSocket.bind(new InetSocketAddress("192.168.0.101",8888)); System.out.println("bind方法之后的端口是:"+serverSocket.getLocalPort()); InetSocketAddress inetSocketAddress=(InetSocketAddress) serverSocket.getLocalSocketAddress(); System.out.println("inetSocketAddress.getHostName="+inetSocketAddress.getHostName()); System.out.println("inetSocketAddress.getHostString="+inetSocketAddress.getHostString()); System.out.println("inetSocketAddress.getPort="+inetSocketAddress.getPort()); serverSocket.close(); } }
SocketAddress与InetAddress本质的区别是SocketAddress不基于任何协议。
Server.java
package com.company.s12_1; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket=new ServerSocket(); InetAddress inetAddress=InetAddress.getByName("localhost"); InetSocketAddress inetSocketAddress=new InetSocketAddress(inetAddress,8888); serverSocket.bind(inetSocketAddress); System.out.println("server begin"); Socket socket=serverSocket.accept(); System.out.println("server end"); socket.close(); serverSocket.close(); } }
Client.java
package com.company.s12_1; import java.io.IOException; import java.net.Socket; public class Client { public static void main(String[] args) throws IOException { System.out.println("client begin"); Socket socket=new Socket("localhost",8888); System.out.println("client end"); } }
getHostName()和getHostString()方法的区别
getHostName()方法的作用是获取主机名。注意,如果地址是字面IP地址创建的,则此方法可能触发名称服务反向查找,也就是DNS服务通过IP找到域名。
getHostString()方法的作用是返回住居名或地址的字符串形式,如果它没有主机名,则返回IP地址。这样做的好处是不尝试返回查找。
package com.company.s12_2; import java.net.InetSocketAddress; public class Server { public static void main(String[] args) { InetSocketAddress inetSocketAddress1=new InetSocketAddress("192.168.0.101",80); InetSocketAddress inetSocketAddress2=new InetSocketAddress("192.168.0.101",80); System.out.println(inetSocketAddress1.getHostName()); System.out.println(inetSocketAddress2.getHostString()); } }
测试结果:
原文:https://www.cnblogs.com/guoyansi19900907/p/11117188.html