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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3997459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:08:17  来源:igfitidea点击:

Send and receive serialize object on UDP

javaserializationudp

提问by Noor

I am trying to send a serialized object from a server process to a client process in Java using UDP. The problem is that the client is being blocked on the receive method. Can someone help?!

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

here is the server code for sending the object:

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

  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);

and here is the client code for receiving the object:

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

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

I just want to receive the object to be able to reconstruct but I cannot receive the packet itself.

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

采纳答案by Niko

I dont know what you want to accomplish in the end, but working with UDP is not so easy... the main reason is in the Description of the DatagramPacket Object:

我不知道你最后想要完成什么,但是使用 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.

数据报数据包用于实现无连接的数据包传递服务。每条消息从一台机器路由到另一台机器,仅基于该数据包中包含的信息。从一台机器发送到另一台机器的多个数据包可能会以不同的方式路由,并且可能以任何顺序到达。不保证包裹递送。

A good tutorial when working with udp is http://download.oracle.com/javase/tutorial/networking/datagrams/clientServer.html

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

About your blocking:

关于您的屏蔽:

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.

从此套接字接收数据报包。当此方法返回时,DatagramPacket 的缓冲区将填充接收到的数据。数据报包还包含发送者的 IP 地址和发送者机器上的端口号。

该方法阻塞直到接收到数据报。数据报包对象的长度字段包含接收到的消息的长度。如果消息长于数据包的长度,则消息将被截断。

I didnt really test it, but I am pretty sure - based on the description - that the datagramsocket.reseive function will block until the packet is filled (in your case until 100000 bytes are received).

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

I would suggest you start with a datagrampacket with a fixed known length, where you transmit the size of the actual payload. Something like:

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

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();
    }
}

On the other side you now KNOW your sizes:

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

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();
    }
}

The CientModel clas sI used:

我使用的 CientModel 类:

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

    String name = "";
    int data = 1;

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

I tested this code and it works just fine. Hope that helps (I got the byte-To-int and around from http://www.tutorials.de/java/228129-konvertierung-von-integer-byte-array.html)

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

Edit: As stated in the comments, it is often a very bad idea to use UDP, mainly, because you do not know if your packets are received in the correct order, or even at all. UDP does NOT guarantee that. I didn't do too much udp programming, but the only part you can rely on (if I understood correctly) is, that if you get a packet and it fits within the datagram (65,527 bytes - see https://en.wikipedia.org/wiki/User_Datagram_Protocol) it will contain the whole thing. So if you do not care about the order in which the message come and your object fits in the datagram, you should be fine.

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

Edit2: As for the code: do not use it as is. it is only an example, ind UDP you should only have one type of packet, and this with a known size. that way you do not need to send the "size". If you use the code as shown above, and one packet is dropped, the next packet will be the wrong size (i.e. the first packet is dropped, suddenly you are checking the first bytes of the payload to get the size).

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

回答by teu

I didnt really test it, but I am pretty sure - based on the description - that the datagramsocket.reseive function will block until the packet is filled (in your case until 100000 bytes are received).

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

This is wrong. The receive function will block until a datagram is received, which can be smaller than the buffer size (and usually will be). The method packet.getLength() will tell you how big it was.

这是错误的。接收函数将阻塞,直到接收到一个数据报,该数据报可能小于缓冲区大小(通常会如此)。packet.getLength() 方法会告诉你它有多大。