JavaME,实现对等通信 [英] JavaME, Implementing Peer to Peer communication

查看:88
本文介绍了JavaME,实现对等通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4部电话连接到Wifi接入点,并且我知道所有这些设备的MAC/IP,包括Wifi接入点.

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.

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

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?

这些电话的IP在私有范围192.168 ....中,所以我可以使用 http: //192.168.xx.xx/port 并使用http与任何电话联系?我可以使用哪种类来实现此目的,或者可以直接使用现成的框架?

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?

推荐答案

您正在计划的内容就可以了:您也可以让电话在套接字上监听.如果您只想进行点对点通信,并且对正在编写的应用程序更感兴趣,则可以查看 JXTA ,它是一种流行的Java P2P系统.我不知道,我听说过有关它性能的一些坏消息,但是对于您的应用程序来说,它可能是合适的.

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.

但是也可以很容易地推出自己的游戏.但是,我还没有看到Java ME的任何HTTP服务器端库,因此使用HTTP可能比必要的工作更多.我可能只是在TCP套接字上实现自定义协议,因为它似乎并不需要与任何已有的东西都可以互操作.

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.

Java ME中的套接字通信是通过javax.microedition.io包中的Generic Connection Framework进行的,从客户端看来,这就像使用HTTP连接一样,即类似

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);

然后您可以从中获得InputStreamOutputStream进行连接,如果要发送二进制数据,则可以得到DataInputStreamDataOutputStream.

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();

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.

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

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.

还有一个称为Push Registry的东西.创建Midlet时,可以在JAD文件中使用MIDlet-Push属性注册该应用程序,这样就不必运行应用程序,但是当尝试建立连接时系统会唤醒它某个端口.我从未真正实现过此功能,因此无法对此提供更多建议.

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.

这篇关于JavaME,实现对等通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆