📄 clientsocketdemo.java
字号:
/*
* ClientSocketDemo.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 ClientSocketDemo extends MIDlet
implements Runnable, CommandListener {
private Display display = null;
private Form form;
private TextField tfData;
private StringItem siMsg;
private StringItem siRecData;
//定义使用的命令按钮
private Command cmdExit;
private Command cmdSend;
private Command cmdConnect;
private Command cmdDisConnect;
private boolean stopFlag;
InputStream is;
OutputStream os;
SocketConnection sc;
Sender sender;
public ClientSocketDemo() {
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 (sc != null) {
sc.close();
sc = null;
}
if (sender != null) {
sender.stopFlag = true;
sender = null;
}
} catch (IOException ioe) {
}
}
public void run() {
try {
sc = (SocketConnection) Connector.open("socket://localhost:9999");
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) {
Alert a = new Alert("客户端", "请首先运行服务器,并进入监听状态",
null, AlertType.ERROR);
a.setTimeout(Alert.FOREVER);
a.setCommandListener(this);
display.setCurrent(a);
stop();
} finally {
stop();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -