-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.java
57 lines (51 loc) · 1.62 KB
/
server.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
package nbm.server;
import java.net.*;
import java.io.*;
import nbm.common.*;
class ServerWorker implements Runnable {
private Socket socket;
private String id;
public ServerWorker(Socket socket) {
this.socket = socket;
this.id = this.socket.getInetAddress() + ":" + String.valueOf(this.socket.getPort());
System.out.println("Connection `" + this.id + "` connected!");
}
public void run() {
try {
BufferedReader inputStream = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
PrintStream outputStream = new PrintStream(this.socket.getOutputStream());
try {
String inputLine;
while ((inputLine = inputStream.readLine()) != null) {
if (inputLine.equals("bye")) {
break;
}
Message msg = new Message(inputLine);
if(msg.getBody().startsWith("[-usedownlink]")) {
outputStream.println(inputLine);
outputStream.flush();
}
}
}
finally {
inputStream.close();
outputStream.close();
this.socket.close();
System.out.println("Connection `" + this.id + "` disconnected!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Server {
public static void main(String args[]) throws Exception {
int port = (args.length > 0 ? Integer.parseInt(args[0]) : 2912);
ServerSocket server = new ServerSocket(port);
System.out.println("Server listening on port " + server.getLocalPort());
while (true) {
Socket socket = server.accept();
new Thread(new ServerWorker(socket)).start();
}
}
}