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
New Developer
Posts: 12
Registered: ‎01-13-2009
My Device: BlackBerry 8900

Inter process communication using J2ME classes

Hi,

 

I am trying to implement inter process communication between a J2ME application and a BlackBerry application in a BlackBerry. I am not sure if this is possible, have done the following based on documentation and what I have read on the internet,

 

In the BlackBerry application,

 private class IntegrationListenersThread extends Thread
 {
  public void run()
  {
   try
   {
    ServerSocketConnection ssc = (ServerSocketConnection)Connector.open("socket://:9002;deviceside=true");
    StreamConnection sc = null;
    InputStream is = null;
    try
    {
     sc = ssc.acceptAndOpen();
     is = sc.openInputStream();
     int ch = 0;
     StringBuffer sb = new StringBuffer();
     while ((ch = is.read()) != -1)
     {
      sb.append((char)ch);
     }

     Dialog.inform(sb.toString());
    }
    finally
    {
     ssc.close();
     sc.close();
     is.close();
    }
   }
   catch (IOException x)
   {
    log(x.toString());
   }

  }
 }

 

In the J2ME application,

try{
  SocketConnection sc = (SocketConnection)
    Connector.open("socket://:9002;deviceside=true");//inserting localhost here does not make any difference
  OutputStream os = null;
  try{
    os = sc.openOutputStream();
    byte[] data = "Hello from Integration Sample Socket!".getBytes();
    os.write(data);
   
  } finally{
      os.flush();
      os.close();
      sc.close();
     
  }
} catch (IOException x){
        Dialog.alert(x.toString());
}

 

I get a NullPointerException when it encounters Connector.open in the J2ME app in the BlackBerry device and get a IOPortAlreadyBoundException in the simulator in the same line.

 

Any help\guidance on this is appreciated.

 

Thanks

Ponnu

Developer
Posts: 212
Registered: ‎07-18-2008
My Device: Storm 9550

Re: Inter process communication using J2ME classes

1st)

you should try using JSR-211 (CHAPI) for inter process communication.

 

2nd)

The AlreadyBoundException seems to be clear because you are opening the same address for  both Connector.open. So the 2nd would also open a ServerSocket.

Also:

socket:// returns a SocketConnection but with socket://Smiley Tongueort only it returns ServerSocketConnection

ServerSocketConnection DOES NOT inherit from SocketConnection, be careful. So the current version of the J2ME will fail.

 

But don't know what else to do if localhost does not work.

If your problem was solved, please mark answer as "Accepted solution"
If your want to thank, click the "kudo" symbol
___________
visit me: http://mobilejavadevelopment.blogspot.com/
visit the Berlin BlackBerry Developer Group: http://berlinblackberrydevelopers.blogspot.com/
Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Inter process communication using J2ME classes

Connector.open("socket://:9002;deviceside=true") will not listen for local connections inside the device at all. Instead, it will listen for incoming TCP connections on port 9002 at the far end of the IP tunnel that your device established via the mobile carrier network using the TCP APN (i.e., on the GGSN and to the IP address that the tunnel was assigned at the GGSN). If you prepend interface=wifi, then, similarly, you'll be listening for incoming TCP connections inside the WiFi network and to the IP address that your device has in the WiFi network.
Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Inter process communication using J2ME classes

LOL, sockets for inter-process communication? Who would do that? I got into a discussion about some similar

issue here before. I think it turned out that 127.0.0.1 wasn't available or something. I think I could set up

serversockets on the phone and that worked but I couldn't hit them from the phone ( although I may have

not appended the suffix and it didn't know which interface to use, you could try talking to 127.0.0.1;deviceside=true)

and I think someone here said localhost is not available to us. Further AFAIK there is no way for common folk to initiate 

even a TCP/80 connection to your serversocket from outside.

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Inter process communication using J2ME classes

marchywka, these sockets listen at the remote end of the IP tunnel corresponding to the socket. In WiFi this means listening on the WiFi interface of the BlackBerry. In Direct TCP this means the GGSN. It is pretty easy to write a TCP server on BlackBerry to which you could connect over WiFi or from the carrier IP network, especially if that's a private company network (via a private APN).

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

Re: Inter process communication using J2ME classes

have you tried this? They aren't "supposed to" listen from random locations, admittedly making concession for

the environment may make sense it does create confusing situations. There is normally some reliable means to talk to yourself

since it doesn't rely on RF working.

 

So, ok, I setup a server socket on the phone. How do I connect to it? I've tried using IP from a recent hit to

server, that don't work. I didn't seem to be able to use 127.0.0.1 from phone.

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Inter process communication using J2ME classes

Once you've set up a server socket on the phone, you can ask that object what its "local" address and port is. In WiFi case that will be the IP of the WiFi interface of this BlackBerry in the WiFi network and the port will be the port you've requested. In mobile/cellular network case, the IP will be the IP of this device in the carrier IP network (as identified by the APN you are using) and the port will be the port you've requested. Depending on various network configurations (e.g., normal IP network with Internet access, blackberry.net APN, or company-private network) and the topology inside that IP network, you may or may not be able to connect to that carrier-IP-network-internal IP address + port.

 

Try it on WiFi first. You'll see that it works. In fact, on WiFi you can even connect to that server socket from the same device by creating a client socket connection using Connector.open. Same applies to cellular IP network accessible via the blackberry.net APN. In general though, the connectivity varies in the cellular network case.

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

Re: Inter process communication using J2ME classes

I can't remember if I tried using the IP address returned from server socket or not- it does say "external system"

and IIRC opinion here was against it. I do remember trying to hit from desktop with no luck and 127.0.0.1 didn't

seem to work. WiFi should work AFAIK unless carriers disabled that like GPS LOL.