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

Send and receive serialize object on UDP(在 UDP 上发送和接收序列化对象)

本文介绍了在 UDP 上发送和接收序列化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?!

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

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.

推荐答案

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

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:

数据报包用于实现无连接的数据包传递服务.每条消息都是从仅基于一台机器到另一台机器关于其中包含的信息包.发送的多个数据包一台机器到另一台机器可能被路由不同,并且可能到达任何命令.包邮不保证.

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

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

关于您的屏蔽:

从这里接收一个数据报包插座.当此方法返回时,DatagramPacket 的缓冲区被填满收到的数据.数据报包还包含发件人的 IP 地址,和发件人的端口号机器.

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 个字节).

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

我建议您从具有固定已知长度的数据报包开始,您可以在其中传输实际有效负载的大小.比如:

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

我使用的 CientModel 类:

The CientModel clas sI used:

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)

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)

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

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.

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

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

这篇关于在 UDP 上发送和接收序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 UDP 上发送和接收序列化对象

基础教程推荐