注意在聯網的時候我們還是必須要在另一個線程內進行聯網操作,而不能再主線程進行。這樣的目的是為了避免用戶界面的堵塞。通常我們可以定義一個類擴展Thread類並提供一個把MIDlet作為參數的構造器,為了提高效率我們通過使用wait()和notify()方法,這樣只有在用戶按下按鈕的時候,線程才被喚醒去執行聯網操作,否則再啟動後他一直出去"wait"的狀態。
class CommandThread extends Thread {
MIDlet parent;
StreamConnection socket = null;
InputStream is = null;
boolean exit = false;
public CommandThread(MIDlet parent) {
this.parent = parent;
}
public void run() {
while (!exit) {
synchronized(parent) {
while(!commandAvailable) {
try {
parent.wait();
}
catch (InterruptedException e) {
}
}
}
......................
..................
TCP/IP服務器的寫法和一般的服務器寫法是一樣的,因此不再多介紹。如果有問題可以參考Java.Net包的內容。下面通過例子演示一下,我們構建一個時間服務器,在手機端來通過socket訪問並得到當前時間顯示在屏幕上。代碼如下:
import Java.io.BufferedOutputStream;
import Java.io.DataOutputStream;
import Java.io.IOException;
import Java.Net.Socket;
import Java.Net.ServerSocket;
import Java.util.Date;
public class DayTimeServer {
public static void main(String args[]) {
int dayTimePort = 13;
if (args.length == 1) {
try {
dayTimePort = Integer.parseInt(args[0]);
}
catch (NumberFormatException e) {
System.out.println("invalid port number");
System.exit(0);
}
}
ServerSocket serverSocket = null;
Socket sock;
DataOutputStream dataout;
try {
serverSocket = new ServerSocket(dayTimePort);
}
catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
System.exit(0);
}
while (true) {
try {
sock = serverSocket.accept();
dataout = new DataOutputStream(new BufferedOutputStream
(sock.getOutputStream()));
String dateString = new Date().toString();
dataout.write(dateString.getBytes(),0,dateString.length());
dataout.flush();
sock.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
}
import Javax.microedition.midlet.*;
import Javax.microedition.lcdui.*;
import Java.io.*;
import Javax.microedition.io.*;
public class DayTimeClIEnt extends Javax.microedition.midlet.MIDlet
implements CommandListener {
Display display;
private boolean commandAvailable;
CommandThread commandThread;
List menu;
Form outputForm;
StringItem dt;
Command cmdBack;
Command cmdExit;
private static final String PROTOCOL = "socket:";
private String dayTimeURL;
public void startApp() {
String host = getAppProperty("HOST");
String port = getAppProperty("DAYTIME_PORT");
try {
(Integer.parseInt(port));
}
catch (NumberFormatException e) {
destroyApp(false);
notifyDestroyed();
}
dayTimeURL = PROTOCOL + "//" + host + ":" + port;
display = Display.getDisplay(this);
outputForm = new Form("Date/Time");
dt = new StringItem(null,null);
outputForm.append(dt);
cmdBack = new Command("Back",Command.BACK,1);
outputForm.addCommand(cmdBack);
cmdExit = new Command("Exit",Command.EXIT,1);
outputForm.addCommand(cmdExit);
outputForm.setCommandListener(this);
menu = new List("Menu",List.IMPLICIT);
menu.append("Get Date/Time",null);
menu.append("Exit",null);
menu.setCommandListener(this);
display.setCurrent(menu);
commandAvailable = false;
commandThread = new CommandThread(this);
commandThread.start();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command cmd, Displayable d) {
if (cmd == cmdExit) {
destroyApp(false);
notifyDestroyed();
}
else if (cmd == cmdBack) {
display.setCurrent(menu);
}
else if ((d == menu) && (cmd == List.SELECT_COMMAND)) {
synchronized (this) {
commandAvailable = true;
notify();
}
}
}
class CommandThread extends Thread {
MIDlet parent;
StreamConnection socket = null;
InputStream is = null;
boolean exit = false;
public CommandThread(MIDlet parent) {
this.parent = parent;
}
public void run() {
while (!exit) {
synchronized(parent) {
while(!commandAvailable) {
try {
parent.wait();
}
catch (InterruptedException e) {
}
}
}
commandAvailable = false;
switch (menu.getSelectedIndex()) {
case 0:
getDate();
break;
case 1:
exit = true;
}
}
destroyApp(false);
notifyDestroyed();
}
public void getDate() {
try {
socket =
(StreamConnection)Connector.open(dayTimeURL,
Connector.READ, true);
is = socket.openInputStream();
}
catch (Exception e) {
}
try {
int b;
StringBuffer sb = new StringBuffer();
while ( (b = is.read()) != -1) {
sb.append((char)b);
}
socket.close();
dt.setText(sb.toString());
display.setCurrent(outputForm);
}
catch (Exception e) {
}
}
}
}
你需要在jad文件中定義兩個屬性值,當然你也可以直接寫在源代碼中
DAYTIME_PORT: 13
HOST: 127.0.0.1