UDP通信
UDP通信原理图解
案例:UDP数据传输
实现步骤:
发送Send
创建DatagramSocket, 随机端口号
创建DatagramPacket, 指定数据, 长度, 地址, 端口
使用DatagramSocket发送DatagramPacket
关闭DatagramSocket
接收Receive
创建DatagramSocket, 指定端口号
创建DatagramPacket, 指定数组, 长度
使用DatagramSocket接收DatagramPacket
关闭DatagramSocket
从DatagramPacket中获取数据
案例:实现UDP的发送和接收
public static void main(String[] args) throws Exception { // 1.发送Send // 创建DatagramSocket, 随机端口号 DatagramSocket sender = new DatagramSocket(); // 创建DatagramPacket【数据包】, 指定数据, 长度, 地址, 端口 String s = "你好吗?最近很想你"; byte[] bs = s.getBytes("UTF-8"); DatagramPacket packet = new DatagramPacket(bs, bs.length, Inet4Address.getByName("192.168.1.125"), 5288); // 使用DatagramSocket发送DatagramPacket sender.send(packet); // 关闭DatagramSockets sender.close(); }
public static void main(String[] args) throws IOException { // 2.接收Receive // 创建DatagramSocket, 指定端口号 DatagramSocket receiver = new DatagramSocket(5288); // 创建DatagramPacket, 指定数组, 长度 byte[] buf = new byte[1024]; DatagramPacket packet = new DatagramPacket(buf, 1024); // 使用DatagramSocket接收DatagramPacket receiver.receive(packet); // 关闭DatagramSocket receiver.close(); // 从PackeDatagramt中获取数据 String s = new String(buf, 0, packet.getLength(), "UTF-8"); System.out.println("接收到数据:" + s); }
案例:添加键盘和死循环功能
public class Demo_Sender { public static void main(String[] args) throws Exception { // 1.发送Send // 创建DatagramSocket, 随机端口号 DatagramSocket sender = new DatagramSocket(); //2 Scanner Scanner scanner = new Scanner(System.in); while(true){ // 创建DatagramPacket【数据包】, 指定数据, 长度, 地址, 端口 System.out.println("输入内容:"); String s = scanner.nextLine(); byte[] bs = s.getBytes("UTF-8"); DatagramPacket packet = new DatagramPacket(bs, bs.length, Inet4Address.getByName("192.168.1.125"), 5288); // 使用DatagramSocket发送DatagramPacket sender.send(packet); } // 关闭DatagramSockets //sender.close(); } } package lesson05; public class Demo_Receiver { public static void main(String[] args) throws IOException { // 2.接收Receive // 创建DatagramSocket, 指定端口号 DatagramSocket receiver = new DatagramSocket(5288); while(true){ // 创建DatagramPacket, 指定数组, 长度 byte[] buf = new byte[1024]; DatagramPacket packet = new DatagramPacket(buf, 1024); // 使用DatagramSocket接收DatagramPacket receiver.receive(packet); // 关闭DatagramSocket //receiver.close(); // 从PackeDatagramt中获取数据 String s = new String(buf, 0, packet.getLength(), "UTF-8"); System.out.println(new Date() + "接收到数据:" + s); } } }
案例:使用双线程来收发数据
public class Demo { public static void main(String[] args) { // 案例:UDP数据传输+键盘和循环+双线程实现(了解) new Receiver().start(); new Sender().start(); } } class Receiver extends Thread { @Override public void run() { // TODO Auto-generated method stub try { // 创建DatagramSocket, 指定端口号 DatagramSocket receiver = new DatagramSocket(5288); while (true) { // 创建DatagramPacket, 指定数组, 长度 byte[] buf = new byte[1024]; DatagramPacket packet = new DatagramPacket(buf, 1024); // 使用DatagramSocket接收DatagramPacket receiver.receive(packet); // 关闭DatagramSocket // receiver.close(); // 从PackeDatagramt中获取数据 String s = new String(buf, 0, packet.getLength(), "UTF-8"); System.out.println(new Date() + "接收到数据:" + s); } } catch (Exception e) { // TODO: handle exception } } } class Sender extends Thread { @Override public void run() { try { // 创建DatagramSocket, 随机端口号 DatagramSocket sender = new DatagramSocket(); //2 Scanner Scanner scanner = new Scanner(System.in); while(true){ // 创建DatagramPacket【数据包】, 指定数据, 长度, 地址, 端口 System.out.println("输入内容:"); String s = scanner.nextLine(); byte[] bs = s.getBytes("UTF-8"); DatagramPacket packet = new DatagramPacket(bs, bs.length, Inet4Address.getByName("192.168.1.125"), 5288); // 使用DatagramSocket发送DatagramPacket sender.send(packet); } } catch (Exception e) { // TODO: handle exception } } }