标签:nbsp 案例 packet etl except 简单 调用 this .data
package UDP; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; //接收端 public class TestUdpR { public static void main(String[] args) throws IOException { //建立UDP的服务 DatagramSocket socket = new DatagramSocket(9000); //准备空数据包用于存储数据 byte buf[] = new byte[1024]; DatagramPacket dataPacket = new DatagramPacket(buf, buf.length); //调用UDP服务接收数据 socket.receive(dataPacket); System.out.println(new String(buf, 0, dataPacket.getLength()));//getLength()获取接收数据的长度 //关闭资源 socket.close(); } }
package UDP; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; //发送端 public class TestUdpT { public static void main(String[] args) throws IOException { //建立UDP的服务 DatagramSocket dgs = new DatagramSocket(); //准备数据,把数据封装到数据包中 String data = "This is UDP Packet"; //创建一个数据包 DatagramPacket packet = new DatagramPacket(data.getBytes(), data.getBytes().length,InetAddress.getLocalHost(),9000); //调用UDP的服务发送数据 dgs.send(packet); //关闭资源 dgs.close(); } }
标签:nbsp 案例 packet etl except 简单 调用 this .data
原文地址:https://www.cnblogs.com/xiaotao520/p/9194842.html