//
//  CSC 468. DBMS Organization
//

//  Stub for MiniXBase server code. Rename to class MiniXBase if using.

//  Alex Dekhtyar.


import java.io.*;
import java.net.*;

public class ServerSide {

        // server socket for communication with clients

	static ServerSocket serverSocket;
	
	
	public static void main(String[] args) {

                // using port 4444. if using another port, update accordingly

		int port=4444;	
		
		try {
		    serverSocket = new ServerSocket(port);
		    
		    Socket clientSocket = null;
		    BufferedReader in=null;
		    PrintWriter out=null;
		    
		    try {
		        clientSocket = serverSocket.accept();
		        
		        out = new PrintWriter(clientSocket.getOutputStream(), true);
	            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
	            String inputLine;
                    String outputLine="Server is ready, please go ahead and" + 
                                       "type in the query";
		        
		        
	            out.println(outputLine); //telling the client that the server is ready
	            
		        
	            while ((inputLine = in.readLine()) != null) {	
	            	
	            	//'inputLine' is a string variable that stores the incoming query
	            	
	            	//Write code here to do what needs to be done with that query
	            	
	                out.println("You have sent:"+inputLine); //remove 'you have sent' and just send the result
	                
	                if (inputLine.equals("exit"))//The command to close the connection 
	                {
	                	
	                	out.println("exit");
	                break;
	                }
	            }

		        System.out.println("The client closed the connection");
		        
		        
		        
		        
		        
		        
		    } catch (IOException e) {
		        System.out.println("Accept failed on:"+port+" because "+e.getMessage());
		        System.exit(-1);
		    }//end of client catch


		    
		    out.close();
	        in.close();
	        clientSocket.close();
	        serverSocket.close();
	        
		} catch (IOException e) {
		    System.out.println("Could not listen on port:"+port+ " Because "+e.getMessage());
		    System.exit(-1);
		}//end of serverSocket catch


		

	}//end of main

}//end of serverSide class