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
pritesh_bb
Posts: 10
Registered: ‎09-29-2009
Accepted Solution

Port already in use exception...

Hi,

Being newbie in blackberry i have some problems while developing Java Application for blackberry. I am trying to develop CLDC application that receives SMS on perticular port in a background thread. But when i sent sms to my app using that perticular port, i got "Port alredy in use exception." What could be the problem.?

Code posted below...

 

Constructor contains:

 

try{
            incomingConnection = (DatagramConnection)Connector.open("sms://:7878");
           
        }catch(Exception e){
            Dialog.alert("port problem "+e);

 

 

Thread contains:

 

 while(true) {
                    try {
                            Datagram sms = incomingConnection.newDatagram(incomingConnection.getMaximumLength());
                            incomingConnection.receive(sms);
                            byte[] payload = sms.getData();
        }catch(Exception e){}

}

 

 

 

Thanks,

Pritesh

Please use plain text.
Developer
Posts: 43
Registered: ‎09-28-2009

Re: Port already in use exception...

Hi pritesh_bb

 

The following code is from this KB article

 

 

DatagramConnection _dc = (DatagramConnection)Connector.open("sms://"); for(;;) { Datagram d = _dc.newDatagram(_dc.getMaximumLength()); _dc.receive(d); byte[] bytes = d.getData(); String address = d.getAddress(); String msg = new String(bytes); System.out.println( "Received SMS text from " + address + " : " + msg); }

 

 As you can see you do not need to specify a port for a Datagram connection. Ports are used to identify what channel will be used to send an sms/mms etc, and these ports are assigned and grabbed in a First In First Out manner. Meaning if there is already an application that is linked to port 7878, you will recieve a "Port already in use exception",

 

However as mentioned in the above code, you do not need to specify the port number for datagram connections.

 

Hope this helps,

 

Best Wishes

 

Andre

 

Please use plain text.
New Developer
pritesh_bb
Posts: 10
Registered: ‎09-29-2009

Re: Port already in use exception...

Hi Andre,

    I really appreciate ur reply on my post. I have already tried the same code as u pasted here, but this code is usd to receive all incoming sms. And i want my app to listen only that sms which are sent on percular port like i have wriiten in open method...."7878". I dont want to use j2ME api. Want to develop app in RIM API's only. Appreciate ur help me to solve this problem.

 

 

Thanks,

Pritesh

Please use plain text.
Developer
Posts: 43
Registered: ‎09-28-2009

Re: Port already in use exception...

Hi pritesh_bb,

 

Fully understood, the error itself would mean that another application has already gotten hold of that port, and as explained it is first come first serve. As such you wont be able to retake the port, unless you uninstall the other application. Would it be possible for you to change the port number? Perhaps 9189 or similar?

 

Regards

Please use plain text.
New Developer
pritesh_bb
Posts: 10
Registered: ‎09-29-2009

Re: Port already in use exception...

Hi Andre,

    Its really a strange problem. I have tried few combination of ports to listen like..5000,5001,7333,7878,3590..etc...but every time i get same exception. But i will try the port u mentioned.

 

Thanks,

Pritesh

Please use plain text.
New Developer
pritesh_bb
Posts: 10
Registered: ‎09-29-2009

Re: Port already in use exception...

And one more thing, my blackberry dont have any other application(apart from that comes with manufacturers).

 

Thanks,

Pritesh

Please use plain text.
New Developer
pritesh_bb
Posts: 10
Registered: ‎09-29-2009

Re: Port already in use exception...

Ohhh Andre,

        Thanks man,,,,,u r genious....The port u mentioned worked perfectly fine. My application is running smotthly now.

Thanks a lot. I really apprecite ur prompt and very very useful reply.

         This forum is very good for newbie like me.

         Thanks Andre once again.

 

Thanks,

Pritesh

Please use plain text.