读书人

为何真机下TCP连不到公网服务器模拟

发布时间: 2012-12-26 14:39:29 作者: rapoo

为何真机上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() );
}
}
catch(InterruptedException e){}
}

}
catch(IOException e)


{
try{
dis.close();
dos.close();
socketClient.close();
}
catch(IOException ee){}

frm.append("连接异常" + e.toString());
}
}

public void commandAction(Command cmd,Displayable displayable){
if(cmd==loginCmd){
//登陆时候启动LoginHandle
//启动Net线程
Thread netThread = new Thread(this);
netThread.start();
}else if(cmd == exitCmd){

}

}
}

[解决办法]
先友情帮顶。。。。。。。。。。。。。
[解决办法]
真机出现什么状况了?????
连网线程应该要与主线程MIDLET分开,不然容易引起死机,程序疆死等问题。。

[解决办法]
友情帮顶
[解决办法]
帮顶吧,呵呵。。。。。。。。。
[解决办法]
你的手机支持socket么
还有接入点选的什么
[解决办法]
那LZ是怎么解决的了,,分享一下。。。。

读书人网 >J2ME开发

热点推荐