鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 手机系统开发 > J2ME > >

J2ME网络应用程序性能探讨

来源:互联网 作者:佚名 时间:2015-08-02 08:06
欢迎进入IT技术社区论坛,与200万技术人员互动交流 >>进入 5、在无线应用网络连接术语中有呼叫建立延迟(Call Setup latency)和通话信号传输延迟(Intra-call latency)两种,从以上数据可以看出,第一种延迟要比后一种延迟要明显一些,即第一次连接时用户需要
欢迎进入IT技术社区论坛,与200万技术人员互动交流 >>进入

  5、在无线应用网络连接术语中有呼叫建立延迟(Call Setup latency)和通话信号传输延迟(Intra-call latency)两种,从以上数据可以看出,第一种延迟要比后一种延迟要明显一些,即第一次连接时用户需要等待更多的时间;

 以下是本文的代码片断:

public class Game extends MIDlet implements CommandListener {
 private Display display;
 private Form fmHTTP;
 private Form fmHTTP2;
 private Form fmStream;
 private Form fmSocket;
 private Command cmdTest   = new Command("测试", Command.BACK, 0);
 private Command cmdStream = new Command("STREAM", Command.ITEM, 1);
 private Command cmdSocket = new Command("SOCKET", Command.ITEM, 2);
 private Command cmdHTTP   = new Command("HTTP", Command.ITEM, 3);
 private TestThread testThread;
 public Game() {
     display = Display.getDisplay(this);
     
     fmHTTP = new Form("1.HTTP测试");
     fmHTTP.addCommand(cmdStream);
     fmHTTP.addCommand(cmdSocket);
     fmHTTP.addCommand(cmdHTTP2);
     fmHTTP.addCommand(cmdExit);
     fmHTTP.setCommandListener(this);
     fmHTTP.append("HTTP协议网络测试工具用于检测手机是否具备HTTP网络连接能力。第一行数据是由服务器产生的,经过UTF编码和DES加密传输到手机上的。\n");
        
     fmStream = new Form("3.STREAM测试");
     fmStream.addCommand(cmdHTTP);
     fmStream.addCommand(cmdHTTP2);
     fmStream.addCommand(cmdSocket);
     fmStream.setCommandListener(this);
     fmStream.append("STREAM网络连接是走的socket端口,对应的服务器端口是10000。\n");
     testThread = new TestThread(getAppProperty("Server").toLowerCase().trim());
  }


 class TestThread implements Runnable {
   private String server;
   private String message;
   private Form curForm;
   private int responseCode;
   private InputStream is2;
   private HttpConnection conHttp;
      
   private String keyOptimize;
   private HttpConnection conOptimize;
      
   public TestThread(String server) {
     this.server = server;
   }
        
   public void start(Form curForm) {
     this.curForm = curForm;
     Thread t = new Thread(this);
     t.start();    
   }
                
   public void run() {
     message = "";
     long start,timeused;
     int formid = Integer.parseInt(curForm.getTitle().substring(0,1));
     start = System.currentTimeMillis(); //网络用时测试起点
     if (formid==1) {  //1.HTTP测试
      try {
        String key = genPassword();
        //产生8位的随机字符串密码,用于返回给服务器进行DES加密
        conHttp = (HttpConnection) Connector.open("http://" + server + ":8080/ns/hellodes?r=" + key);
        responseCode = conHttp.getResponseCode();
        if (responseCode==HttpConnection.HTTP_OK){
         InputStream is = conHttp.openInputStream();
         int len = (int) conHttp.getLength();
         byte[] bytestr = new byte[len];
         is.read(bytestr); //直接从输入流中读取字节,                   cipher = new DesCipher(key.getBytes());
         for (int i = 0; i < bytestr.length / 8; i++) {
          cipher.decrypt(bytestr, i * 8, bytestr, i * 8); 
          //解密;
         }
         message = gbDecode(new String(bytestr)); //解码
         is.close();
         is = null;
        }else{
         message = "服务器无法连接!" + responseCode;
       }
      } catch (Exception e) {
        message = e.getMessage();
      }
     } else if (formid==3) {  //3.STREAM测试
     try {
      conStream = (StreamConnection) Connector.open("socket://"    + server + ":10000", Connector.READ_WRITE);
      InputStream is = conStream.openInputStream();
      byte[] outs = new byte[20];
      int len = 0, ch;
      while ((ch = is.read()) != -1) {
       outs[len] = (byte) ch;
       len++;
      }
      byte[] bytestr = new byte[len];
      System.arraycopy(outs, 0, bytestr, 0, len);
      message = gbDecode(new String(bytestr));
      is.close();
      is = null;
     } catch (Exception e) {
        message = e.getMessage();
     }
    } else if (formid==4) {  //SOCKET测试
      /*
       try { 
        sck = (SocketConnection)Connector.open("socket://"+server+":10009",Connector.READ_WRITE);
                    sck.setSocketOption(SocketConnection.LINGER,5);
        dis = sck.openDataInputStream();
        message = dis.readUTF();
        dis.close();
        sck.close();
        dis = null;
        sck = null;
       } catch (Exception e) {
        message = e.getMessage();
       }
       */
       message = "SOCKET连接只能在MIDP2.0中使用";
      }
     timeused = System.currentTimeMillis() - start;
     curForm.append(message+"\n");
     message = "网络用时 " + timeused / 1000 + "." + timeused % 1000 + "\n";
     curForm.append(new StringItem(null,message));
     //HTTP连接结果为1.6-1.9秒之间
    }
  private String alpha = 
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  private String genPassword() {
    StringBuffer tmpstr = new StringBuffer(8);
    int iRandNum;
    Random rnd = new Random();
    for (int i = 0; i < 8; i++) {
      iRandNum = Math.abs(rnd.nextInt()) % 62;
      tmpstr.append(alpha.substring(iRandNum, iRandNum + 1));
    }
    return tmpstr.toString();
  }
     
  private String gbDecode(final String dataStr) {
   int start = 0;
   int end = 0;
   final StringBuffer buffer = new StringBuffer();
   while (start > -1) {
     end = dataStr.indexOf("\\u", start + 2);
     String charStr = "";
     if (end == -1) {
      charStr = dataStr.substring(start + 2, dataStr.length());
     } else {
      charStr = dataStr.substring(start + 2, end);
     }
     char letter = (char) Integer.parseInt(charStr, 16); 
     // 16进制parse整形字符串。
     buffer.append(new Character(letter).toString());
     start = end;
   }
   return buffer.toString();
  }
 }

  [1] [2] 

网友评论
<