13,097
社区成员




import java.io.DataInputStream;
import java.io.DataOutputStream;
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 ServerSocketConnectionDemo extends MIDlet
implements Runnable, CommandListener {
private Display display = null;
private Form form = new Form("Socket服务器演示");
//用于输入要发送的消息
private TextField tfData = new TextField("请输入发送的消息",
"",255,TextField.ANY);
//定义使用的命令按钮
private Command cmdExit = new Command("退出", Command.EXIT, 0);
private Command cmdSend = new Command("发送", Command.SCREEN, 1);
//线程运行标志
private boolean isRunning = true;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private ServerSocketConnection scn = null;
public ServerSocketConnectionDemo() {
super();
form.append(tfData);
form.addCommand(cmdExit);
form.addCommand(cmdSend);
form.setCommandListener(this);
}
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
display.setCurrent(form);
//开始服务器线程
new Thread(this).start();
}
protected void pauseApp() {
//暂停时要关闭Socket连接
close();
}
protected void destroyApp(boolean arg0)
throws MIDletStateChangeException {
close();
}
/**
* 处理命令按钮事件
*/
public void commandAction(Command cmd, Displayable d) {
if (cmd == cmdExit) {
isRunning = false;
notifyDestroyed();
} else if(cmd == cmdSend) {
//发送数据
new Thread() {
public void run() {
if (dos == null) {
return;
}
String str = tfData.getString();
try {
dos.writeUTF(str);
} catch (IOException e) {
e.printStackTrace();
}
//显示发送的消息
form.append("[S] " + str + "\n");
}
}.start();
}
}
/**
* 服务器线程
*/
public void run() {
isRunning = true;
try {
scn = (ServerSocketConnection) Connector.open("socket://:9999");
form.append("连接端口9999...\n");
form.append("Local address: " + scn.getLocalAddress()+"\n");
form.append("Local Port: " + scn.getLocalPort()+"\n");
//等待客户端的连接
SocketConnection sc = (SocketConnection)scn.acceptAndOpen();
form.append("连接已经接受\n");
//获得输入流
dis = sc.openDataInputStream();
//获得输出流
dos = sc.openDataOutputStream();
while (isRunning) {
String str = dis.readUTF();
//休息1秒
Thread.sleep(1000);
//显示接收到的消息
form.append("[R] " + str + "\n");
}
sc.close();
sc = null;
form.append("连接已经关闭");
} catch (Exception e) {
e.printStackTrace();
//客户端关闭连接
form.append("客户端连接断开,重新监听");
close();
new Thread(this).run();
} finally {
close();
}
}
/**
* 关闭Socket连接
*/
public void close() {
try {
isRunning = false;
if (dis != null) {
dis.close();
dis = null;
}
if (dos != null) {
dos.close();
dos = null;
}
if (scn != null) {
scn.close();
scn = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
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 ClientSocketConnectionDemo extends MIDlet
implements Runnable, CommandListener {
private Display display = null;
private Form form = new Form("Socket客户端演示");
//用于输入服务器地址
private TextField tfAddress = new TextField("服务器地址",
"127.0.0.1",255,TextField.ANY);
//用于输入服务器端口
private TextField tfPort = new TextField("服务器端口",
"9999",6,TextField.ANY);
//用于输入要发送的消息
private TextField tfData = new TextField("请输入发送的消息",
"",255,TextField.ANY);
private Command cmdExit = new Command("退出", Command.EXIT, 0);
private Command cmdSend = new Command("发送", Command.SCREEN, 1);
private Command cmdConnect = new Command("连接", Command.SCREEN, 1);
//线程运行标志
private boolean isRunning = true;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private SocketConnection scn = null;
public ClientSocketConnectionDemo() {
super();
form.append(tfAddress);
form.append(tfPort);
form.append(tfData);
form.addCommand(cmdExit);
form.addCommand(cmdConnect);
form.setCommandListener(this);
}
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
display.setCurrent(form);
}
protected void pauseApp() {
close();
}
protected void destroyApp(boolean arg0)
throws MIDletStateChangeException {
close();
}
/**
* 处理命令按钮事件
*/
public void commandAction(Command cmd, Displayable d) {
if (cmd == cmdExit) {
isRunning = false;
notifyDestroyed();
} else if(cmd == cmdSend) {
//发送数据
new Thread() {
public void run() {
if (dos == null) {
return;
}
String str = tfData.getString();
try {
dos.writeUTF(str);
} catch (IOException e) {
e.printStackTrace();
}
//显示发送的消息
form.append("[S] " + str + "\n");
}
}.start();
} else if (cmd == cmdConnect) {
form.removeCommand(cmdConnect);
form.addCommand(cmdSend);
//开始客户端线程
new Thread(this).start();
}
}
/**
* 客户端线程
*/
public void run() {
isRunning = true;
try {
String constr = "socket://"+ tfAddress.getString()
+ ":" + tfPort.getString();
scn = (SocketConnection) Connector.open(constr);
scn.setSocketOption(SocketConnection.DELAY, 0);
scn.setSocketOption(SocketConnection.LINGER, 5);
scn.setSocketOption(SocketConnection.KEEPALIVE, 0);
scn.setSocketOption(SocketConnection.RCVBUF, 128);
scn.setSocketOption(SocketConnection.SNDBUF, 128);
form.append("连接"+constr+"\n");
form.append("Local address: " + scn.getLocalAddress()+"\n");
form.append("Local Port: " + scn.getLocalPort()+"\n");
//获得输入流
dis = scn.openDataInputStream();
//获得输出流
dos = scn.openDataOutputStream();
while (isRunning) {
String str = dis.readUTF();
//休息1秒
Thread.sleep(1000);
//显示接收到的消息
form.append("[R] " + str+"\n");
}
form.append("连接已经关闭");
} catch (Exception e) {
e.printStackTrace();
//服务器关闭连接
form.append("服务器连接断开");
close();
} finally {
close();
}
}
/**
* 关闭Socket连接
*/
public void close() {
try {
isRunning = false;
if (dis != null) {
dis.close();
dis = null;
}
if (dos != null) {
dos.close();
dos = null;
}
if (scn != null) {
scn.close();
scn = null;
}
form.removeCommand(cmdSend);
form.addCommand(cmdConnect);
} catch (Exception e) {
e.printStackTrace();
}
}
}