Generic Connection Framework (GCF) by R4R Team

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: :

; The mode parameter allows the connection to be established in various access modes, such as read-only, read-write and write-only. These modes are defined by the Connector constants READ, READ_WRITE, and WRITE. The timeouts parameter is a flag indicating whether or not the connection should throw an InterruptedIOException if a timeout occurs.


some important Supported URLs in the GCF ar given below:

http://host:port
Make an HTTP connection to a remote server. Supported on all devices.


https://host:port

Make an HTTPS connection to a remote server Supported on Developer Platform 2.0 devices.


socket://host:port
Make a socket connection to a remote server.


ssl://host:port

Make a secure socket connection to a remote server.


socket://:port
Listen for incoming socket connections at a local port.


comm://IR# or nokiacomm://IR# or comm://COM#
Connect to the device's IR or serial port.


datagram://host:port
Send or receive UDP datagrams.

HttpConnection HTTP is the most popular data protocol of the Internet. Most wireless data networks support HTTP on top of their native wireless data carrier layers. The MIDP specification mandates support for HTTP version 1.1 on all Java handsets. We can use HTTP connections to pass both text and binary data between clients and servers.

HttpConnection hc = (HttpConnection) Connector.open("http://www.r4r.co.in");
HTTP data is transferred over continuous streams. Hence, the HttpConnection inherits from the StreamConnection interface. The two most important methods in HttpConnection open data streams to and from the server.

DataInputStream getDataInputStream ()
DataOutputStream getDataOutputStream ()


The HttpConnection interface supports both HTTP GET or POST operations.


In a GET operation, data is retrieved from the specified URL. We can customize the URL to pass request arguments. In this case, we only need to call the openDataInputStream() method to make the physical connection and read the data from the DataInputStream.

In a POST operation, we have to open a DataOutputStream to the specified URL first and write the request parameters to the stream. A connection is made, and the request is submitted when we close the DataOutputStream. Then we open a DataInputStream to retrieve the server response.
 

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:

Leave a Comment:
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!