1.一发一收
客户端
public static void main(String[] args) throws Exception { System.out.println("========客户端启动=============="); //创建发送端对象;发送端自带默认端口号 DatagramSocket socket=new DatagramSocket(); // 创建一个数据包对象封装数据 /** * DatagramPacket(byte buf[], int length, * InetAddress address, int port) * 参数一:封装要发送的数据 * 参数二:数据长度大小 * 参数三:接收端的IP地址 * 参数四:接收端的端口号 */ byte[] buf="你好".getBytes(); DatagramPacket packet=new DatagramPacket(buf,buf.length, InetAddress.getLocalHost(),8888); //发送数据出去 socket.send(packet); socket.close(); }
服务端
public class ServerDome { public static void main(String[] args) throws Exception { System.out.println("==========服务端启动========="); //1.创建接收端对象,注册端口 DatagramSocket socket=new DatagramSocket(8888); //2.创建一个数据包对象接收数据 byte[] buffer = new byte[1024*64]; DatagramPacket packet=new DatagramPacket(buffer,buffer.length); //3.等待接收数据 socket.receive(packet); //4.取出数据 String s = new String(buffer); System.out.println(s); //获取发送端的IP和端口 System.out.println(packet.getAddress()); System.out.println(packet.getPort()); socket.close(); } }
多发多收
客户端
public static void main(String[] args) throws Exception { System.out.println("========客户端启动=============="); //创建发送端对象;发送端自带默认端口号 DatagramSocket socket=new DatagramSocket(); // 创建一个数据包对象封装数据 Scanner sc=new Scanner(System.in); while (true) { System.out.println("请说"); String msg= sc.nextLine(); if ("exit".equals(msg)){ System.out.println("离线成功"); socket.close(); break; } byte[] buf=msg.getBytes(); DatagramPacket packet=new DatagramPacket(buf,buf.length, InetAddress.getLocalHost(),8888); //发送数据出去 socket.send(packet); } }
服务端
public static void main(String[] args) throws Exception { System.out.println("==========服务端启动========="); //1.创建接收端对象,注册端口 DatagramSocket socket=new DatagramSocket(8888); //2.创建一个数据包对象接收数据 byte[] buffer = new byte[1024*64]; DatagramPacket packet=new DatagramPacket(buffer,buffer.length); while (true) { //3.等待接收数据 socket.receive(packet); //4.取出数据 String s = new String(buffer); System.out.println("收到了来自"+packet.getAddress()+"的数据 "+s); } }
广播
客户端
public static void main(String[] args) throws Exception { System.out.println("========客户端启动=============="); //创建发送端对象;发送端自带默认端口号 DatagramSocket socket=new DatagramSocket(); // 创建一个数据包对象封装数据 Scanner sc=new Scanner(System.in); while (true) { System.out.println("请说"); String msg= sc.nextLine(); if ("exit".equals(msg)){ System.out.println("离线成功"); socket.close(); break; } byte[] buf=msg.getBytes(); DatagramPacket packet=new DatagramPacket(buf,buf.length, InetAddress.getByName("255.255.255.255"),9999); //发送数据出去 socket.send(packet); } }
服务端
public static void main(String[] args) throws Exception { System.out.println("==========服务端启动========="); //1.创建接收端对象,注册端口 DatagramSocket socket=new DatagramSocket(9999); //2.创建一个数据包对象接收数据 byte[] buffer = new byte[1024*64]; DatagramPacket packet=new DatagramPacket(buffer,buffer.length); while (true) { //3.等待接收数据 socket.receive(packet); //4.取出数据 String s = new String(buffer); System.out.println("收到了来自"+packet.getAddress()+"的数据 "+s); } }