From 2b8cd7c73a6c6f6604baaa5456887ca960597efa Mon Sep 17 00:00:00 2001 From: Louis Royer Date: Thu, 2 Apr 2020 00:53:57 +0200 Subject: [PATCH] UDP packets max size fix --- src/protocolP2P/ProtocolP2PPacketUDP.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/protocolP2P/ProtocolP2PPacketUDP.java b/src/protocolP2P/ProtocolP2PPacketUDP.java index 0b639fe..afc1e64 100644 --- a/src/protocolP2P/ProtocolP2PPacketUDP.java +++ b/src/protocolP2P/ProtocolP2PPacketUDP.java @@ -30,6 +30,7 @@ import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketAddress; import java.io.IOException; +import java.util.Arrays; /** Representation of packet. * @author Louis Royer @@ -116,9 +117,11 @@ public class ProtocolP2PPacketUDP < T extends Payload> extends ProtocolP2PPacket throw new InternalError(); } DatagramSocket ss = (DatagramSocket)socket; - byte[] packet = new byte[1024]; - DatagramPacket reception = new DatagramPacket(packet, packet.length); + byte[] packetTmp = new byte[0xFFFF]; + DatagramPacket reception = new DatagramPacket(packetTmp, packetTmp.length); ss.receive(reception); + int payloadSize = Payload.getPayloadSize(packetTmp); + byte[] packet = Arrays.copyOf(packetTmp, Payload.PAYLOAD_START_POSITION + payloadSize); responseSocketAddress = reception.getSocketAddress(); remoteHost = new HostItem(reception.getAddress().getHostName(), reception.getPort()); // contruction @@ -216,9 +219,11 @@ public class ProtocolP2PPacketUDP < T extends Payload> extends ProtocolP2PPacket throw new InternalError(); } // reception - byte[] packet = new byte[8192]; - DatagramPacket reception = new DatagramPacket(packet, packet.length); + byte[] packetTmp = new byte[0xFFFF]; + DatagramPacket reception = new DatagramPacket(packetTmp, packetTmp.length); requestSocket.receive(reception); + int payloadSize = Payload.getPayloadSize(packetTmp); + byte[] packet = Arrays.copyOf(packetTmp, Payload.PAYLOAD_START_POSITION + payloadSize); // contruction try {