From 3da6673156d6c9d6e0cb612c3fd8bea72926ee60 Mon Sep 17 00:00:00 2001 From: Mehrdad Shirvani Date: Mon, 12 May 2025 22:42:44 +0330 Subject: [PATCH] feat: add BattleShipServer --- src/main/java/Server/BattleShipServer.java | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/main/java/Server/BattleShipServer.java diff --git a/src/main/java/Server/BattleShipServer.java b/src/main/java/Server/BattleShipServer.java new file mode 100644 index 0000000..4e990af --- /dev/null +++ b/src/main/java/Server/BattleShipServer.java @@ -0,0 +1,59 @@ +package Server; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.ArrayList; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +public class BattleShipServer { + private static int PORT = 3737; + // TODO: Create a collection to keep track of connected clients + private static ArrayList clients = new ArrayList(); + private static ExecutorService pool = Executors.newFixedThreadPool(4); + + static ClientHandler playerOne; + static ClientHandler playerTwo; + + + public static void main(String[] args) { + ServerSocket listener = null; + + try { + // TODO: Create a ServerSocket to listen for incoming connections + //int i = 0; + + while(true) { + + // TODO: Accept a client connection + + // TODO: Create a ClientHandler for the connected client + + //TODO: uncomment this code + //if(i == 0) { + // playerOne = clientThread; + //} + //if(i == 1) { + // playerTwo = clientThread; + // playerOne.setEnemy(playerTwo); + // playerTwo.setEnemy(playerOne); + //} + //i++; + + //TODO add clientThread to clients and executor service + + } + } finally { + if (listener != null) { + try { + listener.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + + pool.shutdown(); + } + } +} \ No newline at end of file