-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
54 lines (40 loc) · 1.33 KB
/
Server.java
File metadata and controls
54 lines (40 loc) · 1.33 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
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
ServerSocket serverSocket = null;
int port = 4445;
public Server() {
try {
serverSocket = new ServerSocket(port);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int i = 0;
while (i < 2) { //waits for 2 clients to connect. Once they both disconnenct the server terminates.
Socket clientSocket = null;
try {
//establishes client connection. Waits till a client connects
clientSocket = serverSocket.accept();
clientSocket.setKeepAlive(true);
clientSocket.setTcpNoDelay(true);
//start new thread, one per client
ServerConnection sconn = new ServerConnection(clientSocket);
Thread clientThread = new Thread(sconn);
clientThread.start();
i++;
} catch (IOException e) {
System.out.println("could not accept connection on port " + port);
}
}
}
public boolean getStopProcessing() {
return true;
}
public static void main(String[] args) {
System.out.println("Start");
Server s = new Server();
System.out.println("Disconnecting");
}
}