timeout now working, no more exceptions

This commit is contained in:
Ulf Gebhardt 2012-11-06 15:04:24 +01:00
parent 62cc435a5f
commit 1e0cee1061
2 changed files with 22 additions and 8 deletions

View File

@ -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();

View File

@ -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());
}
}
});