j2me Socket通信例子2014-10-20 16:52:10
( 还没有投票,继续加油! )
客户端代码:
package test7www; 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 Test7_2Client extends MIDlet implements CommandListener { private Form form = null; private Display display = null; private TextField field = null; private Command send = null; private SocketConnection sc = null; public Test7_2Client() { form = new Form("客户端"); display = Display.getDisplay(this); field = new TextField("文本信息", "", 100, TextField.ANY); send = new Command("发送", Command.OK, 1); form.setCommandListener(this); form.addCommand(send); form.append(field); } protected void startApp() throws MIDletStateChangeException { display.setCurrent(form); } public void commandAction(Command c, Displayable d) { if (c == send) { new SendMessage().start(); } } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } class SendMessage extends Thread { private DataOutputStream dos = null; public void run() { try { sc = (SocketConnection) Connector .open("socket://localhost:50000"); dos = sc.openDataOutputStream(); dos.writeUTF(field.getString()); } catch (Exception e) { e.printStackTrace(); } finally { try { if (dos != null) dos.close(); if (sc != null) sc.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
服务器端代码:
package test7www; import java.io.DataInputStream; 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 Test7_2Server extends MIDlet implements CommandListener { private Form form = null; private Display display = null; private TextField field = null; private Command accept = null; private ServerSocketConnection ssc = null; private SocketConnection sc = null; private String message = null; public Test7_2Server() { form = new Form("服务端"); display = Display.getDisplay(this); field = new TextField("文本信息", "", 100, TextField.ANY); accept = new Command("接受", Command.OK, 1); form.setCommandListener(this); form.addCommand(accept); form.append(field); new AcceptMessage().start(); } public void commandAction(Command c, Displayable d) { if (c == accept) { field.setString(message); } } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { display.setCurrent(form); } class AcceptMessage extends Thread { private DataInputStream dis = null; public void run() { while (true) { try { ssc = (ServerSocketConnection) Connector .open("socket://:50000"); sc = (SocketConnection) ssc.acceptAndOpen(); dis = sc.openDataInputStream(); message = dis.readUTF(); System.out.println("Server : " + message); } catch (Exception e) { e.printStackTrace(); } finally { try { if (dis != null) dis.close(); if (sc != null) sc.close(); if (ssc != null) ssc.close(); } catch (IOException e) { e.printStackTrace(); } } } } } }
上一篇:j2me联网测试
下一篇:j2me多线程的应用实例