java_udp编程
两个重要的类:
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/DatagramPacket.html
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/DatagramSocket.html
简单示例:
package netProgram; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; /** * 发送端 * 1.使用DatagramSocket 指定端口 创建发送端 * 2.准备数据 准成字节数组 * 3.封装成 DatagramPacket,需要指定目的地 * 4.发送包裹 send(DatagramPacket packet) * 5.释放资源 * */ public class UDPTest2 { public static void main(String[] args) throws Exception { DatagramSocket client = new DatagramSocket(8888);//自身的端口号 String data = "hello udp program!"; byte[] datas = data.getBytes(); //指定发往的目的地 DatagramPacket packet = new DatagramPacket (datas, 0, datas.length, new InetSocketAddress("localhost", 9999)); client.send(packet); client.close(); } }
package netProgram; import java.net.DatagramPacket; import java.net.DatagramSocket; /** * 接收端 * 1.使用DatagramSocket 指定端口 创建接收端 * 2.准备容器 封装成DatagramPacket * 3.阻塞式接收 packet receive(DatagramPacket packet) * 4.分析数据 byte[] getData() * 5.释放资源 * */ public class UDPTest { public static void main(String[] args) throws Exception { DatagramSocket server = new DatagramSocket(9999); byte[] container = new byte[64*1024]; DatagramPacket packet = new DatagramPacket(container, 0,container.length); server.receive(packet); byte[] data = packet.getData(); System.out.println(new String(data)); server.close(); } }
发送对象:关键是要转换成字节数组
package netProgram; import java.io.ByteArrayInputStream; import java.io.ObjectInputStream; import java.net.DatagramPacket; import java.net.DatagramSocket; /** * 接收端 1.使用DatagramSocket 指定端口 创建接收端 2.准备容器 封装成DatagramPacket 3.阻塞式接收 packet * receive(DatagramPacket packet) 4.分析数据 byte[] getData() 5.释放资源 */ public class UDPTest { public static void main(String[] args) throws Exception { DatagramSocket server = new DatagramSocket(9999); byte[] container = new byte[64 * 1024]; DatagramPacket packet = new DatagramPacket(container, 0, container.length); server.receive(packet); byte[] data = packet.getData(); ByteArrayInputStream stream = new ByteArrayInputStream(data, 0, data.length); ObjectInputStream ois = new ObjectInputStream(stream); Person temp = (Person) ois.readObject(); System.out.println(temp.getAge() + "--->" + temp.getName()); server.close(); } }
package netProgram; import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; /** * 发送端 * 1.使用DatagramSocket 指定端口 创建发送端 * 2.准备数据 准成字节数组 * 3.封装成 DatagramPacket,需要指定目的地 * 4.发送包裹 send(DatagramPacket packet) * 5.释放资源 * */ public class UDPTest2 { public static void main(String[] args) throws Exception { DatagramSocket client = new DatagramSocket(8888);//自身的端口号 //String data = "hello udp program!"; ByteArrayOutputStream stream = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(stream); oos.writeObject(new Person(15, "john")); //byte[] datas = data.getBytes(); byte[] datas = stream.toByteArray(); //指定发往的目的地 DatagramPacket packet = new DatagramPacket (datas, 0, datas.length, new InetSocketAddress("localhost", 9999)); client.send(packet); client.close(); } }