public class MenuBarDemo {public static void main(String[] args) throws Exception{byte[] bytes = "你好".getBytes(StandardCharsets.UTF_8);DatagramPacket dp = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("127.0.0.1"),10000);DatagramSocket ds = new DatagramSocket();ds.send(dp);}
}
public static void main(String[] args)throws Exception {byte[] bytes1 = new byte[1024];//创建数据包对象,接收数组DatagramPacket dap = new DatagramPacket(bytes1,bytes1.length);DatagramSocket das = new DatagramSocket(10000);das.receive(dap);//拆解数据包int lengh = dap.getLength();int port = dap.getPort();InetAddress i = dap.getAddress();String ip = i.getHostAddress();System.out.println(new String(bytes1,0,lengh)+"..."+port+ip);}