package test7www;
import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Test7_2Server extends MIDlet implements CommandListener {
private Form form = null;
private Display display = null;
private TextField field = null;
private Command accept = null;
private ServerSocketConnection ssc = null;
private SocketConnection sc = null;
private String message = null;
public Test7_2Server() {
form = new Form("服务端");
display = Display.getDisplay(this);
field = new TextField("文本信息", "", 100, TextField.ANY);
accept = new Command("接受", Command.OK, 1);
form.setCommandListener(this);
form.addCommand(accept);
form.append(field);
new AcceptMessage().start();
}
public void commandAction(Command c, Displayable d) {
if (c == accept) {
field.setString(message);
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
display.setCurrent(form);
}
class AcceptMessage extends Thread {
private DataInputStream dis = null;
public void run() {
while (true) {
try {
ssc = (ServerSocketConnection) Connector
.open("socket://:50000");
sc = (SocketConnection) ssc.acceptAndOpen();
dis = sc.openDataInputStream();
message = dis.readUTF();
System.out.println("Server : " + message);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dis != null)
dis.close();
if (sc != null)
sc.close();
if (ssc != null)
ssc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}