-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainServer.java
More file actions
90 lines (81 loc) · 3.12 KB
/
Copy pathmainServer.java
File metadata and controls
90 lines (81 loc) · 3.12 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
import java.io.*;
import java.net.*;
import java.util.concurrent.*;
/**
* Main server which handles the creation of 20 Questions game lobbies.
* Listens on port 9999 and allows clients to specify custom ports for new game lobbies.
*/
public class mainServer {
private static final int MAIN_PORT = 9999;
/**
* Starts the lobby server and listens for incoming client requests to create game lobbies.
*
* @throws IOException if the main server socket cannot be created.
*/
public static void main(String[] args) throws IOException {
ServerSocket mainSocket = new ServerSocket(MAIN_PORT);
System.out.println("Lobby server started on port " + MAIN_PORT);
ExecutorService pool = Executors.newCachedThreadPool();
while (true) {
Socket clientSocket = mainSocket.accept();
pool.execute(() -> handleLobbyRequest(clientSocket));
}
}
/**
* Handles a client's request to create a new game lobby.
* Asks for a port and starts a game on that port if available
*
* @param clientSocket the socket connected to the requesting client.
*/
private static void handleLobbyRequest(Socket clientSocket) {
try (
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)
) {
out.println("Welcome! Enter a port number for your game lobby (e.g., 1000–9998):");
String portStr = in.readLine();
int port = Integer.parseInt(portStr.trim());
if (!isPortAvailable(port)) {
out.println("That port is already in use! Please choose a different one.");
} else {
launchGameLobby(port);
out.println("Game lobby started on port " + port);
out.println("Whoever connects first will be the answerer; the second will be the questioner.");
}
} catch (IOException | NumberFormatException e) {
System.err.println("Error in lobby setup: " + e.getMessage());
} finally {
try {
clientSocket.close();
} catch (IOException ignored) {}
}
}
/**
* Checks if a port is open.
*
* @param port the port number to check.
* @return true if the port is available, false if its already in use.
*/
private static boolean isPortAvailable(int port) {
try (ServerSocket socket = new ServerSocket(port)) {
return true;
} catch (IOException e) {
return false;
}
}
/**
* Launches a new game lobby on a separate thread using the specified port.
*
* @param port the port number on which the game will run.
*/
private static void launchGameLobby(int port) {
new Thread(() -> {
try {
gameLogic.startGameOnPort(port);
} catch (IOException e) {
System.err.println("Error launching game on port " + port);
e.printStackTrace();
}
}).start();
}
}