JAVA中如何通过UDP连接发送向量

JAVA中如何通过UDP连接发送向量,java,sockets,vector,Java,Sockets,Vector,我试图用Java将矢量对象从UDP服务器发送到UDP客户端 串行化后作为对象发送和接收字符串已经实现,但我无法发送或接收向量。下面是服务器ide代码 public class UDPReceive { public UDPReceive() throws IOException { try { int port = Integer.parseInt("1233"); int allReceived=0; String[] custDat

我试图用Java将矢量对象从UDP服务器发送到UDP客户端

串行化后作为对象发送和接收字符串已经实现,但我无法发送或接收向量。下面是服务器ide代码

public class UDPReceive {


    public UDPReceive() throws IOException {
    try {

      int port = Integer.parseInt("1233");

      int allReceived=0;
      String[] custData=new String[3];

      DatagramSocket dsocket = new DatagramSocket(port);

      byte[] buffer = new byte[2048];
      for(;;) {
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        dsocket.receive(packet);

        String msg = new String(buffer, 0, packet.getLength());
        String msg2 = new String(packet.getData());
        custData[allReceived]=msg;
        allReceived++;
        if(allReceived == 3){
            System.out.println("All Data Received");
            for(int i=0;i<3;i++){
                System.out.println(custData[i]);
            }
            Vector rawData=getTransactions(custData[0],custData[1],custData[2]);
            System.out.println("Vectot size "+ rawData.size());
            byte[] sendData = new byte[1024];
            sendData=(object[])rawData.toArray();
            allReceived=0;
         }/*if ends here */
      }
    }
    catch (Exception e) {
      System.err.println(e);
    }
  }

在这里,我想将rawData变量发送回客户端并接收它,然后在客户端将其转换为vector。我也尝试过使用byte[],但没有成功

我建议您将向量序列化为ObjectOutputStream,然后使用ObjectInputStream获取原始向量

public static byte[] objectToBytes(Object o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);
    oos.close();
    return baos.toByteArray();
}
逆转

public static <T> T bytesToObject(byte[] bytes) throws IOException, ClassNotFoundException {
    return (T) new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject();
}