Java Reference
In-Depth Information
If there is currently a HTTPServer listening to the specified port, an IOException will be thrown
as described in the section " Creating a Connection ." To track the status of the HTTPServer that is
printed to the console using the System.out.println() method, you can just write the results
into a text widget of MIDP or PDAP.
The HTTP server application can also be run on a desktop machine without a MIDP or PDAP
environment emulated. In order to do so, you need either to compile the SUN GCF for the desktop, or
to access the ME4SE CLDC emulation available from http://www.me4se.org .
A short description of the strings that are transferred between client and server is given in the next
section, " HTTP Connections ."
Listing 6.1 shows the complete source code of the HTTP server. Figure 6.2 shows the HTML sample
page that is sent to a Web browser by the HTTP thread if it is included in a server application.
Figure 6.2. The output of the HTTP server implementation shown in Listing 6.1 when
connected with a Netscape 4.7 Web browser.
Listing 6.1 HttpServer.java—A Simple HTTP Server Thread Sending a Headline and the
Actual Time to the Connecting Web Browser as an Identification String
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
public class HttpServer extends Thread {
StreamConnectionNotifier serverConnection;
int port;
public HttpServer (int port) throws IOException {
this.port = port;
serverConnection =
(StreamConnectionNotifier) Connector.open
("serversocket://:"+port);
}
public void run() {
try {
while (true) {
System.out.println ("Waiting for connection on port
"+port);
StreamConnection clientConnection =
 
 
Search WWH ::




Custom Search