Table of ContentsGame APIPush Registry

Communications

MIDP 2 provides revised support for communications under the GCF (Generic Connec tion Framework). This includes support for server sockets, secure connections, and low-level connections using TCP or UDP. Read on to take a brief look at each of these.

Server Sockets

In MIDP 1 you could only create an outbound (client) connection. This meant communications had to originate from the MID and go outward to a server. In order to receive inbound connections, MIDP 2 added support for a server socket. You can instantiate one using the GCF's open method passed in with the type socket,but without the destination host portion specified. For example:

// create a server socket by excluding the host portion
ServerSocketConnection server = (ServerSocketConnection)
Connector.open("socket://:8888");

This will return a new ServerSocketConnection object linked to port 8888.

You then need to put the server socket into a wait mode so that it will accept incoming connections. This will block (sit there waiting until something happens) when you call it, so you would normally do this in another thread to your main application. To wait for a connection, use the acceptAndOpen method. When a new connection comes, this method will return a new SocketConnection object that represents the connection between your server and the new remote client.

// wait for a connection
SocketConnection client = (SocketConnection) server.acceptAndOpen();

You can then use the client SocketConnection just like you would any other type of connection. To read the data transmitted to you, use a DataInputStream; to send data, use a DataOut putStream.

// open streams linked to this client socket
DataInputStream dis = client.openDataInputStream();
DataOutputStream dos = client.openDataOutputStream();

// read client data
String result = is.readUTF();
// process request and send response
os.writeUTF(...);

When you're finished, remember to close any socket or stream instances you have.

// close streams and connections
is.close();
os.close();
client.close();
server.close();

NOTE

Tip

Typically the best way to handle server socket processing is to create a central thread that handles incoming connections and another thread for each new client connection as it comes in. When the connection is then closed, you can remove the thread.

This method will let you communicate with clients, as well as accept other connections, without blocking on any one part of the process.

Secure Connections

MIDP 2 adds mandatory support for secure connections in the form of HTTPS (HTTP using the secure sockets layer). This provides for the encryption of information transmitted over the network.

You can get access to an HTTPS connection using the HttpsConnection object. For example:

HttpsConnection c = (HttpsConnection)Connector.open("https://www.secure.com/");

All communications over this socket will be encrypted automatically.

Low-Level Communications

MIDP 1 provided very minimal communications based on HTTP. With MIDP 2, you have access to lower-level IP socket types using either the TCP (streaming) or UDP (datagram) protocols.

Like with server sockets, the Connector.open method acts as a factory for new connections. To create a new stream socket use the form:

SocketConnection c = (SocketConnection)Connector.open("socket://host:port");

And for a datagram connection use:

UDPDatagramConnection c = (UDPDatagramConnection)Connector.open("datagram://host:port");Then you can use these connections as typical TCP and UDP sockets.

    Table of ContentsGame APIPush Registry