I have a javafx desktop chess application which can be played online via sockets. One player create the GameRoom(Serversocket) and the other can join directly by entering IP address in a dialog. Instead of this method i would like to list all the available rooms and connect that way.
What would be the easiest way to implement this? I thought about putting the Server adresses in an online database like Firestore but that doesn't seem optimal for a desktop application.
The ServerSocket:
public class GameServer implements Runnable{
private PrintWriter pw;
private BufferedReader in;
private ServerSocket listener;
public GameServer(){
}
@Override
public void run() {
try {
listener=new ServerSocket(50000);
System.out.println("Server is listening on port 50000");
if(in==null || pw==null){
Socket socket=listener.accept();
System.out.println("A player has connected");
pw=new PrintWriter(socket.getOutputStream(),true);
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
Executors.newFixedThreadPool(1).execute(() -> {
try {
OnlineGameFunctions.receiveMoveAndResign(in);
} catch (IOException e) {
e.printStackTrace();
}
});
}catch (IOException e){
e.printStackTrace();
}
}
The client:
public class ClientSocket {
private Socket socket;
private PrintWriter pw;
private BufferedReader in;
public ClientSocket() {
}
public void Connect(String ip, int port) throws IOException {
if(in==null || pw==null){
socket=new Socket(ip,port);
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw=new PrintWriter(socket.getOutputStream(),true);
}
Executors.newFixedThreadPool(1).execute(() -> {
try {
OnlineGameFunctions.receiveMoveAndResign(in);
} catch (IOException e) {
e.printStackTrace();
}
});
}
}