/* This is the core of the server and contains the client listener */ import java.net.*; public class Server { private static PacketHandler handler = new PacketHandler(); private static int port = 1027; //The port that the server will be started on private int loopTime = 500; //Number of milliseconds before the server loops private static int maxConnections = 10; //Absolute maximum amount of players able to use the server at one time public static int currentlyOnline = 0; //Number of players currently online public static int[] XCoords = new int[maxConnections], YCoords = new int[maxConnections], playerDirections = new int[maxConnections]; public static String[] usernames = new String[maxConnections]; public Server() { } public static void main(String[] args) { Updater updater = new Updater(); try { ServerSocket ss = new ServerSocket(port); // MAKE THIS SOCKET, NON BLOCKING. System.out.println("Server started on " + ss.getInetAddress().getHostAddress() + ":" + port); while (true) { Socket newConnection = ss.accept(); // MAKE THIS SOCKET NON BLOCKING String hostname = ss.getInetAddress().getHostAddress(); System.out.println("Client connection accepted from " + hostname + ":" + newConnection.getPort()); // WE HAVE A CONNECTION PacketReader packetType = new PacketReader(newConnection.getInputStream()); // GIVE A THREAD HERE // runUserThread(sockety thing) } } catch (java.io.IOException ioe) { ioe.printStackTrace(); } } } public void runUserThread(Socket theConnection) { PacketReader packetType = new PacketReader(theConnection.getInputStream()); switch (packetType.getID()) { case 0: handler.handleNewConnection(newConnection, hostname); break; //new user case 1: handler.handleNewConnection(newConnection, hostname); break; //login case 2: (new Thread(updater)).start(); updater.startUpdateHandling(newConnection, hostname); break; //update packet } }