diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java index f43a907e..1bc2a986 100644 --- a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java +++ b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java @@ -11,6 +11,8 @@ public class BufferedNetworkStackClient { protected static final byte CMD_POP = '2'; protected static final byte CMD_PRINT = '3'; + protected static final long TIMEOUT = 5000; + protected static final int BUF_SIZE = 128; private DatagramChannel channel = null; @@ -38,11 +40,15 @@ public class BufferedNetworkStackClient { buf.flip(); buf = ByteBuffer.allocate(BUF_SIZE); - channel.configureBlocking(false); - - if(channel.receive(buf) == null){ + + channel.configureBlocking(false); + long time = System.currentTimeMillis() + TIMEOUT; + boolean rec = false; + while(rec = channel.receive(buf) == null && System.currentTimeMillis() < time){} + if(!rec){ throw new Exception("ERROR: Buffer empty - not data recieved"); } + channel.configureBlocking(true); byte[] receivedData = buf.array(); String infoPacket = new String(receivedData).trim(); @@ -97,10 +103,13 @@ public class BufferedNetworkStackClient { channel.send(ByteBuffer.wrap(new byte[] { CMD_POP }), dest); channel.configureBlocking(false); - - if(channel.receive(buf) == null){ + long time = System.currentTimeMillis() + TIMEOUT; + boolean rec = false; + while(rec = channel.receive(buf) == null && System.currentTimeMillis() < time){} + if(!rec){ throw new Exception("ERROR: Buffer empty - not data recieved"); } + channel.configureBlocking(true); byte[] receivedData = buf.array(); String strReceived = new String(receivedData).toString().trim();