Java Reference
In-Depth Information
("User-Agent", Profile/MIDP-1.0 Configuration/CLDC-1.0);
InputStream is = httpConnection.openInputStream();
// read data from server here
is.close();
httpConnection.close();
}
catch (IOException e) {
// put exception handling here...
}
The most important request property is the Request method. The default Request method is GET . The
GET request method is used by Web browsers for requesting HTML pages from Web servers. HTTP
does not allow GET requests to have side effects on the server. Thus, if data will be submitted to the
server, the POST method must be used. For example, Web browsers use POST requests for sending the
content of HTML forms to the server. HTML editors can use POST requests to update HTML pages
stored at the server. The openOutputStream() method can be used to open a stream for writing
data to the server.
A complete application example for running a chat system over HTTP using the GET and POST
methods is given in the section " A Simple HTTP Based Client-Server Chat Application . " For full
coverage of the HTTP protocol refer to RFC2616, which can be found at http://www.ietf.org/rfc .
Datagram Connections
Datagram connections provide a mechanism for transferring simple data packets between two
applications. In contrast to TCP socket connections, UDP datagram connections are not reliable. Thus,
DatagramConnection s can only be used for connections where packet losses are acceptable.
Typical applications of datagram connections are streaming or real-time applications. The advantage of
datagrams is that their transport produces less protocol overhead. Thus, the performance for datagrams
can be higher than TCP performance.
Datagram connections can be used as server and client connections as well. The protocol name for both
client and server datagram connections is datagram:// . For client connections, the protocol name is
followed by the host address and the port, separated by a colon. For server connections, the host is
omitted and just the colon and the port are given. For example, a client datagram connection to port
1234 of the server myserver.some.com is set up by the following line of code:
DatagramConnection datagramConn =
(DatagramConnection)Connector.open
("datagram://myserver.some.com:1234");
Each datagram that is sent over a datagram connection is a small data packet consisting of the
destination address and a buffer containing the payload data. In J2ME, datagrams are encapsulated in
the Datagram class. Datagram objects are created using the newDatagram() method of a
datagram connection. The newDatagram() method takes the buffer containing the data and the size
of the buffer to be transferred. It then returns a new Datagram object. A datagram that is created in
this way automatically contains the receiver address of the connection.
The following snippet creates a Datagram object containing a "Hello World" string:
String helloWorldString = new String ("Hello World");
byte[] buffer = helloWorldString.getBytes();
Datagram myDatagram = datagramConn.newDatagram(buffer, buffer.length);
Datagrams are sent using the method send() of the DatagramConnection :
Search WWH ::




Custom Search