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 b46611ca..f1062069 100644 --- a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java +++ b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/BufferedNetworkStackClient.java @@ -18,13 +18,13 @@ public class BufferedNetworkStackClient { buf = ByteBuffer.allocate(1024); } - public void push(String s, String host, int port) throws IOException { + public void push(String s, String host, int port) throws IOException, Exception{ if (channel == null) { channel = DatagramChannel.open(); } InetSocketAddress dest = new InetSocketAddress(host, port); - + buf.clear(); buf.put(CMD_PUSH); @@ -32,11 +32,15 @@ public class BufferedNetworkStackClient { buf.put(length); buf.flip(); - channel.send(buf, dest); + channel.send(buf, dest); buf.flip(); buf.clear(); - channel.receive(buf); + channel.configureBlocking(false); + + if(channel.receive(buf) == null){ + throw new Exception("ERROR: Buffer empty - not data recieved"); + } byte[] receivedData = buf.array(); String infoPacket = new String(receivedData).trim(); @@ -46,6 +50,7 @@ public class BufferedNetworkStackClient { int remoteBufSize = Integer.valueOf(info[0]); int sessionId = Integer.valueOf(info[1]); + System.out.println('4'); sendChunks(s, dest, remoteBufSize, sessionId); } @@ -80,7 +85,7 @@ public class BufferedNetworkStackClient { } } - public String pop(String host, int port) throws IOException { + public String pop(String host, int port) throws IOException, Exception{ if (channel == null) { channel = DatagramChannel.open(); } @@ -88,7 +93,12 @@ public class BufferedNetworkStackClient { InetSocketAddress dest = new InetSocketAddress(host, port); channel.send(ByteBuffer.wrap(new byte[] { CMD_POP }), dest); - channel.receive(buf); + channel.configureBlocking(false); + + if(channel.receive(buf) == null){ + throw new Exception("ERROR: Buffer empty - not data recieved"); + } + byte[] receivedData = buf.array(); String strReceived = new String(receivedData).toString().trim(); diff --git a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/ClientGUI.java b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/ClientGUI.java index 4baca129..62dea1b6 100644 --- a/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/ClientGUI.java +++ b/ws2012/P2P/uebungen/2/p2p_ex2/src/buffered/ClientGUI.java @@ -115,7 +115,9 @@ public class ClientGUI extends JFrame { e1.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); - } + } catch (Exception e3){ + textPane.setText(textPane.getText() + "\n" + e3.getMessage()); + } textPane.setText(textPane.getText() + "\n" + received); } @@ -129,7 +131,9 @@ public class ClientGUI extends JFrame { } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); - } + } catch (Exception e3){ + textPane.setText(textPane.getText() + "\n" + e3.getMessage()); + } } });