1

要通过 DatagramPacket 发送字符串,我们使用:

 String msg = "example";
 byte[]data = msg.getBytes();
 DatagramPacket pktOut = new DatagramPacket(data, 0, data.length, dest, port)

如何通过 DatagramPacket 发送数组?

 int num[] = {50,20,45,82,25,63};
 //I need to send this over two packets, but I don't know how to deal
//with arrays when sending them

先感谢您

4

1 回答 1

2

您可以使用ByteBuffer类将整数数组转换为字节缓冲区。

int num[] = { 50 , 20 , 45 , 82 , 25 , 63 };
ByteBuffer bb = ByteBuffer.allocate( num.length * 4 );
for ( int i : num ) {
    bb.putInt( i );
}
byte[] data = bb.array();
于 2012-12-10T14:13:44.610 回答