/src/ChatAppServer/EchoServer.java
Java | 101 lines | 25 code | 10 blank | 66 comment | 1 complexity | 43fe03e8a639adf0e7d680d0a8075a2b MD5 | raw file
1package ChatAppServer;
2import java.io.*;
3import java.net.*;
4
5
6public class EchoServer {
7
8 /**
9 * Manages accounts for chat clients that connect to it. Communication is handled
10 * through the following textual protocol:
11 *
12 * FORMAT:
13 * <ACTION>
14 * Send:
15 * <VERB> [<DATA> | <DATA>... ] <TERMINATOR>
16 * Recieve:
17 * <VERB> [<DATA> | <DATA>... ] <TERMINATOR>
18 *
19 * PROTOCOL SPECIFICATION:
20 * <LOGIN>
21 * Send:
22 * LOGIN <USERNAME> <PASSWORD>\n\n
23 * Recieve:
24 * LOGIN_SUCCESS\n\n
25 * or
26 * LOGIN_FAIL\n\n
27 *
28 * <CHECK USERNAME>
29 * Send:
30 * CHECK_USER <USERNAME>\n\n
31 * Recieve:
32 * USER_OK\n\n
33 * or
34 * USER_TAKEN\n\n
35 *
36 * <CREATE ACCOUNT>
37 * Send:
38 * CREATE_USER <USERNAME> <PASSWORD> <EMAIL>\n\n
39 * Recieve:
40 * USER_SUCCESS\n\n
41 * or
42 * USER_FAIL\n\n
43 *
44 * <CHECK ONLINE>
45 * Send:
46 * IS_ONLINE <USERNAME>\n\n
47 * Recieve:
48 * TRUE\n\n
49 * or
50 * FALSE\n\n
51 *
52 * <SET OFFLINE>
53 * Send:
54 * LOGOFF <USERNAME>\n\n
55 * Recieve:
56 * No response
57 *
58 * <GET IP>
59 * Send:
60 * GET_IP <USERNAME>\n\n
61 * Recieve:
62 * <IP>\n\n
63 * or
64 * FAIL\n\n
65 *
66 * @param args
67 */
68 public static void main(String[] args) throws IOException {
69 // Create our account database
70 db = new AccountDB();
71
72 // Declare a Socket to bind to.
73 ServerSocket sockem = null;
74
75 // Bind to port 4444
76 sockem = new ServerSocket(4444);
77
78 try {
79 while (true) {
80 // Blocks until client connects
81 System.out.println("Waiting for Client...");
82 Socket rockem = sockem.accept();
83
84 // Client connected
85 System.out.println("Client " + rockem.getInetAddress().toString() + " connected on PORT: " + rockem.getLocalPort());
86 System.out.println("Starting new thread to handle client...");
87
88 Runnable r = new ClientHandler(rockem, db);
89 Thread clientHandlerThread = new Thread(r);
90 clientHandlerThread.start();
91 }
92 } catch (Exception e) {
93 e.printStackTrace();
94 }
95
96 // Close the server socket and release the port
97 sockem.close();
98 }
99
100 private static AccountDB db;
101}