java - 在 UDP 上发送和接收序列化对象

标签 java serialization udp

我正在尝试使用 UDP 将序列化对象从服务器进程发送到 Java 中的客户端进程。问题是客户端在接收方法上被阻塞。有人可以帮忙吗?!

这里是发送对象的服务器代码:

  ClientModel C1= new ClientModel(100,"Noor","Noor",38,38,"asd");
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  ObjectOutputStream oos = new ObjectOutputStream(baos);
  oos.writeObject(C1);
  oos.flush();
  byte[] Buf= baos.toByteArray();
  packet = new DatagramPacket(Buf, Buf.length, client, port);
  socket.send(packet);

这是接收对象的客户端代码:

byte[] buffer = new byte[100000];
packet = new DatagramPacket(buffer, buffer.length );
socket.receive(packet);
System.out.println("packet received");

我只想接收能够重建的对象,但我无法接收数据包本身。

最佳答案

我不知道你最终想要完成什么,但是使用 UDP 并不是那么容易...主要原因在 DatagramPacket 对象的描述中:

Datagram packets are used to implement a connectionless packet delivery service. Each message is routed from one machine to another based solely on information contained within that packet. Multiple packets sent from one machine to another might be routed differently, and might arrive in any order. Packet delivery is not guaranteed.

使用 udp 的一个很好的教程是 http://download.oracle.com/javase/tutorial/networking/datagrams/clientServer.html

关于您的屏蔽:

Receives a datagram packet from this socket. When this method returns, the DatagramPacket's buffer is filled with the data received. The datagram packet also contains the sender's IP address, and the port number on the sender's machine.

This method blocks until a datagram is received. The length field of the datagram packet object contains the length of the received message. If the message is longer than the packet's length, the message is truncated.

我没有真正测试它,但我很确定 - 根据描述 - datagramsocket.reseive 函数将阻塞直到数据包被填满(在你的情况下直到收到 100000 个字节)。

我建议您从具有固定已知长度的数据报包开始,在其中传输实际负载的大小。像这样的东西:

public static void main(String[] args) {
    ClientModel c1 = new ClientModel ();
    c1.data = 123;
    c1.name = "test";

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(baos);
      oos.writeObject(c1);
      oos.flush();
      // get the byte array of the object
      byte[] Buf= baos.toByteArray();

      int number = Buf.length;;
      byte[] data = new byte[4];

      // int -> byte[]
      for (int i = 0; i < 4; ++i) {
          int shift = i << 3; // i * 8
          data[3-i] = (byte)((number & (0xff << shift)) >>> shift);
      }

      DatagramSocket socket = new DatagramSocket(1233);
      InetAddress client = InetAddress.getByName("localhost");
      DatagramPacket packet = new DatagramPacket(data, 4, client, 1234);
      socket.send(packet);

      // now send the payload
      packet = new DatagramPacket(Buf, Buf.length, client, 1234);
      socket.send(packet);

      System.out.println("DONE SENDING");
    } catch(Exception e) {
        e.printStackTrace();
    }
}

另一方面,您现在知道自己的尺码了:

public static void main(String[] args) {
    try {
      DatagramSocket socket = new DatagramSocket(1234);

      byte[] data = new byte[4];
      DatagramPacket packet = new DatagramPacket(data, data.length );
      socket.receive(packet);

      int len = 0;
      // byte[] -> int
      for (int i = 0; i < 4; ++i) {
          len |= (data[3-i] & 0xff) << (i << 3);
      }

      // now we know the length of the payload
      byte[] buffer = new byte[len];
      packet = new DatagramPacket(buffer, buffer.length );
      socket.receive(packet);

        ByteArrayInputStream baos = new ByteArrayInputStream(buffer);
      ObjectInputStream oos = new ObjectInputStream(baos);
      ClientModel c1 = (ClientModel)oos.readObject();
      c1.print();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

我使用的 CientModel 类:

public class ClientModel implements Serializable{
    private static final long serialVersionUID = -4507489610617393544L;

    String name = "";
    int data = 1;

    void print() {
        System.out.println(data +": " + name);
    }
}

我测试了这段代码,它工作得很好。希望有帮助(我从 http://www.tutorials.de/java/228129-konvertierung-von-integer-byte-array.html 得到了 byte-To-int 和周围)

编辑:如评论中所述,使用 UDP 通常是一个非常糟糕的主意,主要是因为您不知道您的数据包是否以正确的顺序接收,甚至根本不知道。 UDP 不保证这一点。我没有做太多的 udp 编程,但你唯一可以依赖的部分(如果我理解正确的话)是,如果你得到一个数据包并且它适合数据报(65,527 字节 - 请参阅 https://en.wikipedia.org/wiki/User_Datagram_Protocol)它将包含整个东西。因此,如果您不关心消息的顺序以及您的对象适合数据报的顺序,您应该没问题。

Edit2:至于代码:不要按原样使用它。这只是一个例子,在 UDP 中你应该只有一种类型的数据包,而且这个数据包的大小是已知的。这样你就不需要发送“大小”。如果您使用如上所示的代码,并且丢弃了一个数据包,则下一个数据包的大小将是错误的(即第一个数据包被丢弃,突然您正在检查有效负载的第一个字节以获取大小)。

关于java - 在 UDP 上发送和接收序列化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3997459/

相关文章:

django - 如何将标志传递给 DRF 中的子序列化程序

sockets - 作为 UDP 客户端的 Netcat 不接收来自 Arduino Wifi Shield 的响应数据包。简单的 C UDP 客户端。为什么?

python - 使用Python在UDP客户端-服务器中打印消息时的编码问题

java - 水壶:processRow() 中的多个 putRows() 正确吗?

serialization - logstash 配置文件的格式是什么

java - LWJGL 矩阵堆栈意外行为

java - Fasterxml Jackson 原始 boolean 序列化

c - Windows 自定义消息记录器的实现 : reports 10049 when ntwk cable unplugged

java - 为什么这个 INSERT 添加了一个额外的空行?

java - jasper 2.0.5 中的子报告未显示在主报告 pdf 中