【发布时间】:2012-09-09 10:05:21
【问题描述】:
我正在编写一个简单的手机应用程序(使用 j2me),使用数据报协议进行客户端-服务器交互。移动应用程序将一些文本发送到服务器,服务器将该文本打印到标准输出。下面是发送数据报的代码(用手机):
String ip;
String port;
String msg;
//Loading ip, port and msg from the user input, I won't write it here, 'cause it
//does not matter
//Below is a simple code snippet for sending msg to the ip:port address
connection = (DatagramConnection) Connector.open(
"datagram://" + ip + ":" + port);
Datagram datagram = connection.newDatagram(msg.getBytes(),
msg.getBytes().length);
connection.send(datagram);
下面是接收数据报的代码(用PC):
DatagramSocket s = new DatagramSocket(7777);
DatagramPacket p = new DatagramPacket(new byte[1024], 1024);
s.receive(p);
System.out.println(new String(p.getData()));
所以,当我使用模拟器启动第一个代码 sn-p(用于发送数据报)时,一切正常:服务器成功接收并打印数据报。但是当发送数据报的程序从手机启动时,数据报并没有到达服务器。
通过模拟器测试程序时,IP地址是我的本地网络IP,通过真机测试时,IP取自http://www.whatismyip.com/。当使用以前的 IP 进行模拟器测试时,数据报也不会到达服务器。 端口始终设置为 7777。
那么,我能解决这个问题吗?
【问题讨论】:
-
您在手机上测试时是否发现任何异常?另外,它支持数据报吗?根据 MIDP 2 规范,此协议是可选的
-
不,没有例外
标签: java networking java-me datagram