分享

j2me模板3

 偶尔疯狂 2011-07-18
服务器端
Java code
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(); } } } } }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多