Networking is still based on the Connector.open
class.
Other types of connection are added
(SocketConnection) Connector.open("socket://...")
(HttpsConnection) Connector.open("https://...")
(SSLConnection) Connector.open(ssl://...")
(ServerSocketConnection) Connector.open(socket://:80
starts a server on port 80
(UDPDatagramConnection) Connector.open("datagram://...")
(CommConnection) new Connector.open("comm:...")
.class
files and these can be run
directly from the emulator
manifest.mf
must be created using a text
editor to hold information
about the class files, entry point (where main()
is)
etc. This is standard to all versions of Java
.jar
file is created to hold the class files and
manifest file, using the jar
command
.jad
file is created using a text editor.
This includes the location of the jar file (maybe as a URL)
inet
server: a single server that listens on lots of
ports,and starts the appropriate service when a message
arrives
MIDlet-Push-1: socket://:79, com.sun.example.SampleChat, *
startApp()
method when a TCP connection is made on port 79
jad
file
startApp() {
// we have been started because there is a connection for us
open connection
handle message(s)
exit the midlet
}
startApp() {
if (first time this midlet has run)
register the midlet
exit
else
open connection
handle message(s)
exit
}
while
loop - the midlet
just handles one session and then exits. The while
loop
in previous servers is handled by the AMS
exit
? By calling notifyDestroyed()
.
For safety (ensure it cleans up) also call destroyApp()
// exit
destroyApp();
notifyDestroyed();
String[] connections = PushRegistry.listConnections();
if (connections == null || connections.length == 0) {
// first time - so register
} else {
// called by AMS in response to incoming connection
}
Spacer
allows control over spacing between elements
in a Form
Sprite
contains multiple images that can displayed
to give simple animations
Layer
is a superclass for visual elements in
games. A Sprite
is a subclass of Layer
TiledLayer
is used for e.g. backgrounds made
up of tiled images
CustomItem
can be subclassed to define your
own additional elements for Forms
MIDP 2.0 has a number of classes for handling multi-media
Player
Manager
Control
ToneControl
VolumeControl
PlayerListener
Controllable
The CLDC device can tell you what protocols and what media types it can handle
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
public class MediaInfo extends MIDlet {
public MediaInfo() {
}
public void startApp() {
String [] protocols = Manager.getSupportedProtocols(null);
System.out.println("Supported protocols are:");
for (int n = 0; n < protocols.length; n++) {
System.out.println("- " + protocols[n]);
String [] contentTypes =
Manager.getSupportedContentTypes(protocols[n]);
System.out.println(" Supported content types are:");
for (int m = 0; m < contentTypes.length; m++) {
System.out.println(" " + contentTypes[m]);
}
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
Manager.playTone(int tone, int duration, int volume)
can be used to pplay a single tone
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
public class Tone1 extends MIDlet {
protected Display display;
protected Form form;
public Tone1() {
display = Display.getDisplay(this);
form = new Form("Info entry form");
}
public void startApp() {
display.setCurrent(form);
try {
Manager.playTone(ToneControl.C4, 200, 100);
// sleep about the same time as the
// tone plays for
Thread.sleep(2000);
// the next tone may be early, late
// or maybe on time
Manager.playTone(80, 2000, 100);
} catch(MediaException e) {
System.out.println(e.toString());
} catch(InterruptedException e) {
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
display = null;
form = null;
}
}
Player p = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR)
Manager.TONE_DEVICE_LOCATO
for information on using this
The following server uses ServerSocketConnection
to listen
to connections on port 8189. Each message that is received on this
port is echo'ed back to the client
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import java.io.*;
public class EchoServer extends MIDlet implements Runnable {
public static int MYECHOPORT = 8189;
public void run() {
ServerSocketConnection s = null;
try {
s = (ServerSocketConnection) Connector.open("socket://:" +
MYECHOPORT);
} catch(IOException e) {
System.out.println(e);
return;
}
while (true) {
try {
// Wait for a connection.
SocketConnection sc = (SocketConnection) s.acceptAndOpen();
handleSocket(sc);
} catch(IOException e) {
break;
}
}
}
public void startApp() {
new Thread(this).start();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public static void handleSocket(SocketConnection incoming)
throws IOException {
System.out.println("Got connection");
DataInputStream din = incoming.openDataInputStream();
DataOutputStream dout = incoming.openDataOutputStream();
boolean done = false;
while ( ! done) {
String str = din.readUTF();
if (str == null) {
done = true;
System.out.println("Null received");
}
else {
System.out.println("Echo: " + str);
dout.writeUTF(str);
}
}
din.close();
dout.close();
incoming.close();
}
}
This client connects to a server on port 8189 and sends two messages
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import java.io.*;
public class EchoClient extends MIDlet implements Runnable {
public static final int MYECHOPORT = 8189;
public void run() {
String address = "localhost";
SocketConnection sock = null;
DataInputStream din = null;
DataOutputStream dout = null;
System.out.println("Opening connection");
try {
sock = (SocketConnection) Connector.open("socket://" +
address + ":" +
MYECHOPORT);
din = sock.openDataInputStream();
dout = sock.openDataOutputStream();
} catch(IOException e) {
e.printStackTrace();
System.exit(3);
}
System.out.println("Opened connection");
String response = null;
try {
// Just send a few messages, for testing
dout.writeUTF("Hello");
response = din.readUTF();
System.out.println("Response: " + response);
dout.writeUTF("Bye");
response = din.readUTF();
System.out.println("Response: " + response);
} catch(IOException e) {
e.printStackTrace();
return;
}
return;
}
public void startApp() {
// run all the socket handling code
// in a separate thread
new Thread(this).start();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
} // Client
J2ME does not have the full range of IO classes that are in
the J2SE. For example, it does not have BufferedReader
and PrintStream
that are useful for reading and writing
lines of text. It only has InputStream
,
DataInputStream
,
OutputStream
and DataOutputStream
.
To read and write strings using In/OutputStream
, you need
to turn the strings into byte arrays
InputStream in = ...;
OutputStream out = ...;
String msg = "hello";
byte[] barray = msg.getBytes();
out.write(barray);
in.read(barray);
msg = new String(barrray);
To read and write strings using DataIn/OutputStream
,
use the UTF methods
DataInputStream din = ...;
DataOutputStream dout = ...;
String str = din.readUTF();
dout.writeUTF(str);