-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.java
More file actions
53 lines (39 loc) · 1.18 KB
/
server.java
File metadata and controls
53 lines (39 loc) · 1.18 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
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class server
{
//All connections
public static DynamicArray<ServerConnection> connections;
//Output with pretty print and this module's name
private static void out(String message)
{
PrettyPrint.out("server", message)
}
public static void startServer(int port)
{
out("Starting server on port " + port + " ...");
connections = new DynamicArray<ServerConnection>(0);
try
{
// The server socket
ServerSocket servSock = new ServerSocket(port);
while (true)
{
out("Waiting for a connection...");
// Incoming client socket
Socket clientSock = servSock.accept();
out("Connection received, setting up data structures...");
//Create a new `ServerConnection` which represents a connection
ServerConnection connection = new ServerConnection(clientSock);
//Append the `ServerConnection` object to the array
connections.append(connection);
out("Connection object created.");
}
}
catch (IOException err)
{
out("There was an error with the server socket.");
}
}
}