Newer
Older
package com.example.schedulerapp;
import java.io.*;
import java.net.*;
import java.util.*;
public class ScheduleServer {
private final int port;
private final Set<UserThread> userThreads = new HashSet<>(); // notify when there are changes
public ScheduleServer(int port) {
this.port = port;
String ip = "localhost"; //Set "localhost" is working locally. Razer Blade lan address (NOT static) is "172.16.1.99"
try (ServerSocket serverSocket = new ServerSocket(port, 50, InetAddress.getByName(ip))) {
System.out.println("Server is listening on port " + port);
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New user connected");
UserThread newUser = new UserThread(socket, this, this.rafisWeirdClass);
userThreads.add(newUser);
newUser.start();
}
} catch (IOException error) {
System.out.println("Error in the server :" + error.getMessage());
error.printStackTrace();
}
}
void broadcast(String message) {
for (UserThread aUser : userThreads) {
aUser.sendMessage(message);
}
}
void removeUser(UserThread aUser) {
userThreads.remove(aUser);
}
public static void main(String[] args) {
ScheduleServer server = new ScheduleServer(8989);
server.execute();
}
}