Server.java
package com.sxt.udp.object; import java.io.ByteArrayInputStream; import java.io.ObjectInputStream; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import com.sxt.entity.user.User; /* * UDP传输对象 */ //服务端 public class Server { public static void main(String[] args) throws Exception { System.out.println("这是服务端,已经准备就绪!"); //创建DatagramSocket 并指定端口 DatagramSocket ds = new DatagramSocket(8888); //创建byte为接收数据包做准备 byte[]buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, buf.length); //客户端DatagramSocket 接收 DatagramPacket数据包(字节) ds.receive(dp ); //将字节数组转换成对象(反序列化) //基类流 :内存流 读 ByteArrayInputStream bais = new ByteArrayInputStream(buf); //包装流 :对象流 ObjectInputStream ois = new ObjectInputStream(bais); //内存输入流 读取对象信息 Object object = ois.readObject(); //向下转型 获取对象信息 User user = (User)object; System.out.println("客户端的消息:"+user.getName()+" "+user.getAge()); //关socket ds.close(); //关流 bais.close(); ois.close(); //新建发送端对象 DatagramSocket ds1 = new DatagramSocket(); //往客户端发送的数据 String message = "你的消息我已经收到!"; //将String转成byte[]数组 为装数据包做准备 byte[] buf1 = message.getBytes(); DatagramPacket dp1 = new DatagramPacket(buf1, buf1.length, InetAddress.getLocalHost(), 9999); ds1.send(dp1 ); //关socket ds1.close(); } }
Client.java
package com.sxt.udp.object; import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import com.sxt.entity.user.User; //客户端 public class Client { public static void main(String[] args) throws Exception { //创建客户端的DatagramSocket对象 DatagramSocket ds = new DatagramSocket(); //发送的对象 User user = new User("张三", 18); //对象流 写 对象输出流 //内存流(基类流) ByteArrayOutputStream baos = new ByteArrayOutputStream(); //对象流(包装流 )因为传输对象 ObjectOutputStream oos = new ObjectOutputStream(baos); //将对象写入对象流 oos.writeObject(user); //将对象转化为字节数组 因为数据包需要字节数组 该方法属于字节流 很重要! byte[] buf = baos.toByteArray(); //将字节数组的数据放入数据包 DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getLocalHost(), 8888); //向客户端传输数据包 ds.send(dp ); //关流 ds.close(); //接收服务端的反馈消息(byte[] --> String) //创建接收端的Socket DatagramSocket ds1 = new DatagramSocket(9999); //新建byte[]数组为接收数组做准备 byte[]b1 = new byte[1024]; DatagramPacket dp1 = new DatagramPacket(b1, b1.length); //接收服务端的数据 ds1.receive(dp1); //从数据包取数据 byte[] data = dp1.getData(); int length = dp1.getLength(); //byte[] ---> String String message = new String(data, 0, length); System.out.println("服务端:"+message); //或者 System.out.println("服务端:"+new String(dp1.getData(),0,dp1.getLength())); //关流 ds1.close(); } }
User.java
package com.sxt.entity.user; import java.io.Serializable; /* * java.io.NotSerializableException * 所以传输对象时 对象必须序列化 * implements Serializable * 有默认序列号 * */ public class User implements Serializable{ private static final long serialVersionUID = 1L;//默认序列号 private String name; private int age; public User(String name, int age) { super(); this.name = name; this.age = age; } public User() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } }