j2me Socket通信例子2014-10-20 16:52:10

( 还没有投票,继续加油! )
分享:
31.3K

客户端代码:

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();
          }
        }
      }
    }
  }
         
}






头像

snowcoal
  • j2me
  • Socket
  • 客户端
  • 服务端

收藏到我的私密空间

标题:j2me Socket通信例子

作者:柳岸花明

你暂未登录,请登录后才可收藏至您的私密空间 确认取消
雪炭网

键盘操作 更便捷 -雪炭网雪中送炭-乐趣无限

如果本站的内容有幸帮助到了您,建议您了解一下当页的广告内容哦,我们的进步离不开您的支持,Thank you~