程序比较乱,望能给予参考。更复杂的写法,自己开发
服务器端代码:
public class SocketServer1 extends MIDlet {
private ServerSocketConnection ssc;
private Display display;
private Form myForm;
private Command setUpCommand;
private boolean isConnect=true;
public SocketServer1() {
display = Display.getDisplay(this);
myForm = new Form("显示客户端信息");
setUpCommand = new Command("启动服务器", Command.OK, 1);
//打开一个连接端口
try {
ssc = (ServerSocketConnection) Connector.open("socket://:5003");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void startApp() throws MIDletStateChangeException {
display.setCurrent(myForm);
myForm.addCommand(setUpCommand);
myForm.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable arg1) {
if(c==setUpCommand){
new ReceiveRequestThread().start();
}
}
});
}
class ReceiveRequestThread extends Thread {
public void run() {
while (isConnect) {
try {
System.out.println("连接请求");
SocketConnection sc = (SocketConnection) ssc
.acceptAndOpen();
System.out.println("请求成功");
//启动服务器代理
new ServerAgent(sc).start();
} catch (IOException e) {
System.out.println("断开连接");
isConnect=false;
}
}
}
}
class ServerAgent extends Thread{
SocketConnection sc;
public DataInputStream din;
public DataOutputStream dout;
boolean flag = false;
public ServerAgent(SocketConnection _sc){
sc=_sc;
try {
din=sc.openDataInputStream();
dout=sc.openDataOutputStream();
dout.writeUTF("欢迎你的连接");
} catch (IOException e) {
System.out.println("断开连接");
isConnect=false;
}
flag=true;
}
public void run() {
while(flag){
try {
String msg = din.readUTF();
if(msg.startsWith("<#LOGIN#>")){
myForm.append(msg.substring(9)+"\n");
}
} catch (IOException e) {
System.out.println("断开连接");
}
}
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
}
客户端:
public class SocketClient1 extends MIDlet {
private Display display;
private Form myForm;
private Command SendCommand;
private TextField tfName;
private SocketConnection sc;
private DataOutputStream dos;
private DataInputStream dis;
private int count=1;
public SocketClient1() {
display = Display.getDisplay(this);
myForm = new Form("显示客户端信息");
SendCommand = new Command("发送", Command.OK, 1);
tfName = new TextField("姓名:", "", 10, TextField.ANY);
}
protected void startApp() throws MIDletStateChangeException {
display.setCurrent(myForm);
myForm.append(tfName);
myForm.addCommand(SendCommand);
try {
sc = (SocketConnection) Connector.open("socket://127.0.0.1:5003");
dos = sc.openDataOutputStream();
dis= sc.openDataInputStream();
System.out.println("AAAAAAAAAAAAAAAAA");
myForm.setTitle(dis.readUTF());
} catch (IOException e) {
e.printStackTrace();
}
myForm.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable arg1) {
if (c == SendCommand) {
String msg = tfName.getString();
try {
dos.writeUTF("<#LOGIN#>"+msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
}
}