Java Reference
In-Depth Information
serverConnection.acceptAndOpen();
System.out.println ("Connection to established.");
sendAnswer (clientConnection);
}
} catch (IOException e) {
System.out.println (e.toString());
}
}
public void sendAnswer (StreamConnection s) throws IOException {
OutputStream o = s.openOutputStream();
Date date = new Date (System.currentTimeMillis());
o.write (("HTTP/1.1 200 OK\r\n\r\n"
+"<HTML><H1>This is a GCF based HTTP Server.</H1>"
+ date.toString() + "\r\n\r\n").getBytes());
o.close();
s.close();
System.out.println ("Connection closed.");
}
}
Note
Please note, that the procedure described previously is based on the CLDC 1.0 spec. MIDP 2.0
introduces a new PushRegistry class that can to be used with the PushListener interface in
order to establish inbound socket connections.
HTTP Connections
HTTP is the only communication protocol that must be supported by all MIDP and PDAP devices. The
HTTP support must include the HTTP protocol version 1.1 and the HEAD , GET , and POST requests as
described in RFC2616, which can be found at http://www.ietf.org/rfc/ .
HTTP is the default protocol for transmitting HTML Web pages. The HTTP protocol is based on a
request/response paradigm where a client establishes a connection to a server that is listening on the
TCP port number 80 by default. Both the HTTP request and response consist of three parts: the request
or response line, header entries, and the actual payload data. The payload is separated from the header
by a single empty line. Line breaks are indicated by a sequence consisting of a carriage return and a
line feed control character ( \r\n ).
The client initiates the HTTP connection by sending a request line. The request line consists of a
method (for example, GET ), the URL identifying the requested page, and the protocol version, all
separated by space characters. The following lines contain header lines and the actual payload. The
server processes the request and sends back a response to the client containing the status of the server,
its own header information, and the requested information.
The following sample lines, taken from an actual HTTP transfer, illustrate the protocol. Note that the
request payload is empty in this simple case. Actually, the request payload is empty in most HTTP
requests for HTML pages except from POST requests transmitting, for example, form data to the server.
A client sends a request for the root page (/) with an additional header indicating the device profile and
configuration:
GET / HTTP/1.1
User-Agent: Profile/MIDP-1.0 Configuration/CLDC-1.0
 
Search WWH ::




Custom Search