J2me手机Socket编程问题 ( 积分: 200 )

  • 主题发起人 主题发起人 zangqi
  • 开始时间 开始时间
Z

zangqi

Unregistered / Unconfirmed
GUEST, unregistred user!
基于WTK做了个测试程序,部分测试代码如下:
public void mynewsocket(){
StringBuffer buf =new StringBuffer();
try {
String url="socket://211.148.×.×:6666";
byte mybytes[];
mybytes = url.getBytes();
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("1");
MIDlet1.instance.displayable2.mypaint();
buf.append("1");
SocketConnection sc;
sc=(SocketConnection)Connector.open("socket://211.148.×.×:9188");
//sc=(SocketConnection)Connector.open("socket://211.148.×.×:6666");
sc.setSocketOption(SocketConnection.DELAY,1);
sc.setSocketOption(SocketConnection.LINGER,25);
sc.setSocketOption(SocketConnection.KEEPALIVE,1);
sc.setSocketOption(SocketConnection.RCVBUF,1024);
sc.setSocketOption(SocketConnection.SNDBUF,1024);
int myport=sc.getLocalPort();
//String myadd=sc.getLocalAddress();
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append((char)myport);
MIDlet1.instance.displayable2.mypaint();
buf.append("p"+myport);
sc.setSocketOption(SocketConnection.KEEPALIVE,1);
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("2");
MIDlet1.instance.displayable2.mypaint();
buf.append("2");
//sc.setSocketOption();
DataInputStream is = sc.openDataInputStream();
DataOutputStream os= sc.openDataOutputStream();
// DataOutputStreamdo
s = new DataOutputStream(sc.openDataOutputStream());
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("3");
MIDlet1.instance.displayable2.mypaint();
buf.append("3");
//os.writeUTF("hello/n");
os.write(mybytes,0,mybytes.length);
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("4");
MIDlet1.instance.displayable2.mypaint();
buf.append("4");
os.flush();
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("5");
MIDlet1.instance.displayable2.mypaint();
buf.append("5");
//DataInputStream dis= new DataInputStream(sc.openDataInputStream());
String content="";
int ic;
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("6");
MIDlet1.instance.displayable2.mypaint();
buf.append("6");
//buf.append(content);
while((ic=is.read())!= -1)
{
buf.append((char)ic);
}
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("7");
MIDlet1.instance.displayable2.mypaint();
buf.append("7");
is.close();
os.close();
sc.close();
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("8");
MIDlet1.instance.displayable2.mypaint();
buf.append("8");

}
catch (Exception e) {
e.printStackTrace();
}
Form f =new Form("http test");
f.append(buf.toString());
Display.getDisplay(this).setCurrent(f);

}
在计算机的模拟器上运行都正常,收发数据也没问题。但是在手机上实际运行后,只执行到sc=(SocketConnection)Connector.open("socket://211.148.×.×:9188");就抛出了异常。在PC上的服务器软件检测到已经建立了TCP连接
10.21.9.193:32772连接!
10.21.9.193:32772错误!
10.21.9.193:32772断开!
但是一建立连接就立刻报错并断开了Asynchronous socket error 100053;
用手机自带浏览器建立连接,PC软件接收如下信息:
GET / HTTP/1.0
Host: 211.148.×.×:9188
Accept: application/vnd.wap.wmlc, application/vnd.wap.wmlscriptc, application/vnd.wap.multipart.related, application/vnd.wap.multipart.mixed, application/vnd.phonecom.mmc-wbxml, application/octet-stream, application/vnd.oma.drm.message, text/plain, text/css, image/bmp, image/gif, image/jpeg, image/png, image/vnd.wap.wbmp, application/vnd.wap.sic, application/vnd.wap.slc, application/vnd.wap.coc, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, text/vnd.sun.j2me.app-descriptor, application/java, application/java-archive, application/smil, application/vnd.wap.mms-message, image/x-up-wpng, application/vnd.smaf, audio/imelody, audio/midi, audio/sp-midi, audio/mid, audio/x-midi, application/game, text/x-imelody, audio/amr, application/vnd.uplanet.bearer-choice-wbxml, text/x-vcard, text/x-vcalendar, text/vnd.wap.sl, text/vnd.wap.si, text/vnd.wap.co, text/vnd.wap.wml, text/vnd.wap.wmlscript
user-agent: LG-G850 V100 UP.Browser/6.2.2 (GUI) MMP/1.0 Profile/MIDP-1.0 Configuration/CLDC-1.0
accept-charset: utf-8
accept-language: zh-cn
x-up-devcap-num-softkeys: 2
X-Network-info: GPRS,10.21.16.110,13821******,211.139.172.70,unsecured
X-Forwarded-For: 10.21.16.110
X-Up-Calling-Line-ID: 13821******
X-Source-ID: 211.139.172.70
X-Nokia-CONNECTION_MODE: CMODE
X-Up-Bearer-Type: GPRS
X-Nokia-gateway-id: NWG/4.0/Build68
x-wap-profile: http://gsm.lge.com/html/gsm/LG-G850.xml
Client-ip: 10.0.0.155
Connection: keep-alive
Via: HTTP/1.1 BJBJ-B-PROXY01-WAP[0A0000E1] (Traffic-Server/4.0.9 [uSc ])
并且收发数据都正常,PC上检测到的连接IP为:211.136.16.41:20517连接!
不是10.0.几的IP。请问为什么?
 
基于WTK做了个测试程序,部分测试代码如下:
public void mynewsocket(){
StringBuffer buf =new StringBuffer();
try {
String url="socket://211.148.×.×:6666";
byte mybytes[];
mybytes = url.getBytes();
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("1");
MIDlet1.instance.displayable2.mypaint();
buf.append("1");
SocketConnection sc;
sc=(SocketConnection)Connector.open("socket://211.148.×.×:9188");
//sc=(SocketConnection)Connector.open("socket://211.148.×.×:6666");
sc.setSocketOption(SocketConnection.DELAY,1);
sc.setSocketOption(SocketConnection.LINGER,25);
sc.setSocketOption(SocketConnection.KEEPALIVE,1);
sc.setSocketOption(SocketConnection.RCVBUF,1024);
sc.setSocketOption(SocketConnection.SNDBUF,1024);
int myport=sc.getLocalPort();
//String myadd=sc.getLocalAddress();
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append((char)myport);
MIDlet1.instance.displayable2.mypaint();
buf.append("p"+myport);
sc.setSocketOption(SocketConnection.KEEPALIVE,1);
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("2");
MIDlet1.instance.displayable2.mypaint();
buf.append("2");
//sc.setSocketOption();
DataInputStream is = sc.openDataInputStream();
DataOutputStream os= sc.openDataOutputStream();
// DataOutputStreamdo
s = new DataOutputStream(sc.openDataOutputStream());
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("3");
MIDlet1.instance.displayable2.mypaint();
buf.append("3");
//os.writeUTF("hello/n");
os.write(mybytes,0,mybytes.length);
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("4");
MIDlet1.instance.displayable2.mypaint();
buf.append("4");
os.flush();
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("5");
MIDlet1.instance.displayable2.mypaint();
buf.append("5");
//DataInputStream dis= new DataInputStream(sc.openDataInputStream());
String content="";
int ic;
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("6");
MIDlet1.instance.displayable2.mypaint();
buf.append("6");
//buf.append(content);
while((ic=is.read())!= -1)
{
buf.append((char)ic);
}
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("7");
MIDlet1.instance.displayable2.mypaint();
buf.append("7");
is.close();
os.close();
sc.close();
MIDlet1.instance.displayable2.buf1.setLength(0);
MIDlet1.instance.displayable2.buf1.append("8");
MIDlet1.instance.displayable2.mypaint();
buf.append("8");

}
catch (Exception e) {
e.printStackTrace();
}
Form f =new Form("http test");
f.append(buf.toString());
Display.getDisplay(this).setCurrent(f);

}
在计算机的模拟器上运行都正常,收发数据也没问题。但是在手机上实际运行后,只执行到sc=(SocketConnection)Connector.open("socket://211.148.×.×:9188");就抛出了异常。在PC上的服务器软件检测到已经建立了TCP连接
10.21.9.193:32772连接!
10.21.9.193:32772错误!
10.21.9.193:32772断开!
但是一建立连接就立刻报错并断开了Asynchronous socket error 100053;
用手机自带浏览器建立连接,PC软件接收如下信息:
GET / HTTP/1.0
Host: 211.148.×.×:9188
Accept: application/vnd.wap.wmlc, application/vnd.wap.wmlscriptc, application/vnd.wap.multipart.related, application/vnd.wap.multipart.mixed, application/vnd.phonecom.mmc-wbxml, application/octet-stream, application/vnd.oma.drm.message, text/plain, text/css, image/bmp, image/gif, image/jpeg, image/png, image/vnd.wap.wbmp, application/vnd.wap.sic, application/vnd.wap.slc, application/vnd.wap.coc, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, text/vnd.sun.j2me.app-descriptor, application/java, application/java-archive, application/smil, application/vnd.wap.mms-message, image/x-up-wpng, application/vnd.smaf, audio/imelody, audio/midi, audio/sp-midi, audio/mid, audio/x-midi, application/game, text/x-imelody, audio/amr, application/vnd.uplanet.bearer-choice-wbxml, text/x-vcard, text/x-vcalendar, text/vnd.wap.sl, text/vnd.wap.si, text/vnd.wap.co, text/vnd.wap.wml, text/vnd.wap.wmlscript
user-agent: LG-G850 V100 UP.Browser/6.2.2 (GUI) MMP/1.0 Profile/MIDP-1.0 Configuration/CLDC-1.0
accept-charset: utf-8
accept-language: zh-cn
x-up-devcap-num-softkeys: 2
X-Network-info: GPRS,10.21.16.110,13821******,211.139.172.70,unsecured
X-Forwarded-For: 10.21.16.110
X-Up-Calling-Line-ID: 13821******
X-Source-ID: 211.139.172.70
X-Nokia-CONNECTION_MODE: CMODE
X-Up-Bearer-Type: GPRS
X-Nokia-gateway-id: NWG/4.0/Build68
x-wap-profile: http://gsm.lge.com/html/gsm/LG-G850.xml
Client-ip: 10.0.0.155
Connection: keep-alive
Via: HTTP/1.1 BJBJ-B-PROXY01-WAP[0A0000E1] (Traffic-Server/4.0.9 [uSc ])
并且收发数据都正常,PC上检测到的连接IP为:211.136.16.41:20517连接!
不是10.0.几的IP。请问为什么?
 
后退
顶部