Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 28
Registered: ‎12-17-2008
My Device: Not Specified

Socket communication between applications

Hi,

 

I'm trying to implement some communication between multiple applications on the handset and I've been trying to use Sockets.

 

I've seen a few other posts here on this, but some things are not clear

 

1)  Is it possible for two+ applications on the handset to use Sockets for communication?  (the socket example looks to work between PC and Handset)

 

2)  Is the "localhost" name valid on the handset?  When getting the local address on the simulator, I'm getting either null or \\u0000\\u0000\\u0000\\u0000.  Neither of these function.  When attempting to connect to "socket://localhost:5678" I get an IO exception "Peer refused connection".

 

3)  Are there any examples which cover application to application communication?

 

Thanks,

 

DD

 

 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Socket communication between applications

LOL- see my earlier post about being ubable to connect to a server socket on  127.0.0.1 due

to security issues. Anyone care to explain this?

 

Sockets are quite reasonable ways for talking between processes you would think.

 

There are a bunch of silly security problems on phones...

 

Developer
Posts: 28
Registered: ‎12-17-2008
My Device: Not Specified

Re: Socket communication between applications

I think I may have seen your post.  Thanks for the LOL :-)  I'm glad th

 

What I didn't see explicitly (and I'm putting the RIM folks here on the spot), is a direct answer on the socket issue (i.e. why have ServerSocketConnection if you don't support connections) and the "localhost" name.

 

If you don't support sockets, how can I implement a socket like connection between applications?

 

Thanks,

 

DD

 

 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Socket communication between applications

Two options, persistentobject or runtimestore. But, it does seem you should be able to

send arbitrary udp and tcp packets to 127.0.0.1 I'm pretty sure at least with the browser

they don't want you to copy headers etc. But, since it is almost impossible for someone to

initiate a connection, the only reason to setup a server socket is to talk to yourself.

 

Developer
Posts: 28
Registered: ‎12-17-2008
My Device: Not Specified

Re: Socket communication between applications

That's exactly what I was thinking (wrt ServerSocketConnection).  Sockets are a pretty well established means of communication between processes, and it makes our application design more portable across platforms.

 

Since there's just about zero chance that anyone will ever be able to reach the device from the outside world (i.e. initiate a request from the PC to the handset), it seems like ServerSocketConnection should really/especially be accepting ONLY from localhost.

 

In addition, when setting the IP address in the Emulator, it faults out during bootup!...  What's going on here?

 

I'm getting a "connection refused by peer" as the exception message, is there any way I can get any more information about why it was refused?  I.e. it's security, I'm not configured correctly, bad parameter...  you get the idea...

 

Thanks for the help marchywka, I do appreciate your answers.

 

DD

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Socket communication between applications

I tried it on a real phone- that is where you encounter all the "security" problems.

LOL./

 

You did create a server socket to listen didn't you?

Developer
Posts: 28
Registered: ‎12-17-2008
My Device: Not Specified

Re: Socket communication between applications

Yeah, I've got the ServerSocketConnection (ripped directly from the API docs) waiting to receive..  Here's the snip:

 

public void openReceiveSocket(){ try { ServerSocketConnection scn = (ServerSocketConnection) Connector.open("socket://:5678;"); SocketConnection sc = (SocketConnection) scn.acceptAndOpen(); sc.setSocketOption(SocketConnection.DELAY, 0); sc.setSocketOption(SocketConnection.LINGER, 0); sc.setSocketOption(SocketConnection.KEEPALIVE, 0); sc.setSocketOption(SocketConnection.RCVBUF, 128); sc.setSocketOption(SocketConnection.SNDBUF, 128); DataInputStream is = sc.openDataInputStream(); DataOutputStream os = sc.openDataOutputStream(); String result = is.readUTF(); os.writeUTF(result); Spew.msg(3, "NameIdLookupMgr::openReceiveSocket()... Received:" + result); is.close(); os.close(); sc.close(); scn.close(); } catch (IOException ioe){ Spew.msg(1,"NameIdLookupMgr::openReceiveSocket()... Caught exception" + ioe.getMessage()); } catch (ClassCastException cc){ Spew.msg(1, "NameIdLookupMgr::openReceiveSocket()...Caught Class Cast Exception:" + cc.getMessage()); } }

 

 

The other process is supposed to send information on the socket, again using code ripped directly from the API docs:

 

public static void openSendSocket(String toSend){ try { SocketConnection sc = (SocketConnection)Connector.open("socket://localhost:5678;deviceside=true"); sc.setSocketOption(SocketConnection.LINGER, 5); DataInputStream is = sc.openDataInputStream(); DataOutputStream os = sc.openDataOutputStream(); os.writeUTF(toSend); String transfered = is.readUTF(); Spew.msg(4, "NameIdLookupMgr::openSendSocket()... Transfered:" + transfered); is.close(); os.close(); sc.close(); } catch (IOException ioe) { Spew.msg(1, "NameIdLookupMgr::openSendSocket()... IO Exception:" + ioe.getMessage()); } catch (ClassCastException cc){ Spew.msg(1, "NameIdLookupMgr::openSendSocket()...Caught Class Cast Exception:" + cc.getMessage()); } }

 

 

Thanks

DD

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Socket communication between applications

Does a non WiFi device actually have an IP address? 

 

In my simple model of what goes on, the non WiFi device does not actually have an address, it is proxied by the DES/MDS or by the BIS Service.  All its communication is tunneled by RIM's communication until it pop at at the NOC or the MDS.  So there is not a full TCP/IP Stack running on the device.

 

However I'd appreciate someone explaining how WiFi changes this!

 

Anyway, if you want to communicate between applications on the device, I think you are going to have to use another mechanism like Global Events or RuntimeStore.

 

Just my 2p worth, I'd appreciate being corrected.

 

 

Developer
Posts: 28
Registered: ‎12-17-2008
My Device: Not Specified

Re: Socket communication between applications

Thanks for the response Peter,

 

I just looked up "GlobalEvent" in the API docs, none there.  Do you mean regular "Event" class?  Is there a means of setting a global event and passing an object instances between Apps?

 

I understand about the external addresses, but shouldn't the device recognize the loopback?.  Compared with sockets, all of the other inter-application communication mechanisms really seem to blow.  I'm assuming that it's something that I've done wrong.  If it was NOT possible to use sockets like this because of something RIM is in control of, I would expect that to be CLEARLY posted in the API docs.  Sockets are a "fairly well established" means of interprocess communication, and there are listed classes shown in the API.

 

Thanks

BlackBerry Development Advisor
Posts: 15,753
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Socket communication between applications

BlackBerry handhelds do not support socket loopback communication.

 

When connecting through the BlackBerry Enterprise Server or BlackBerry Internet Service, 127.0.0.1 and localhost refer to the actual server the handheld is connecting to, not the device itself.  This is blocked to prevent applications from attempting to connect to resources on the server.

 

A ServerSocketConnection can be used to listen for data pushed via the BlackBerry Enterprise Server.

 

You can find information on global events here.

 

What Is - Global Events and Global Event Listeners
Article Number: DB-00145

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800620/What_Is...

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker