Fixed some bugs in the GUI-Client, such as buffer issues, missing port-checking and some problems with pushing empty messages.
This commit is contained in:
parent
5b4f5a8b00
commit
f33b45b67f
@ -8,7 +8,7 @@ import java.nio.channels.DatagramChannel;
|
||||
import common.MessageType;
|
||||
|
||||
public class BufferedNetworkStackClient {
|
||||
protected static final long TIMEOUT = 5000;
|
||||
protected static final long TIMEOUT = 3000;
|
||||
|
||||
protected static final int BUF_SIZE = 128;
|
||||
|
||||
@ -27,7 +27,7 @@ public class BufferedNetworkStackClient {
|
||||
|
||||
InetSocketAddress dest = new InetSocketAddress(host, port);
|
||||
|
||||
buf.clear();
|
||||
buf = ByteBuffer.allocate(BUF_SIZE);
|
||||
buf.put(MessageType.CMD_PUSH);
|
||||
|
||||
buf.putInt(s.length());
|
||||
@ -69,7 +69,7 @@ public class BufferedNetworkStackClient {
|
||||
|
||||
for (int chunkId = 0; chunkId < numChunks; chunkId++) {
|
||||
buf.flip();
|
||||
buf.clear();
|
||||
buf = ByteBuffer.allocate(BUF_SIZE);
|
||||
|
||||
buf.put(MessageType.CMD_DATA);
|
||||
buf.putInt(sessionId);
|
||||
@ -88,7 +88,7 @@ public class BufferedNetworkStackClient {
|
||||
|
||||
try {
|
||||
channel.send(buf, dest);
|
||||
buf.clear();
|
||||
buf = ByteBuffer.allocate(BUF_SIZE);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -100,6 +100,7 @@ public class BufferedNetworkStackClient {
|
||||
if (channel == null) {
|
||||
channel = DatagramChannel.open();
|
||||
}
|
||||
buf = ByteBuffer.allocate(BUF_SIZE);
|
||||
|
||||
InetSocketAddress dest = new InetSocketAddress(host, port);
|
||||
channel.send(ByteBuffer.wrap(new byte[] { MessageType.CMD_POP }), dest);
|
||||
@ -116,7 +117,8 @@ public class BufferedNetworkStackClient {
|
||||
|
||||
byte[] receivedData = buf.array();
|
||||
String strReceived = new String(receivedData).toString().trim();
|
||||
|
||||
|
||||
System.out.println("Received: " + strReceived);
|
||||
return strReceived;
|
||||
}
|
||||
|
||||
|
||||
@ -123,15 +123,25 @@ public class ClientGUI extends JFrame {
|
||||
} catch (IOException e1) {
|
||||
textPane.setText(textPane.getText()
|
||||
+ "\nCould not send to server. " + e1.getMessage());
|
||||
} catch (IllegalArgumentException e1) {
|
||||
textPane.setText(textPane.getText() + "\n"
|
||||
+ e1.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
btnPush.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
try {
|
||||
String host = txtHost.getText();
|
||||
int port = Integer.valueOf(txtPort.getText());
|
||||
client.push(userinput.getText(), host, port);
|
||||
if (!userinput.getText().isEmpty()) {
|
||||
String host = txtHost.getText();
|
||||
int port = Integer.valueOf(txtPort.getText());
|
||||
client.push(userinput.getText(), host, port);
|
||||
textPane.setText(textPane.getText() + "\nPushed: "
|
||||
+ userinput.getText());
|
||||
} else {
|
||||
textPane.setText(textPane.getText() + "\nUserinput is Empty.");
|
||||
}
|
||||
|
||||
} catch (NumberFormatException e1) {
|
||||
textPane.setText(textPane.getText()
|
||||
+ "\nErroneous port number. " + e1.getMessage());
|
||||
@ -141,6 +151,8 @@ public class ClientGUI extends JFrame {
|
||||
} catch (TimeoutException e) {
|
||||
textPane.setText(textPane.getText() + "\nServer timed out."
|
||||
+ e.getMessage());
|
||||
} catch (IllegalArgumentException e) {
|
||||
textPane.setText(textPane.getText() + "\n" + e.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user