Back | Tutorial Home | Next
There are various IPC(Inter Process Communications) approaches used in the desktop environment like
Here each application runs as a separate process and can interact with the other processes. With introduction of MIDlet concurrency in MIDP 3.0, the MIDlet to MIDlet communication seems to be an important fallout. Now Two MIDlets can communicate with each other using IMC(Inter MIDlet Communication). This protocol is similar to Socket communication protocol. There are two parties here
IMC client
javax.microedition.io.IMCConnection is used for creating IMC client connection
| (IMCConnection)Connector.open("imc://<MIDlet
UID>:<Server name> : <Server version>; [<Authmode>]); |
Example : (IMCConnection)Connector.open("imc://*com.j2mesalsa.MyServer:1.0 ; false);
Parameter definitions of the connection string is given below
MIDlet UID: This
is a unique identifier of a MIDlet suite containing MIDlet name, vendor name
and version. If the client want to connect to any MIDlet then * is used.
Server name: The class name of the Server.
Server version: MIDlet-Version attribute of the server. Backward compatibility
is supported.
Authmode: Define whether the client is authorized to access IMC server
Code to create a IMC client connection
void imcClientRequest() throws IOException {
IMCConnection con = null;
DataInputStream is = null;
DataOutputStream os = null;
try {
// Create connection to IMC server Class
(com.j2mesalsa.MyServer) defined in another MIDlet
con = (IMCConnection)Connector.open(
"imc://*:com.j2mesalsa.MyServer:1.0;authmode=false");
// Open outbound connection
os = con.openDataOutputStream();
// write data to output stream
String sentData = “I am J2ME=ED, Are You?”;
os.write(sentData.getBytes());
os.flush();
// Get response from IMC server
is = con.openDataInputStream();
} catch (ClassCastException e) {
// Do your exception handling here
} finally {
if (is != null)
is.close();
if (os != null)
os.close();
if (con != null)
con.close();
}
}
|
As you should have already guessed from the above code. The code is similar to other Network client connections like HTTP and UDP.
Class IMCConnection has the following getter methods
IMC Server
javax.microedition.io.IMCServerConnection is used to create an IMC Server connection
(IMCServerConnection)Connector.open("imc://:<Server
name> :<Server version>; [<Authmode>]);
|
Example: (IMCServerConnection)Connector.open( |
Parameter definitions of the connection string is given below
Server name: The
class name of the Server.
Server version: MIDlet-Version attribute of the server. Backward compatibility
is supported.
Authmode: Define whether the client is authorized to access IMC server
Code to create IMC Server connection
void imcServerConnection() throws IOException {
IMCServerConnection serverCon = null;
IMCConnection con = null;
DataInputStream is = null;
DataOutputStream os = null;
try {
// Create connection to IMC server Class(com.j2mesalsa.MyServer)
// defined in another MIDlet
IMCServerConnection serverCon = (IMCServerConnection)Connector.
open("imc://: com.j2mesalsa.MyServer:1.0;authmode=true);
while(true) {
con = (IMCConnection) serverCon.acceptAndOpen();
// Open outbound connection
os = con.openDataOutputStream();
// Open Inbound Connection
is = con.openDataInputStream();
// read data sent by the IMC client
String stringFromIMCClient = is.readString();
// write data to output stream
String sentData = “Yes, I am J2ME-ED”;
os.write(sentData.getBytes());
}
} catch (ClassCastException e) {
// Do your exception handling here
} finally {
if (is != null)
is.close();
if (os != null)
os.close();
if (con != null)
con.close();
if (serverCon!= null)
serverCon.close();
}
}
|
Configure the server for auto launch
You will have to configure
the server to push registry so that the server can auto launch whenever the request
comes from the IMC client.
MIDlet-Push-1:imc://:com.j2mesalsa.MyServer:1.0;authmode=true, com.j2mesalsa.MyServer, * |
MIDlet_UID is set to *
here to allow any IMC client to connect to this IMC server, Alternatively this
registration can be done in the code using PushRegistry.registerConnection().
Class IMCServerConnection has the following getter methods
Interface MIDletIdentity
As discussed in the earlier section this interface has been added to MIDP 3.0 to facilitate the identification of the MIDlet during IMC. If a MIDlet-A is communicating with another MIDlet-B then MIDlet-A can query for the following parameters of MIDlet-B.
IMCConnection con = (IMCConnection)Connector.open( "imc://*:com.j2mesalsa.MyServer:1.0;authmode=false"); MIDletIdentity identity = con.getRemoteIdentity(); // MIDletIdentity gives // four methods to query parameters of the remote MIDlet String midletSuiteName = identity.getName(); String midletVersion = identity.getVersion(); String midletVendor = identity.getVendor(); boolean authorization = identity.isAuthorized() |
Back
| Tutorial
Home | Next
More Tutorials
MIDP for Beginners
MIDP Gaming
JSR 179: Location Based Services
This page is a part of
a frames based web site. If you have landed on this page from a search engine
click here to view the complete page.