-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
112 lines (88 loc) · 3.41 KB
/
Server.java
File metadata and controls
112 lines (88 loc) · 3.41 KB
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
112
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.*;
/*
TODO: > Multiple socket connections
> Selectors
> Have server echo message to all clients (except sender)
*/
public class Server {
private ServerSocketChannel server;
private Selector selector;
private List<SelectionKey> clientList;
public Server(int port) throws Exception{
// Initialize things
server = ServerSocketChannel.open();
selector = Selector.open();
clientList = new ArrayList<SelectionKey>();
server.bind(new InetSocketAddress("localhost", port));
server.configureBlocking(false);
server.register(selector, SelectionKey.OP_ACCEPT);
System.out.println(String.format("Server started on port %s", port));
}
public void start() throws Exception{
while (true) {
selector.select();
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while(iterator.hasNext()) {
SelectionKey key = iterator.next();
if (key.isAcceptable()) {
registerClient(server);
}
if(key.isReadable()){
String message = readFromChannel(key);
System.out.println(message);
sendToClients(key, message, clientList);
}
iterator.remove();
}
}
}
private void registerClient(ServerSocketChannel server) throws Exception{
// Register Client
SocketChannel client = server.accept();
client.configureBlocking(false);
clientList.add(client.register(selector, SelectionKey.OP_READ));
System.out.println("\nClient accepted");
}
private String readFromChannel(SelectionKey key) throws Exception{
ByteBuffer buffer = ByteBuffer.allocate(1024);
SocketChannel clientChannel = (SocketChannel) key.channel();
int channelRead = clientChannel.read(buffer);
if(channelRead == -1){
clientChannel.close();
return "\n";
}
return new String(buffer.array());
}
private void sendToClients(SelectionKey clientToExclude, String message, List<SelectionKey> clientList) throws Exception{
// Send to all clients in list, except for sender
for(SelectionKey client : clientList){
if(!client.equals(clientToExclude))
sendToChannel(client, message, clientList);
}
}
private void sendToChannel(SelectionKey key, String message, List<SelectionKey> clientList) throws Exception{
ByteBuffer buffer = ByteBuffer.allocate(1024);
SocketChannel clientChannel = (SocketChannel) key.channel();
if(!clientChannel.isConnected()){
clientChannel.close();
clientList.remove(key);
return;
}
buffer.clear();
buffer.put(message.getBytes());
buffer.flip();
clientChannel.write(buffer);
}
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("\nPort to use:");
int port = scanner.nextInt();
Server server = new Server(port);
server.start();
scanner.close();
}
}