Java Reference
In-Depth Information
Before describing the mandatory HTTP protocol and other connections, we will cover the optional
socket connections because they form the basis for higher-level protocols such as HTTP or FTP.
Actually, it might be possible to create a CLDC-based device without display, implementing a Web
server as a user interface. Examples might be configurable network routers or production control
hardware.
Note
We cannot cover all the underlying protocols in full detail here, so we will mainly focus on the Java
API. For complete coverage, refer to the corresponding standard specifications or other protocol
specific literature.
General Socket Connections
Socket and datagram connections are simple basic IP connection types. Although sockets and
datagrams are usually available on machines connected to the Internet, for wireless connections the
situation is different. Carriers often use a proprietary protocol over the air, and allow only special
connection types such as HTTP connections. Thus, although sockets are usually required for HTTP
connections, CLDC devices might provide HTTP but no socket connections. The main difference
between sockets and datagrams is that sockets are a reliable and end-to-end connection established for
a period of time, whereas datagrams are simple data packets with a limited length that might never
reach their destination without notifying the sender.
TCP socket connections are usually not symmetric, but a client connects to a server. The server listens
to a specific port, usually depending on the protocol that is used. For example, the default port used by
HTTP servers is 80, and the default port for FTP is 21. A simple method to test the socket functionality
in general is to connect to an existing server and to display what the server sends. Here, a good
candidate is the clock server, usually listening on port 13. For each incoming connection, it sends its
current date and time back to the client. If a certain server supports the time, service can easily be
tested by connecting to that port with a simple terminal program (telnet). You can find a list of time
servers at http://www.boulder.nist.gov/timefreq/service/time-servers.html .
Here, we will first describe the client side of TCP/IP sockets and then sketch how to set up a server
socket. (For general coverage of TCP/IP, refer to TCP/IP Unleashed by Karanjit Rom Siyan and Tim
Parker, ISBN 0672323516.)
Client-Socket Connections
The parameter given to the open() method in order to establish a socket connection at the client side
consists of the protocol identifier (such as socket:// ), the IP address of the server that will be
connected (such as time-a.nist.gov ), and a port number that is separated from the host address
using a colon ( :13 ). For example,
StreamConnection streamConnection =
(StreamConnection) connector.open ("socket://time-a.nist.gov:13");
When a socket connection is opened, the returned instance always implements the
StreamConnection 's interface, providing access to corresponding input and output streams.
In the following code snippet, a socket connection to a time server is established, and then the time is
read from the corresponding input stream:
try {
Connection connection = Connector.open ("socket://time-
a.nist.gov:13");
Search WWH ::




Custom Search