The Simple Web Server web80

This very simple Web server is designed to operate two ways:

• Without the socket server

• With the socket server

This will help illustrate the problem and how the problem was solved. Listing 17.5 shows the web80.c

TIP:

module.

example Listing 17,5: web8Q.c—The Simple Web Server Module web8Q.c

3: * This is an extremely simple Web server:

5: * This program runs in two modes:

10: * In this mode, this program functions

11: * as a very simple standalone Web server.

12: * However, it must run as root to bind

18: * In this mode, this program contacts

19: * the sockserv server to request a

20: * socket bound to port 80. If sockserv

21: * allows the request, it returns

22: * a port 80 socket. This allows this

23: * program to run without root and

24: * with no setuid requirement.

26: #include "common.h" 27:

28: int

43: * If any arguments are present on the

44: * command line, obtain the socket

45: * without help from the server (run

46: * in simple standalone mode):

50: * Standalone mode:

66: * Bind the Web server address-

67: * we need to be root to succeed

74: bail("binding port 80");

77: * Turn on SO_REUSEADDR:

79: z = setsockopt(s,SOL_SOCKET,

80: SO_REUSEADDR,&b,sizeof b);

86: * Run in sockserv mode: Request

95: * Now make this a listening socket:

102: * Perform a simple, Web server loop for

103: * demonstration purposes. Here we just

104: * accept one line of input text, and

105: * ignore it. We provide one simple

106: * HTML page back in response:

110: * Wait for a connect from browser:

118: continue;

120:

122: * Create streams for convenience, and

123: * just eat any Web command provided:

127: fgets(getbuf,sizeof getbuf,rx);

128:

13 0: * Now serve a simple HTML response.

131: * This includes this Web server's

132: * process ID and the current date

137: "<TITLE>Test Page for this little "

141: "<H1>web8 0 Worked!</H1>\n"/tx); 142:

144: fprintf(tx,

145: "<H2>From PID %ld @ %s</H2>\n",

14 9: fputs("</BODY>\n" 150: "</HTML>\n",tx); 151:

The very simple Web server in Listing 17.5 uses the following basic procedure:

1. If command-line arguments are provided, a socket is created and bound with bind(2) if it can (lines 49 to 83). This allows you to prove that you need root access to obtain such a port under Linux.

2. If no command line arguments are provided, the code between lines 89 and 91 is executed instead. Line 89 calls upon a function reqport() to obtain a socket on port 80.

3. The function listen(2) is called to allow connections to this socket (lines 97 to 99).

4. The for loop allows for a continuous number of client connections to take place to this Web server (line 108).

5. Lines 112 to 154 form the body of the for loop, which simply waits for one text line of input and then sends some formatted HTML back to the client in return. The client's socket is closed at lines 152 and 153.

Note in lines 144 to 147 that the process ID of the Web server is reported back to the browser so that you can have confirmation of where this information came from.

Copyright © Macmillan Computer Publishing, Inc.

Was this article helpful?

0 0

Post a comment