Skip to content

Commit

Permalink
Work on fixing up the Java send/receive message code
Browse files Browse the repository at this point in the history
  • Loading branch information
pvcraven committed Mar 24, 2021
1 parent 122fa62 commit 5cc91f3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
22 changes: 12 additions & 10 deletions ReceiveMultipleMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,28 @@ public static void handleConnection() throws Exception {
} else if (myKey.isReadable()) {

SocketChannel myClient = (SocketChannel) myKey.channel();
ByteBuffer myBuffer = ByteBuffer.allocate(2048);
myClient.read(myBuffer);
String result = new String(myBuffer.array()).trim();
fullMessage = fullMessage + result;

// System.out.println("Message received: " + result);
chunks += 1;
ByteBuffer myBuffer = ByteBuffer.allocate(65536);
int bytesRead = myClient.read(myBuffer);
String result = new String(myBuffer.array());
result = result.substring(0, bytesRead);
if (result.length() > 0) {
fullMessage = fullMessage + result;
chunks += 1;
System.out.println("Message received " + result.length() + " bytes: \"" + result + "\"");
}

if (result.endsWith("\n")) {
myClient.close();
myServerSocketChannel.close();
channelSelector.close();
done = true;
System.out.println("It's time to close connection as we got a Z");
System.out.println("It's time to close connection as we got a \\n");
endTime = System.currentTimeMillis();
}
}
keyIterator.remove();
}
}
myServerSocketChannel.close();
channelSelector.close();
long totalTime = endTime - startTime;
System.out.println("Done receiving " + fullMessage.length() + " in " + chunks + " chunks over " + totalTime + " ms.");
}
Expand Down
31 changes: 18 additions & 13 deletions ReceiveOneShot.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public void setupSocket() throws Exception {
System.out.println("\nStarting...");

}
public void checkConnection() throws Exception {

public boolean checkConnection() throws Exception {
// Set to true when we have our whole message
boolean done = false;

// Selects a set of keys whose corresponding channels are ready for I/O operations
// For blocking I/O use a select with no data:
Expand Down Expand Up @@ -72,29 +73,33 @@ public void checkConnection() throws Exception {
// Tests whether this key's channel is ready for reading
} else if (myKey.isReadable()) {

SocketChannel myClient = (SocketChannel) myKey.channel();
ByteBuffer myBuffer = ByteBuffer.allocate(2048);
myClient.read(myBuffer);
String result = new String(myBuffer.array()).trim();

System.out.println("Message received: " + result);
SocketChannel myClient = (SocketChannel) myKey.channel();
ByteBuffer myBuffer = ByteBuffer.allocate(65536);
int bytesRead = myClient.read(myBuffer);
String result = new String(myBuffer.array());
result = result.substring(0, bytesRead);
if (result.length() > 0) {
System.out.println("Message received " + result.length() + " bytes: \"" + result + "\"");
}

if (result.endsWith("Z")) {
if (result.endsWith("\n")) {
myClient.close();
myServerSocketChannel.close();
// channelSelector.close();
done = true;
System.out.println("It's time to close connection as we got a Z");
}
}
keyIterator.remove();
}
return done;
}

public static void main(String[] args) throws Exception {
ReceiveOneShot receiveOneShot = new ReceiveOneShot();

receiveOneShot.setupSocket();
while(true)
receiveOneShot.checkConnection();

boolean done = false;
while(!done)
done = receiveOneShot.checkConnection();
}
}

0 comments on commit 5cc91f3

Please sign in to comment.