BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 06-18-2009, 01:50 AM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: berlin, germany
Model: 8310
PIN: N/A
Carrier: vodafone
Posts: 163
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default TCP Listener

Please Login to Remove!

Hi everybody =)
I want to create a TCP listener without setting up a connection to a server.
I just want to listen if somebody wants to connect to my application via TCP on a special port.

For a connection with a server I have to this here:
PHP Code:
 SocketConnection sc = (SocketConnection)Connector.open("socket://"+serverIP+":"+serverPort+";deviceside=false",Connector.READ_WRITE);
            
sc.setSocketOption(SocketConnection.KEEPALIVE1);
            
sc.setSocketOption(SocketConnection.LINGER5);
            
is  sc.openInputStream();
            
os sc.openOutputStream(); 
Is there a way just to listen and get the inputstream without connection to a server?

thanks hibbert
__________________
My English is so bad, that makes me nobody so quickly after
Offline  
Old 06-18-2009, 07:14 PM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts
Default

The only way I've been able to do that is by using a Data Push (like a Browser Push but to a port I select and listen to) using a BES or Unite server. Blackberry Websignals can do something similar but there are more restrictions and requirements. The main issue is that the Blackberry doesn't have an IP address that you can use to connect to without the support of the other software.

If you are working with a BES or Unite server I can post some sample code.
__________________
My other Blackberry is a PlayBook.
Offline  
Old 06-19-2009, 04:48 AM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: berlin, germany
Model: 8310
PIN: N/A
Carrier: vodafone
Posts: 163
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
i'm working with a BES, and I want to listen to a specified port, but the only way i know is to start a connection from the BB to a Server. But I want that the Server connects to the BB, so the Bb has to listen to a special port ;)

It would be nice if you can help me

thanks, hibbert
__________________
My English is so bad, that makes me nobody so quickly after
Offline  
Old 06-19-2009, 06:24 PM   #4 (permalink)
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts
Default

OK. It is a bit complicated, I will try to post some sample code this weekend. But it will take some time to pull it out of my working code and have make sense stand alone.

The other thing you will have to address is sending the data. You can't just connect to a port on the BES server. For one, it wouldn't know which device you want to send the data to; and you may wish to send the data to many devices. So the next question is what kind of programing access do you have on the BES side. If you understand LAMP programming you can have a look at this site: http://blog.fupps.com/documents/blac...ble-framework/
Even if you can't use the code, his explanation on how a push happens is very good and it helped me a great deal.
__________________
My other Blackberry is a PlayBook.
Offline  
Old 06-19-2009, 09:10 PM   #5 (permalink)
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts
Default

So, for what it is worth here is my TCP (MDS Push listener code):

Code:
    /**
     * The listener thread, listens for push data from the MDS.
     * it is usually better to do TCP communications in a separate thread
     */
    static class ListenerThread extends Thread
    {
        private boolean stopThread;
        private StreamConnectionNotifier notify;
        private StreamConnection stream;
        private InputStream input;
        
        ListenerThread()
        {
            super();
            stopThread = false;
            notify = null;
            stream = null;
            input = null;
        }
        
        public void stop()
        {
            synchronized(this)
            {
                stopThread = true;  // set the flag to stop the thread.
                
                // if the stream is null (no input happening) but notify isn't

                if (stream == null && notify != null)
                {
                    try
                    {
                        notify.close(); // close the listening socket
                    }
                    catch (Exception e)
                    {
                        System.err.println(e.toString());
                    }
                }
            }
            
            try
            {
                join(); // wait until the thread completes.
            }
            catch (Exception e) {}
            
        }
        
        public void run()
        {
            synchronized(this)
            {
                // this sleep shouldn't be necessary, but it helps to stagger 
                // threads if you start lots in an auto run application
                try
                {
                    sleep(1000L);
                }
                catch (Exception e)
                {
                }
                
                System.out.println("background thread -- running");
            }
            
            try
            {
                // Create a stream connection notifier on http protocol (for push)
                // and the port we want to use.
                notify = (StreamConnectionNotifier)Connector.open("http://:555");
                
                
                
                while (!stopThread)
                {
                    // block until data is received
                    stream = notify.acceptAndOpen();

                    synchronized(this)
                    {
                        if (stopThread)
                            break;
                        input = stream.openInputStream();
                    }
                    
                    someFunctionToProcess(input);
                    stream.close();
                    stream = null;
                    input = null;
                }
            }
            catch (Exception e)
            {
                System.err.println(e.toString());
            }
        }
    }
If you read the link in my previous post you will notice in the program "pushout" there is a line:

Code:
$Conf::config->{mdsserver}->{port} . "/push?DESTINATION=${pushdest}&PORT=7874&REQUESTURI=/";
The PORT=7874 is the port that the Blackberry browser listens for MDS Push data. If you set this to your port (555 in the case of my sample code), then the data will be received by your program.

I hope this helps.

Questions?
__________________
My other Blackberry is a PlayBook.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.