CS-465 / testEcho / Server / src / echo / EchoServer.java
EchoServer.java
Raw
package echo;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.PropertyHandler;

/**
 * Simple EchoServer to showcase socket programming as well as thread programming
 * 
 * @author wolfdieterotte
 */
public class EchoServer {

    private static ServerSocket serverSocket;
    private static int port = 0;


    /**
     * Constructor
     * 
     * @param propertiesFile String of a file on relative path containing properties
     */
    public EchoServer(String propertiesFile) {
        
        Properties properties = null;
        
        // open properties
        try
        {
            properties = new PropertyHandler(propertiesFile);
        }
        catch (IOException ex)
        {
            Logger.getLogger(EchoServer.class.getName()).log(Level.SEVERE, "Cannot open properties file", ex);
            System.exit(1);
        } 

        // get server port number
        try
        {
            port = Integer.parseInt(properties.getProperty("SERVER_PORT"));
        }
        catch (NumberFormatException ex)
        {
            Logger.getLogger(EchoServer.class.getName()).log(Level.SEVERE, "Cannot read server port", ex);
            System.exit(1);
        }
        
        // open server socket
        try
        {
            serverSocket = new ServerSocket(EchoServer.port);
        }
        catch(IOException ex)
        {
            Logger.getLogger(EchoServer.class.getName()).log(Level.SEVERE, "Cannot start server socket", ex);
        }
    }

    
    /**
     * Convenience method to be called on an instance of EchoServer in main()
     * 
     * @throws IOException 
     */
    public void runServerLoop() throws IOException {

	while (true) {
            System.out.println("Waiting for connections on port #" + port); 
            
            // if we subclass EchoThread from Thread 
            // (new EchoThread(serverSocket.accept())).start();
            
            // if EchoThread implements Runnable
            (new Thread(new EchoServerThread(serverSocket.accept()))).start();
            
            /*
            // The above is equivalent with the below, taking the above compact expression apart.
            Socket clientSocket = serverSocket.accept();
            EchoThread echoThreadRunnable = new EchoThread(clientSocket);
            Thread echoThread = new Thread(echoThreadRunnable);
            echoThread.start();
            */
	}
    }

    
    /**
     * Getting the ball rolling
     * 
     * @param args
     * @throws Exception 
     */
    public static void main(String args[]) throws Exception {
        // create instance of echo server
        // note that hardcoding the port is bad, here we do it just for simplicity reasons
        EchoServer echoServer = new EchoServer("config/Server.properties");

        // fire up server loop
        echoServer.runServerLoop();
    }
}