在UDP上发送和接收序列化对象 [英] Send and receive serialize object on UDP

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

问题描述

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

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:


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

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

我建议你从具有固定已知长度的datagrampacket开始,在那里传输实际有效载荷的大小。类似于:

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 clas:

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)

编辑:As在评论中说,主要是使用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:至于代码:do不按原样使用它。它只是一个例子,在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上发送和接收序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆