File size: 2,311 Bytes
7e59e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package Server;

/*
 * Server.java
 */

import java.io.*;
import java.net.*;
import java.util.*;
import Common.*;


/**This is the main GUI class of the Server. 
 * The GUI should be built in this class.
 */
public class Server
{
	/**This is the Network Component this Server GUI uses 
	 * for sending and receiving messages.
	 */	
	protected ServerNetwork myServerNetwork; 
	
	/**Constructor to build the Server.
	 * @param p The port number where the host/server is listening.
	 */
	public Server(int p)
	{
		this.myServerNetwork = new ServerNetwork(this, p);
	}
	
	
	/**This method is called by the ServerNetwork whenever 
	 * the ServerSocketReader receives a message from the client.
	 * @param msg The message which the server received from the client.
	 */
	public void gotMessageFromClient(Message msg)
	{
		showMessage("\nMESSAGE RECEIVED FROM THE CLIENT: " + msg.printMessageDetails());
		showMessage("\nSENDING THE MESSAGE BACK TO CLIENT...");
		this.myServerNetwork.sendMessageToClient(msg);
	}
	

	

	 /** Main function taking one or two arguments
    * @param arg[0] The Port number to listen on.
    */
    public static void main (String args[]) 
    {
        int hostPort;
        if (args.length < 1 || args.length > 1)
        {
            error("Usage: java Server port_number");
        }
        
        try 
        {
			hostPort = Integer.parseInt(args[0]);
			if (hostPort < 0 || hostPort > 65535)
			{
				error("Port number out of range (0, 65535): " + hostPort);
			}
			
			Server server = new Server(hostPort);
		}
		catch (NumberFormatException e) 
		{
			error("Port number not in valid format: " + args[0]);
		}
    }
        
    /**A small function to display errors. 
     * System exits after showing the error message
	 * @param msg The message string to show.
	 */
	private static void error(String message) 
	{
		System.out.println("Server error: " + message);
		System.exit(1);
	}
	
	/**A small function to display warnings.
	 * @param msg The message string to show.
	 */
	private static void warning(String message)
	{
		System.out.println("Server warning: " + message);
	}
	
	/**A small function to display messages.
	 * @param msg The message string to show.
	 */
	private void showMessage(String msg)
  	{
  		System.err.println("Server: " + msg);
  	}
}