NP HW3: Remote Batch System (RBS)
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:
As required in spec(hw3Spec.txt), we have to write three programs:
CGI: a webpage(hw3.cgi) that displays server’s response dynamically.HTTP Server: a cpp program that runs as a HTTP server.Winsock: a combination of 1. and 2. but usingWinsock.
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
bindanymore! - Try using
flag(bitwise comparison) instead of individually specifying each property inlogfunction. - 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]connectmay be blocked due to slow connection.
=> SetO_NONBLOCKto sockfd
[2] Client program may block (e.g.sleep)
=> Don’t wait for response (i.e.read) afterwriteto server. Instead, useselectto check when the response is ready toread.
For HTTP Server (Part II):
- Use
selectto accept connections. - Fork a child process to handle request.
- Plain text file can be
read&writedirectly. Content type needs to be set properly. - CGI needs to be
execed. =>dupitsSTDOUT_FILENOto sockfd.
For Winsock (Part III):
- Same logic as
Berkeley Socketversion. - To read file, use
ReadFileinstead ofReadFileEx(I encountered pointer error) - Keep track of
doneMachines. When done (== numberOfMachines), close socket and print footer. - I like
Berkeley Socketmuch more thanWinsock:(
Demo video:

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.