1

I have 4 phones connected to a Wifi access point and I know the MAC/IP of all of these including the Wifi access point.

I need to implement communication between each of these phones, a sort of peer to peer communication, I was thinking about using sockets but then each phone will have to implement a ServerSocket and Socket on each of the phones is this fine?

The Ip's of these phones would be in private range 192.168.... so could I use something like http://192.168.xx.xx/port and contact any phone using http? What kind of classes could I use to implement this, or is there a ready framework that I could directly use?

4

1 回答 1

2

您的计划很好:您也可以让电话在套接字上监听。如果您只是想进行点对点通信并且对您正在编写的应用程序更感兴趣,那么您可能想看看JXTA,它是一种流行的 Java P2P 系统。我不知道,而且我听说过一些关于它的性能不好的事情,但是对于您的应用程序来说它可能是合适的。

但也不是很难自己动手。但是,我还没有看到任何用于 Java ME 的 HTTP 服务器端库,因此使用 HTTP 可能比必要的工作量更大。我可能只是在 TCP 套接字上实现一个自定义协议,因为它似乎不需要与已经存在的任何东西进行互操作。

Java ME 中的套接字通信是通过javax.microedition.io包中的通用连接框架进行的,从客户端来看,它与使用 HTTP 连接完全一样,即类似

String url = "socket://192.168.xxx.xxx:12345";
SocketConnection conn = (SocketConnection) Connector.open(url);

然后你可以从中获得一个InputStream连接OutputStream,或者DataInputStream如果DataOutputStream你想发送二进制数据。

在服务器端你会做

String url = "socket://:12345";
ServerSocketConnection sock = (ServerSocketConnection) Connector.open(url);
SocketConnection conn = (SocketConnection) sock.acceptAndOpen();

acceptAndOpen建立连接之前一直阻塞,因此如果服务器执行其他操作很重要,请确保将连接接受放入其自己的线程中。

一个警告:当我几年前这样做时,我发现仅仅监听一个套接字并不能打开所有手机上的网络,所以即使服务器开始监听,也无法连接到它,因为它不在网络上。我解决它的方法是在手机上打开 Web 浏览器,但是任何打开套接字的客户端都足够了,因此您也可以通过尝试自己打开客户端连接从应用程序中完成。

还有一种叫做推送注册表的东西。创建 Midlet 时,可以使用MIDlet-PushJAD 文件中的属性注册应用程序,这样您就不必运行应用程序,但系统会在某个端口上尝试连接时唤醒它. 我从来没有真正实现过这个,所以我不能给出更多的建议。

于 2010-01-07T20:38:19.293 回答