-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReceiveMultipleMessages.java
111 lines (91 loc) · 4.67 KB
/
ReceiveMultipleMessages.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.Iterator;
import java.util.Set;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
public class ReceiveMultipleMessages {
public static void handleConnection() throws Exception {
// Modify this to match the computer you are connecting to
String ipAddress = "127.0.0.1";
int port = 10000;
// Selector: multiplexor of SelectableChannel objects
Selector channelSelector = Selector.open(); // selector is open here
// ServerSocketChannel: selectable channel for stream-oriented listening sockets
ServerSocketChannel myServerSocketChannel = ServerSocketChannel.open();
InetSocketAddress myAddress = new InetSocketAddress(ipAddress, port);
// Binds the channel's socket to a local address and configures the socket to listen for connections
myServerSocketChannel.bind(myAddress);
// Adjusts this channel's blocking mode.
myServerSocketChannel.configureBlocking(false);
// Used as a 'state machine' for figuring if socket is waiting for a connection,
// or if we have a connection and instead should be receiving data
int ops = myServerSocketChannel.validOps();
SelectionKey selectKy = myServerSocketChannel.register(channelSelector, ops, null);
// Create variables for receiving the message and keeping stats
String fullMessage = "";
boolean done = false;
int chunks = 0;
long startTime = System.currentTimeMillis();
long endTime = 0L;
System.out.println("\nStarting...");
while(!done) {
// Selects a set of keys whose corresponding channels are ready for I/O operations
// For blocking I/O use a select with no data:
// channelSelector.select(10);
// For non-blocking, use a select with a number in ms to wait. (Using zero will chew a lot
// of CPU unless something else in the loop waits.)
// channelSelector.select(0);
channelSelector.select(0);
// Token representing the registration of a SelectableChannel with a Selector
Set<SelectionKey> myKeys = channelSelector.selectedKeys();
Iterator<SelectionKey> keyIterator = myKeys.iterator();
while (keyIterator.hasNext()) {
SelectionKey myKey = keyIterator.next();
// Tests whether this key's channel is ready to accept a new socket connection
if (myKey.isAcceptable()) {
SocketChannel myClient = myServerSocketChannel.accept();
// Adjusts this channel's blocking mode to false
myClient.configureBlocking(false);
// Operation-set bit for read operations
myClient.register(channelSelector, SelectionKey.OP_READ);
System.out.println("Connection Accepted: " + myClient.getLocalAddress());
// Tests whether this key's channel is ready for reading
} else if (myKey.isReadable()) {
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) {
fullMessage = fullMessage + result;
chunks += 1;
System.out.println("Message received " + result.length() + " bytes: \"" + result + "\"");
}
if (result.endsWith("\n")) {
myClient.close();
done = true;
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.");
}
public static void main(String[] args) throws Exception {
while(true)
handleConnection();
}
}