日期:2014-05-20  浏览次数:20745 次

为何真机上TCP连不到公网服务器,模拟器没问题?
简单的测试代码,模拟器正常,真机不行。 怎么解决。
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

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

/**
 * @author Administrator
 */
public class NetTestMid extends MIDlet implements Runnable,CommandListener {

  private Display display = null;

  private SocketConnection socketClient = null;
   
  private DataInputStream dis = null;

  private DataOutputStream dos = null;

  Thread netThread = null;

  byte[] tmpByte = new byte[8] ;
   
  Form frm = new Form("Test");

  private Command loginCmd = new Command("启动线程",Command.OK,1);
  private Command exitCmd = new Command("退出",Command.EXIT,1);

  int waitCount = 0 ;

  byte tmpB ;

  public void startApp() {

  display = Display.getDisplay(this);

  frm.append("NetTest");

  frm.addCommand(loginCmd);
   
  frm.addCommand(exitCmd);

  frm.setCommandListener(this);

  display.setCurrent(frm);

  //System.out.println("start thread");

  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  //synchronized
  public void run()
  {
  try{

  socketClient = (SocketConnection)Connector.open("socket://117.40.91.183:20100");//连接服务端
   
  socketClient.setSocketOption(socketClient.KEEPALIVE, 20);
   
  dis = socketClient.openDataInputStream();

  dos = socketClient.openDataOutputStream();

  //dos.writeUTF("sender IP :" + socketClient.getLocalAddress() );
  dos.writeUTF("sender IP :");
  //System.out.println("sender IP :" + socketClient.getAddress());

  frm.append( "sender IP :" + socketClient.getLocalAddress() );

  while(dis.read(tmpByte) != -1 )
  {
  //dis.read(tmpByte);

  String s = new String(tmpByte);

  //System.out.println("Client receive message from server: " + s );

  //Alert alert = new Alert("Client receive message from server");

  //alert.setString("Client receive message from server " + s);

  //alert.setType(AlertType.INFO);

  //alert.setTimeout(3000);

  //display.setCurrent(alert);
  frm.append("Client receive message from server: " + s );

  s = null ;

  try{

  netThread.sleep(100);
  waitCount++;

  if( waitCount >= 200)
  {
  frm.append("重连");
  waitCount = 0 ;
  dos.writeUTF("sender IP :" + socketClient.getLocalAddress() );
  frm.append( "sender IP :" + socketClient.getLocalAddress() );
  }