Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

ClypeServer.

java
1 package main;
2
3 import java.io.IOException;
14
15 public class ClypeServer {
16
17 private int port;
18 private boolean closeConnection;
19 private ArrayList<ServerSideClientIO> serverSideClientIOList;
20
21 private int nextID = 0;
22
23 static final int DEFAULT_PORT = 7000;
24
25 /**
26 *
27 * Constructor that initializes the port and other settings.
28 *
29 * @param port the port the server uses
30 */
31 public ClypeServer(int port) throws IllegalArgumentException {
32
33 if(port < 1024)
34 throw new IllegalArgumentException();
35
36 this.port = port;
37 closeConnection = false;
38 serverSideClientIOList = new ArrayList<ServerSideClientIO>();
39 }
40
41 /**
42 * Default constructor that defaults the port to 7000.
43 */
44 public ClypeServer() {
45 this(DEFAULT_PORT);
46 }
47
48 /**
49 * This method starts something
50 */
51 public void start() {
52
53 try {
54 ServerSocket sskt = new ServerSocket(port);
55
56 while(!closeConnection) {
57
58 Socket sock = sskt.accept();
59
60 ServerSideClientIO sck = new ServerSideClientIO(this, sock);
61
62 this.serverSideClientIOList.add(sck);
63
64 Thread t = new Thread(sck);
65 t.start();
66
67 }
68
69 //code here
ClypeServer.java
70 sskt.close();
71
72 } catch (UnknownHostException e) {
73 System.err.println("Unknown Host: " + e.getMessage());
74 } catch (NoRouteToHostException e) {
75 System.err.println("Server unreachable " + e.getMessage());
76 } catch (ConnectException e) {
77 System.err.println("Connection refused: " + e.getMessage());
78 } catch (IOException e) {
79 System.err.println(e.getMessage());
80 }
81
82 }
83
84 /**
85 *
86 * Getter method for port.
87 *
88 * @return the port of the server
89 */
90 public int getPort() {
91 return port;
92 }
93
94 public int getNextID() {
95 nextID ++;
96 return nextID;
97 }
98
99 /**
100 *
101 * Sends data to all connected clients
102 *
103 * @param dataToBroadcastToClients the data to be sent to all the clients
104 */
105 public synchronized void broadcast(ClypeData dataToBroadcastToClients) {
106
107 for(ServerSideClientIO s : this.serverSideClientIOList) {
108 if(!s.isClosed()) {
109 s.setDataToSendToClient(dataToBroadcastToClients);
110 s.sendData();
111 }
112 }
113
114 }
115
116 /**
117 *
118 * Removes a client from the connected client list
119 *
120 * @param s ServerSideClientIO object to be removed
121 */
122 public synchronized void remove(ServerSideClientIO s) {
123 this.serverSideClientIOList.remove(s);
124 }
125
126 /**
127 * @return a string of all the connected users
128 */
ClypeServer.java
129 public String getAllUsers() {
130 String r = "";
131 for(ServerSideClientIO s : this.serverSideClientIOList)
132 r += s.getUsername() + "\n";
133
134 return r;
135 }
136
137 /**
138 * @return a string of all the connected users
139 */
140 public String getAllUsers1() {
141 String r = "";
142 for(ServerSideClientIO s : this.serverSideClientIOList)
143 r += s.getUsername() + "." + s.getClientid() + "\n";
144
145 return r;
146 }
147
148 /**
149 * Overriden equals(Object obj) method, which makes sure the obj is not null and also
makes
150 * sure obj is of type ClypeServer. This method makes checks if the following are equal:
151 * port, and closeConnection.
152 */
153 @Override
154 public boolean equals(Object obj) {
155 if(this == obj)
156 return true;
157 if(obj == null || !(obj instanceof ClypeServer))
158 return false;
159
160 ClypeServer c = (ClypeServer) obj;
161 if(c.getPort() == this.getPort() && c.closeConnection == this.closeConnection)
162 return true;
163 else
164 return false;
165 }
166
167 /**
168 * Overriden hashCode() method.
169 */
170 @Override
171 public int hashCode() {
172 //from lecture notes
173 int result = 17;
174 result = 37*result + this.getPort();
175 return result;
176 }
177
178 /**
179 * Overriden toString() method that returns/prints the following:
180 * port, whether or not the connection is closed, data to send to client, data to recieve
from client
181 */
182 @Override
183 public String toString() {
184
185 return "Port: " + this.getPort() + "\n"
ClypeServer.java
186 + "Connection is closed: " + this.closeConnection + "\n";
187
188 }
189
190
191
192 /**
193 * @param args port_number
194 */
195 public static void main(String args[]) {
196 ClypeServer server;
197 if(args.length == 0)
198 server = new ClypeServer();
199 else
200 server = new ClypeServer(Integer.parseInt(args[0]));
201
202 server.start();
203 }
204
205 }
206

You might also like