?? serversocketdemo.java
字號:
/*
* ServerSocketDemo.java
*
* Created on 2005年5月5日, 下午2:39
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
/**
*
* @author Liu Bin
* @version
*/
public class ServerSocketDemo extends MIDlet
implements Runnable, CommandListener {
private Display display = null;
private Form form;
private TextField tfData;
private StringItem siMsg;
//定義使用的命令按鈕
private Command cmdExit;
private Command cmdSend;
private Command cmdConnect;
private Command cmdDisConnect;
private boolean stopFlag;
InputStream is;
OutputStream os;
ServerSocketConnection scn;
Sender sender;
public ServerSocketDemo() {
cmdExit = new Command("退出", Command.EXIT, 0);
cmdSend = new Command("發送", Command.SCREEN, 1);
cmdConnect = new Command("監聽", Command.SCREEN, 2);
cmdDisConnect = new Command("斷開", Command.SCREEN, 2);
tfData = new TextField("請輸入發送的消息",
"",255,TextField.ANY);
siMsg = new StringItem("當前狀態:", "空閑");
form = new Form("Socket服務器端演示");
form.append(siMsg);
form.append(tfData);
form.addCommand(cmdExit);
form.addCommand(cmdConnect);
form.setCommandListener(this);
}
public void startApp() {
display = Display.getDisplay(this);
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
stop();
}
/**
* 處理命令按鈕事件
*/
public void commandAction(Command cmd, Displayable d) {
if (cmd == cmdExit) {
notifyDestroyed();
}
if (cmd == cmdSend) {
if (sender != null) {
String str = tfData.getString();
sender.send(str);
form.append("[S} " + str + "\n");
}
}
if (cmd == cmdConnect) {
form.removeCommand(cmdConnect);
form.addCommand(cmdSend);
form.addCommand(cmdDisConnect);
start();
}
if (cmd == cmdDisConnect) {
form.removeCommand(cmdDisConnect);
form.removeCommand(cmdSend);
form.addCommand(cmdConnect);
stopFlag = true;
}
if (cmd == Alert.DISMISS_COMMAND) {
display.setCurrent(form);
}
}
public void start() {
stopFlag = false;
Thread t = new Thread(this);
t.start();
}
public void stop() {
try {
stopFlag = true;
if (is != null) {
is.close();
is = null;
}
if (os != null) {
os.close();
os = null;
}
if (scn != null) {
scn.close();
scn = null;
}
if (sender != null) {
sender.stopFlag = true;
sender = null;
}
} catch (IOException ioe) {
}
}
public void run() {
try {
siMsg.setText("等待連接");
scn = (ServerSocketConnection) Connector.open("socket://:9999");
form.append("Local address: " + scn.getLocalAddress());
form.append("Local Port: " + scn.getLocalPort());
//等待一個連接
SocketConnection sc = (SocketConnection)scn.acceptAndOpen();
siMsg.setText("連接已經接受");
is = sc.openInputStream();
os = sc.openOutputStream();
sender = new Sender(os);
while (!stopFlag) {
StringBuffer sb = new StringBuffer();
int c = 0;
while (((c = is.read()) != '\n') && (c != -1)) {
sb.append((char) c);
}
if (c == -1) {
break;
}
//顯示接收到的消息
form.append("[R] " + sb.toString() + "\n");
}
siMsg.setText("連接已經關閉");
} catch (Exception e) {
if (e.getMessage().equals("ServerSocket Open")) {
Alert a = new Alert("服務器端", "Port 9999已經被占用",
null, AlertType.ERROR);
a.setTimeout(Alert.FOREVER);
a.setCommandListener(this);
display.setCurrent(a);
} else {
if (!stopFlag) {
e.printStackTrace();
}
}
} finally {
stop();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -