InetAddress类:
表示互联网中的IP地址,示例:
package demo; import java.net.InetAddress; import java.net.UnknownHostException; public class InetAddressDemo { public static void main(String[] args) throws UnknownHostException { function1(); function2(); } public static void function1() throws UnknownHostException { InetAddress inet = InetAddress.getLocalHost(); System.out.println(inet.getHostName());// 获取主机名 // 输出:DESKTOP-Q3O8AEO System.out.println(inet.getHostAddress());// 获取主机IP // 输出:192.168.87.1 } public static void function2() throws UnknownHostException { // 获得其他IP InetAddress inet = InetAddress.getByName("www.baidu.com"); System.out.println(inet); // 输出:www.baidu.com/111.13.100.92 } }
UDP协议:无连接通信协议,最大64KB,不安全
TCP协议:面向连接通信协议,可靠安全,三次握手以确认连接,相对速度慢,适合大数据传输
UDP示例:
发送端:
package demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPSend { public static void main(String[] args) { try { byte[] data = "你好".getBytes(); InetAddress inet = InetAddress.getByName("127.0.0.1"); DatagramPacket dp = new DatagramPacket(data, data.length, inet, 6000); DatagramSocket ds = new DatagramSocket(); ds.send(dp); ds.close(); } catch (IOException ex) { System.out.println(ex); } } }
接收端:
package demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceive { public static void main(String[] args) { try { DatagramSocket ds = new DatagramSocket(6000); byte[] data = new byte[1024]; DatagramPacket dp = new DatagramPacket(data, data.length); ds.receive(dp); int length = dp.getLength(); String ip = dp.getAddress().getHostAddress(); System.out.println(ip + ":" + new String(data, 0, length)); ds.close(); } catch (IOException ex) { System.out.println(ex); } } }
先运行接收端,再运行发送端,结果:
输出:127.0.0.1:你好
功能可以提升,实现键盘输入的聊天:
发送端:
package demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Scanner; public class UDPSend { public static void main(String[] args) { try { Scanner sc = new Scanner(System.in); DatagramSocket ds = new DatagramSocket(); InetAddress inet = InetAddress.getByName("127.0.0.1"); while (true) { String message = sc.nextLine(); byte[] data = message.getBytes(); DatagramPacket dp = new DatagramPacket(data, data.length, inet, 7000); ds.send(dp); } } catch (IOException ex) { System.out.println(ex); } } }
接收端:
package demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceive { public static void main(String[] args) { try { DatagramSocket ds = new DatagramSocket(7000); byte[] data = new byte[1024]; while (true) { DatagramPacket dp = new DatagramPacket(data, data.length); ds.receive(dp); int length = dp.getLength(); String ip = dp.getAddress().getHostAddress(); System.out.println(ip + ":" + new String(data, 0, length)); } } catch (IOException ex) { System.out.println(ex); } } }
先后运行,在发送端发送的消息可以随时在接收端收到,这里就实现了一个简单的聊天功能
同个局域网下的多台电脑之间也可以聊天,只要修改成相应的IP地址即可