main.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <iostream>
  10. #include "ServerThread.h"
  11. #include "HTTPServer.h"
  12. #include <pthread.h>
  13. using namespace std;
  14. extern char echo_buffer[100 *1024];
  15. // command symbol table
  16. enum _cmd {
  17. cunknown,
  18. cexit
  19. };
  20. _cmd str2cmd(string cmdstr) {
  21. if (cmdstr == "x" || cmdstr == "exit" || cmdstr == "quit")
  22. return cexit;
  23. return cunknown;
  24. }
  25. int main(int argc, char *argv[]) {
  26. string cmd;
  27. bool running = true;
  28. int portno = 8021;
  29. // the first argument is always the work directory.
  30. if (argc == 2) {
  31. // get port number from arguments
  32. portno = atoi(argv[1]);
  33. echo_buffer[0] = '\0';
  34. }
  35. if (argc >= 3) {
  36. // get port number from arguments
  37. portno = atoi(argv[1]);
  38. snprintf(echo_buffer, sizeof(echo_buffer), "%s", argv[2]);
  39. }
  40. HTTPServer* server = new HTTPServer(portno);
  41. server->StartServer();
  42. // without the terminator, only the client can close the connection
  43. server->StartTerminator();
  44. while (running) {
  45. cin >> cmd;
  46. if (cmd != "")
  47. switch (str2cmd(cmd)) {
  48. case cexit:
  49. running = false;
  50. break;
  51. default:
  52. cout << "Unrecognized command." << endl;
  53. break;
  54. }
  55. }
  56. server->Stop();
  57. return 0;
  58. }