In this network programming homework, we are asked to build a Remote Batch System (RBS). It’s actually a combination of client and server, whose diagram can be drawn as follows:

rbs_diagram

As required in spec(hw3Spec.txt), we have to write three programs:

  1. CGI: a webpage(hw3.cgi) that displays server’s response dynamically.
  2. HTTP Server: a cpp program that runs as a HTTP server.
  3. Winsock: a combination of 1. and 2. but using Winsock.

For more details, please refer to hw3Spec.txt in the repository (cyhuang1230/NetworkProgramming).

There are some notable ideas I came up with and described as follows.

For CGI (Part I):

  • Write may fail. Write a wrapper class to handle this.
  • Parsing query string needs to be handled carefully.
  • Since we’re client now, doesn’t matter which port to use, i.e. no need to bind anymore!
  • Try using flag(bitwise comparison) instead of individually specifying each property in log function.
  • Output recerived from servers should be handled(e.g. \n, <, >, &, ', ") since JavaScript has different escape characters than C. Also, the order of processing characters matters.
  • Most importantly, non-blocking client design.
        [1] connect may be blocked due to slow connection.
            => Set O_NONBLOCK to sockfd
        [2] Client program may block (e.g. sleep)
            => Don’t wait for response (i.e. read) after write to server. Instead, use select to check when the response is ready to read.

For HTTP Server (Part II):

  • Use select to accept connections.
  • Fork a child process to handle request.
  • Plain text file can be read & write directly. Content type needs to be set properly.
  • CGI needs to be execed. => dup its STDOUT_FILENO to sockfd.

For Winsock (Part III):

  • Same logic as Berkeley Socket version.
  • To read file, use ReadFile instead of ReadFileEx (I encountered pointer error)
  • Keep track of doneMachines. When done (== numberOfMachines), close socket and print footer.
  • I like Berkeley Socket much more than Winsock :(

Demo video:
np_hw3_demo

Source → cyhuang1230/NetworkProgramming

For part I (CGI):
Please look for NP_project3_1.cpp.
To try: make hw3_1_debug, upload to a HTTP server and run <BASE_URL>/hw3.cgi.

For part II (HTTP Server):
Please look for NP_project3_2.cpp.
To try: make hw3_2_debug and run ./np_hw3.

For part III (Winsock):
Please look for NP_project3_3.cpp. Run with Visual Studio is recommended.