/src/ChatAppServer/EchoServer.java

https://bitbucket.org/blitz2145/cop4331-chat-application · Java · 101 lines · 25 code · 10 blank · 66 comment · 1 complexity · 43fe03e8a639adf0e7d680d0a8075a2b MD5 · raw file

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