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
bind
anymore! - Try using
flag
(bitwise comparison) instead of individually specifying each property inlog
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.
=> SetO_NONBLOCK
to sockfd
[2] Client program may block (e.g.sleep
)
=> Don’t wait for response (i.e.read
) afterwrite
to server. Instead, useselect
to check when the response is ready toread
.
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
exec
ed. =>dup
itsSTDOUT_FILENO
to sockfd.
For Winsock (Part III):
- Same logic as
Berkeley Socket
version. - To read file, use
ReadFile
instead ofReadFileEx
(I encountered pointer error) - Keep track of
doneMachines
. When done (== numberOfMachines
), close socket and print footer. - I like
Berkeley Socket
much 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.