//------------------------------------------------------------------------- // File: Server.java // // Created by: Tamer Elsharnouby (sharno@cs.umd.edu) // // Course: Project 1, CMSC417-0101 Fall 1999 // // Description: Creates a server that waits on a specifed port and assigns // a ServerThread to each client requesting a connection. //------------------------------------------------------------------------- import ServerThread; import java.net.*; import java.lang.*; import java.util.*; import java.io.*; //------------------------------------------------------------------------ // Class: Server // // Creates the server's socket waiting on a specifed port, and launches // a ServerThread for each client that requests a connection. // Terminated by killing process from shell (eg, control-C). // // Attributes // sSock: Socket on which server listens for new clients // sPort: Server port number (for sSock); passed from command line. // // Methods // main: The main method for the Server class. //------------------------------------------------------------------------ class Server { static ServerSocket sSock; static int sPort; static String responsePrefix; //------------------------------------------------------------------- // Method: main // // Run body of server. Creates socket and assigns thread to each client. // // Parameters // args: First argument is response prefix. // Second argument is the assigned port number. // // Variables // cSock: Socket of ServerThread by which it connects to client. // // Returns: none public static void main (String[] args) { // Gets assigned port number from command line arguments responsePrefix = args[0].toString(); sPort = Integer.parseInt(args[1]); System.err.println("Socket Port: " + sPort); try { // Create server socket with port "sPort". sSock = new ServerSocket(sPort); } catch (IOException e) { System.err.println("Unable to bind Server to port" + sPort); e.printStackTrace(); } while (true){ Socket cSock = null; // Accept a requesting client and assign it socket cSock try { cSock = sSock.accept(); } catch (IOException e) { System.err.println("Accept failed on server port"); e.printStackTrace(); } // Launch a new ServerThread to deal with the accepted client new ServerThread(cSock, responsePrefix ).start(); } } //---------------- method main ------------------------------------- } //------------------ Class Server ------------------------------------