(1)、InetAddress类
InetAddress的实例对象包含以数字形式保存的IP地址,同时还可能包含主机名(如果使用主机名来获取InetAddress的实例,或者使用数字来构造,并且启用了反向主机名解析的功能)。InetAddress类提供了将主机名解析为IP地址(或反之)的方法。
程序:
public static void main(String[]args) throws Exception{
InetAddress local = InetAddress.getLocalHost();
InetAddress baidu = InetAddress.getByName("www.baidu.com");
System.out.println("本机IP:"+local.getHostAddress());
System.out.println("百度IP:"+baidu.getHostAddress());
}
(2)、UDP通信
DatgramPacket类:用于封装UDP对象
在码头发送和接收货物时都需要使用集装箱来装载货物,UDP通信也是一样,发送和接收的数据也需要使用“集装箱”进行打包,为此JDK中提供了一个DatagramPacket类,该类的实例对象就相当于一个集装箱,用于封装UDP通信中发送或者接收的数据。
构造方法
常用方法
DatagramSocket类:用于发送和接收DatagramPacket对象
(1)、构造方法
常用方法
程序代码:
public static class Receive {
public static void main(String[] args) throws Exception {
byte[] by = new byte[1024];
DatagramSocket ds = new DatagramSocket(60000);
DatagramPacket dp = new DatagramPacket(by, 1024);
System.out.println("等待接收数据!");
ds.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength()) + "\tfrom" + dp.getAddress().getHostAddress() + ":" + dp.getPort();
System.out.println(str);
ds.close();
}
}
public static class Put{
public static void main(String[]args) throws Exception {
DatagramSocket ds = new DatagramSocket();
String str = "Hello!";
ds.send(new DatagramPacket(str.getBytes(),str.getBytes().length,InetAddress.getLocalHost(),60000));
ds.close();
}
}
(3)、TCP通信
ServerSocket类:用于构建服务端程序
构造方法
public ServerSocket(int port)
创建绑定到特定端口的服务器套接字。
public ServerSocket(int port, int backlog)
利用指定的 backlog(服务器忙时的等待数量)创建服务器套接字并将其绑定到指定的本地端口号。
public ServerSocket(int port, int backlog, InetAddress address)
使用指定的端口、侦听 backlog 和要绑定到的本地 IP 地址创建服务器。
public ServerSocket()
创建非绑定服务器套接字。
常用方法
public int getLocalPort()
返回此套接字在其上侦听的端口。
public Socket accept()
侦听并接受到此套接字的连接。
public void setSoTimeout(int timeout)
通过指定超时值启用/禁用 SO_TIMEOUT,以毫秒为单位。
public void bind(SocketAddress host, int backlog)
将 ServerSocket 绑定到特定地址(IP 地址和端口号)。
Socket类:代表客户端和服务器都用来互相沟通的套接字。客户端要获取一个 Socket 对象通过实例化 ,而 服务器获得一个 Socket 对象则通过 accept() 方法的返回值。
构造方法
public Socket()
未连接任何服务器,需要调用connect(SocketAddress endpoint),才能与服务器简历连接
public Socket(String host, int port) throws UnknownHostException, IOException
创建一个流套接字并将其连接到指定主机上的指定端口号。
常用方法
public int getPort()
返回此套接字连接到的远程端口。
public int getLocalPort()
返回此套接字绑定到的本地端口。
public InputStream getInputStream()
返回此套接字的输入流。
public OutputStream getOutputStream()
返回此套接字的输出流。
public void close()
关闭此套接字。
程序代码:
public static class Server {
public static void main(String[] args) throws Exception {
ServerSocket s = new ServerSocket(1024);
Socket accept = s.accept();
InputStream in = accept.getInputStream();
byte[] bytes = new byte[1024];
int such = 0;
while ((such = in.read(bytes)) > 0) {
System.out.println(new String(bytes, 0, such));
}
in.close();
}
}
public static class Client {
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getLocalHost(), 1024);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("我爱学习".getBytes());
outputStream.close();
}
}