j2me 基于socke t通信

服务器端:

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; //ServerSocketMidlet.java /** * @author boolean [email protected]: * @version 创建时间:2009-2-19 下午02:39:51 * @TODO 服务器端 */ public class ServerSocketMidlet extends MIDlet { private Display display; public ServerSocketMidlet() { display = Display.getDisplay(this); } protected void startApp() throws MIDletStateChangeException { display.setCurrent(new ServerForm("Server", display)); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } } /** * * SocketTest * * @author boolean [email protected] 402662067 * @version 创建时间:2009-2-19 下午05:13:32 TODO 服务器端的界面 */ class ServerForm extends Form implements Runnable, CommandListener { private ServerSocketConnection serverSocketConn = null; private SocketConnection socketConnection = null; /** 发送信息时候用的数据流 */ private DataOutputStream dos = null; /** 接受时候用的数据流 */ private DataInputStream dis = null; /** 要说的话 */ private TextField send = null; /** 接受的话 */ private TextField recive = null; private Display display; public ServerForm(String title, Display display) { super(title); System.out.println("serverForm"); this.display = display; send = new TextField("要说的话:", null, 34, TextField.ANY); recive = new TextField("收到:", null, 34, TextField.ANY); try { serverSocketConn = (ServerSocketConnection) Connector .open("socket://:9999"); socketConnection = (SocketConnection) serverSocketConn .acceptAndOpen(); dis = socketConnection.openDataInputStream(); dos = socketConnection.openDataOutputStream(); } catch (IOException e) { e.printStackTrace(); } this.append(send); this.append(recive); this.addCommand(new Command("send", Command.OK, 1)); this.setCommandListener(this); new Thread(this).start(); } /** * 开辟新线程去接收信息 */ public void run() { while (true) { try { recive.setString(dis.readUTF()); } catch (IOException e) { e.printStackTrace(); } finally { // try { // dis.close(); // } catch (IOException e) { // e.printStackTrace(); // } } } } public void commandAction(Command c, Displayable d) { if (c.getLabel().equals("send")) { try { dos.writeUTF(send.getString()); } catch (IOException e) { e.printStackTrace(); } finally {// 注意 千万不可以在这里关闭流,应该在程序结束时候关闭 // try { // dos.close(); // } catch (IOException 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; //ServerSocketMidlet.java /** * @author boolean [email protected]: * @version 创建时间:2009-2-19 下午02:39:51 * @TODO */ public class ClientSocketMidlet extends MIDlet { private Display display; public ClientSocketMidlet() { display = Display.getDisplay(this); } protected void startApp() throws MIDletStateChangeException { display.setCurrent(new ClientForm("Client", display)); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } } class ClientForm extends Form implements Runnable, CommandListener { private SocketConnection scClient = null; /** 发送信息时候用的数据流 */ private DataOutputStream dos = null; /** 接受时候用的数据流 */ private DataInputStream dis = null; /** 要说的话 */ private TextField send = null; /** 接受的话 */ private TextField recive = null; private Display display; public ClientForm(String title, Display display) { super(title); System.out.println("clientForm"); this.display = display; send = new TextField("要说的话:", null, 34, TextField.ANY); recive = new TextField("收到:", null, 34, TextField.ANY); try {//192.168.1.102 测得数据 scClient = (SocketConnection) Connector .open("socket://127.0.0.1:9999"); dis = scClient.openDataInputStream(); dos = scClient.openDataOutputStream(); } catch (IOException e) { e.printStackTrace(); } this.append(send); this.append(recive); this.addCommand(new Command("send", Command.OK, 1)); this.setCommandListener(this); new Thread(this).start(); } public void run() { while (true) { try { recive.setString(dis.readUTF()); } catch (IOException e) { e.printStackTrace(); } finally { // try { // dis.close(); // } catch (IOException e) { // e.printStackTrace(); // } } } } public void commandAction(Command c, Displayable d) { if (c.getLabel().equals("send")) { try { dos.writeUTF(send.getString()); } catch (IOException e) { e.printStackTrace(); } finally { // try { // dos.close(); // } catch (IOException e) { // e.printStackTrace(); // } } } } }

你可能感兴趣的:(thread,服务器,command,dos,null,j2me)