views:

233

answers:

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?

+2  A: 

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.

jk
my only worry is that if multiple peers( for instance 3) try to query one phones server socket it will have to create 3 threads to server each of those, and I'm not sure how many simultaneous threads can a phone have running, here I'm talking of a S60, 5th Edition phone which is pretty advanced in its class but I dont have the stats of how these phones handle threads.Any ideas would be welcome.
Kevin Boyd
There shouldn't be any problems having three, or even more, threads at the same time, but if they are all using the CPU, it can slow down the application. So if the service includes heavy processing, one way is to have one thread that's only doing processing and the communication threads feeding it through a single shared queue; that's how I structured one application and didn't have any performance issues. Or if you really worry about creating threads, don't; just have the server process messages serially and implement a backoff scheme for clients when they don't manage to connect.
jk
Well thanks for that piece of advice, I couldn't understand the caveat that you mentioned regarding socket listening on a port, why can't other phones connect to it? It would be really cumbersome for me to start a browser! Could you please elaborate on your experience?
Kevin Boyd
The problem is that the network didn't get initialized just because listening was started on a socket; these phones (this was several years ago, though) required an active connection opening before they accessed the network, so even though the socket was listening, there was no way to connect to the device on the actual wireless network. But if this hits you (it might not), you can also, say, open a dummy connection to your own IP address; that should work, and would be all inside your application.
jk
Thanks for sharing your experince!
Kevin Boyd