JavaME,实现点对点通信

发布于 2024-08-17 06:51:15 字数 339 浏览 2 评论 0原文

我有 4 部手机连接到 Wifi 接入点,并且我知道所有这些手机的 MAC/IP,包括 Wifi 接入点。

我需要在每个电话之间实现通信,一种点对点通信,我正在考虑使用套接字,但是每个电话都必须在每个电话上实现 ServerSocket 和 Socket 这样可以吗?

这些手机的IP将在私人范围192.168....所以我可以使用类似的东西http://192.168 .xx.xx/port 并使用 http 联系任何电话?我可以使用什么样的类来实现这一点,或者是否有一个可以直接使用的现成框架?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹梦到西洲 2024-08-24 06:51:15

您的计划很好:您也可以让电话在套接字上侦听。如果您只想进行点对点通信并且对您正在编写的应用程序更感兴趣,您可能需要查看 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);

,然后您可以得到用于连接的 InputStreamOutputStream,或者如果要发送二进制数据,则为 DataInputStreamDataOutputStream

在服务器端,您将执行

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

The acceptAndOpen 块,直到建立连接为止,因此,如果服务器执行其他操作很重要,请确保将连接接受放入其自己的线程中。

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

还有一个叫做推送注册表的东西。当您创建 Midlet 时,可以使用 JAD 文件中的 MIDlet-Push 属性注册应用程序,这样您不必让应用程序运行,但系统会唤醒它当尝试在某个端口上建立连接时。我从未真正实施过这一点,所以我无法就此提供更多建议。

What you are planning is just fine: you can have phones listen on sockets too. If you just want to have peer-to-peer communication and are more interested in the application you're writing, you might want to take a look at JXTA, which is a somewhat popular P2P system for Java. I don't know it, and I've heard some bad things about its performance, but for your application it could be suitable.

But it's not very hard to roll your own, either. However, I haven't seen any HTTP server-side libraries for Java ME, so using HTTP might be more work than necessary. I would probably just implement a custom protocol over TCP sockets, since it does not appear you would need to be interoperable with anything already in existence.

Socket communication in Java ME is through the Generic Connection Framework, found in the javax.microedition.io package, and from the client side it's exactly like using HTTP connections, i.e., something like

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

And then you can get an InputStream and OutputStream for the connection from that, or DataInputStream and DataOutputStream if you want to send binary data.

On the server side you would do

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

The acceptAndOpen blocks until a connection is made, so if it is important for the server to be doing something else, make sure to put the connection acceptance into its own thread.

A caveat: when I was doing this a few years back, I found out that just listening on a socket does not turn on the network on all phones, so even though the server began listening, it was not possible to connect to it because it was not on the network. The way I worked around it was to open the Web browser on the phone, but any client opening a socket is enough, so you could also do it from the application by trying to open a client connection yourself.

There is also something called the Push Registry. When you create your Midlet, there is a possibility to register the application with a MIDlet-Push attribute in the JAD file, so that you don't have to have your application running but the system will wake it up when a connection is attempted on a certain port. I've never actually implemented this, so I cannot give any more advice on it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文