Generic Connection Framework (GCF):
GCF was originally defined to rely on the J2ME
platform's Connected Limited Device Configuration (CLDC), version
1.0, because the familiar J2SE java.net and java.io APIs were considered too
large to fit into the constrained memory available in mobile devices.
Today you can find the GCF not only in CLDC-based profiles, such
as the Mobile Information Device Profile (MIDP) and the Information
Module Profile (IMP), but also in Connected Device Configuration (CDC)-based
profiles, such as the Foundation Profile and its relatives the Personal Basis
Profile and Personal Profile - and now, with JSR 197, on the J2SE platform as
well. You can also find the GCF in an increasing number of optional packages,
including those that provide Bluetooth support and access to files and smart
cards.
The GCF has the interfaces and classes to create connections such as
HttpConnection, HttpsConnection ... and to perform the IO operations.
GCF has the following interfaces are given:
.Connection The most basic type of connection in the GCF. All other
connection types extend Connection.
.ContentConnection Manages a connection, such as HTTP, for passing
content, such as HTML or XML. Provides basic methods for inspecting the content
length, encoding and type of content.
.Datagram Acts as a container for the data passed on a Datagram
Connection.
.DatagramConnection Manages a datagram connection.
.InputConnection Manages an input stream-based connection.
.OutputConnection Manages an output stream-based connection.
.StreamConnection Manages the capabilities of a stream. Combines the
methods of both InputConnection and .OutputConnection.
.StreamConnectionNotifier Listens to a specific port and creates a
StreamConnection as soon as activity on the port is detected.
Connector The Connector class is used to create instances of a connection
protocol using one of Connector’s static methods. The Connector class is not
designed to be instantiated. It is simply used to create instances of a
connection protocol. All of the methods Connector defines are static and serve
this purpose. The Connector defines three variations of an open() that return a
Connection instance.
open(String name)
open(String name, int mode)
open(String name, int mode, boolean timeouts)
The name is essentially a URI and is composed of three parts: a scheme, an
address, and a parameter list. The general form of the name parameter is as
follows:
HttpConnection conn =
(HttpConnection) Connector.open("http://http://www.r4r.co.in/");
conn.setRequestMethod(HttpConnection.GET);
DataInputStream din = conn.openDataInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[256];
while (true) {
int rd = din.read(buf, 0, 256);
if (rd == -1) break;
bos.write(buf, 0, rd);
}
bos.flush();
buf = bos.toByteArray();
// Convert the byte array to string using the
// character encoding format of the remote site
String content = new String (buf, "UTF-8");
HttpsConnection:
The HttpsConnection object represents a secure HTTP (HTTPS) connection. HTTPS
support is mandated in MIDP 2.0 . It supports both user authentication and data
encryption. The URL scheme for HttpsConnection is https://. Ex:
HttpsConnection hsc =
(HttpsConnection) Connector.open("https:"/www.r4r.co.in);
The HTTPS protocol utilizes the public key infrastructure to authenticate
communication parties and ensure data confidentiality once the connection is
established. Digital certificates are used for authentication, and public key
algorithms are used to exchange the information
SocketConnection A socket connection allows the client to communicate to
specific socket ports. The URL string of SocketConnection objects takes the
format of socket://host:port. The important methods in the SocketConnection
interface are (i) openDataInputStream() and (ii) openDataOutputStream(), which
open data streams for the client to communicate with the remote socket.
We can also specify the operation mode of the SocketConnection using the
setSocketOption() method. void setSocketOption (byte option, int value) int
getSocketOption (byte option
SecureConnection A SecureConnection interface extends the
SocketConnection interface to add support for the Secure Socket Layer (SSL). Its
URL string takes the form ssl://host:port
ServerSocketConnection The ServerSocketConnection allows us to listen for
incoming connections on a socket port and essentially turns the device into a
server this is not available in MIDP 1.0. Its URL string has the format of
socket://:port, where the port parameter is the port number to listen to. An
external device can now connect to the server device using the socket://host:port
connection string, where host is the IP address of the server device. Once a
ServerSocketConnection object is instantiated, we can call the acceptAndOpen()
method in a loop to listen for incoming connections.
// Create the server listening socket for port 57
ServerSocketConnection scn =
(ServerSocketConnection) Connector.open("socket://:57");
while (true) {
// Wait for a connection
SocketConnection sc =
(SocketConnection) scn.acceptAndOpen();
sc.setSocketOption(RCVBUF, 128);
sc.setSocketOption(SNDBUF, 128);
// do something with sc
sc.close ();
}
scn.close ();
CommConnection This supports access to the device's serial port or
logical serial port (e.g., the IrDA port). The URL scheme takes the form
comm:port_id;params. The port_id is COM# for RS232 serial ports and IR# for IrDA
ports. For example, the URL string comm:IR0;baudrate=19200 opens a connection to
the first infrared port with a 19,200 bit per second data rate.
UDPDatagramConnection The datagram protocol directly passes data packets
between endpoints without trying to assemble them to match their originating
order or recover lost data. The datagram arrival order or even the delivery
itself is not guaranteed. It is, however, often faster and more suitable for
real-time applications.
The UDPDatagramConnection interface inherits from the DatagramConnection
interface. The URL string is datagram://host:port. It cannot open data streams
but has methods to support composing, sending, and receiving datagrams.
Datagram newDatagram(int i)
Datagram newDatagram(int i, String s)
Datagram newDatagram(byte[] bytes, int i)
Datagram newDatagram(byte[] bytes, int i, String s)
void send(Datagram datagram)
void receive(Datagram datagram)
Read next ,Generic Connection Framework (GCF),Network Example: