接收:
DatagramSocket rawSocket = new DatagramSocket(port);
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
this.rawSocket.receive(packet);
ByteBuffer tempdata = ByteBuffer.allocate(packet.getLength());
tempdata.put(buf, 0, packet.getLength());
tempdata.order(ByteOrder.LITTLE_ENDIAN); // 以小段的方式传送
tempdata.position(0);
buf = tempdata.array();
发送:
DatagramSocket rawSocket = new DatagramSocket(port);
DatagramPacket dp = new DatagramPacket(data, 0, data.length, InetAddress.getByName(ip),port);
this.rawSocket.send(dp);
转载于:https://blog.51cto.com/nuoyafangzhou/1149230