Java Reference
In-Depth Information
Figure 14.1 Running MicroServer from the command line. The display here shows
the output to the console after a request for a file arrived.
This short program provides a basic web server that returns web files to browsers
and any other client program that connects to the port and uses the proper HTTP
protocol. The Socket and ServerSocket classes, along with the I/O stream
classes, do most of the work. The complete code listing appears in the Web Course
for Chapter 14.
14.4 Running the server
We run our server application from the command line with
> java MicroServer 1234
The program takes a port number in the command line. (On a Unix platform
you should pick a port number above 1023.) Figure 14.1 shows an example of
running MicroServer from the console with the port 1234. The server prints
to the console when a client connects to it.
In our directory with MicroServer we put a simple web page file named
message.html containing:
< html >
< head >
<TITLE>Message Test</TITLE>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
</HEAD>
<BODY BGCOLOR="#FFFFFF">
A file sent by the MicroServer.
</body>
</html>
Figure 14.2 shows the file displayed in a browser that connected to our
MicroServer .For testing with both the server and browser running on the
same machine you can use the special loopback IP address 127.0.0.1. The URL
must explicitly include the port number since the server uses a different number
than the default HTML port 80.
Search WWH ::




Custom Search