开发J2ME低级联网应用[2]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

    while (true) {
      try {
        sock = serverSocket.accept();
        dataout = new DataOutputStream(new BufferedOutputStream
          (sock.getOutputStream()));
        String dateString = new Date().toString();
        dataout.write(dateString.getBytes(),0,dateString.length());
        dataout.flush();
        sock.close();
        }
      catch (IOException e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
      }
    }
  }
}

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

import java.io.*;
import javax.microedition.io.*;

public class DayTimeClient extends javax.microedition.midlet.MIDlet
    implements CommandListener {

  Display display;

  private boolean commandAvailable;
  CommandThread commandThread;
 
  List menu;
  Form outputForm;
  StringItem dt;
     
  Command cmdBack;
  Command cmdExit;

  private static final String PROTOCOL = "socket:";
 
  private String dayTimeURL;

  public void startApp() {
    String host = getAppProperty("HOST");
    String port = getAppProperty("DAYTIME_PORT");
    try {
      (Integer.parseInt(port));
    }
    catch (NumberFormatException e) {
      destroyApp(false);
      notifyDestroyed();
    }
   
    dayTimeURL = PROTOCOL + "//" + host + ":" + port;
    display = Display.getDisplay(this);
    outputForm = new Form("Date/Time");
    dt = new StringItem(null,null);
    outputForm.append(dt);
    cmdBack = new Command("Back",Command.BACK,1);
    outputForm.addCommand(cmdBack);
    cmdExit = new Command("Exit",Command.EXIT,1);
    outputForm.addCommand(cmdExit);
    outputForm.setCommandListener(this);

    menu = new List("Menu",List.IMPLICIT);
    menu.append("Get Date/Time",null);
    menu.append("Exit",null);
    menu.setCommandListener(this);
    display.setCurrent(menu);

    commandAvailable = false;
    commandThread = new CommandThread(this);
    commandThread.start();
  }


  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command cmd, Displayable d) {
    if (cmd == cmdExit) {
      destroyApp(false);
      notifyDestroyed();
    }
    else if (cmd == cmdBack) {
      display.setCurrent(menu);
    }
    else if ((d == menu) && (cmd == List.SELECT_COMMAND)) {
      synchronized (this) {
        commandAvailable = true;
        notify();
      }
    }
  }   

  class CommandThread extends Thread {
    MIDlet parent;

    StreamConnection socket = null;
    InputStream is = null;

    boolean exit = false;

    public CommandThread(MIDlet parent) {
      this.parent = parent;
    }

本文关键:开发J2ME低级联网应用
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top