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
Highlighted
New Developer
Posts: 14
Registered: ‎08-11-2009
My Device: Not Specified

client/server

i have the below code to receive messages from a client and display it on the blackberry simulator screen. when i run this in simulator, a screen appears with the title "Waiting for connection". then, when i run a client(i'm running the client in flashlite emulator) no connection is being established between the two. i'm also giving below the client code i have in flashlite.wht could be the problem? someone help. thank you,

 

--bhargava

 

package test;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
//import javax.swing.*;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;

class bserver extends UiApplication {
    
    public static void main(String [] args)
    {
        bserver sapp = new bserver();
        sapp.enterEventDispatcher();
        
    }
    
    private bserver(){
        pushScreen(new bscreen());
    }
    
}

final class bscreen extends MainScreen {

    private boolean stop;
    private ServerSocketConnection scn;
    private SocketConnection sc;
    private InputStream is;
    private OutputStream os;
   
    bscreen(){
        try {
            LabelField title = new LabelField("Waiting for connection" , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
            setTitle(title);
            scn = (ServerSocketConnection)Connector.open("ServerSocket://:5000");         // Wait for a connection.
            sc = (SocketConnection)scn.acceptAndOpen();
            add(new RichTextField("Connection accepted"));
            is = sc.openInputStream();
            os = sc.openOutputStream();
           // Sender sender = new Sender(os);

            // Allow sending of messages only after Sender is created
           
            // receive msg from client
            while (true) {
                StringBuffer sb = new StringBuffer();
                int c = 0;

                while (((c = is.read()) != '\n') && (c != -1)) {
                    sb.append((char)c);
                }

                if (c == -1) {
                    break;
                }
                add(new EditField("Message received - ", sb.toString()));
                            }

            stop();
            add(new RichTextField("Connection is closed" ,Field.NON_FOCUSABLE));
            } catch (IOException ioe) {
            if (ioe.getMessage().equals("ServerSocket Open")) {
                Alert a = new Alert("Server", "Port 5000 is already taken.", null, AlertType.ERROR);
                a.setTimeout(Alert.FOREVER);
              //  a.setCommandListener(this);
               // display.setCurrent(a);
            } else {
                if (!stop) {
                    ioe.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   

   
    public void stop(){
        try {
            stop = true;

            if (is != null) {
                is.close();
            }

            if (os != null) {
                os.close();
            }

            if (sc != null) {
                sc.close();
            }

            if (scn != null) {
                scn.close();
            }
        } catch (IOException ioe) {
        }
    }
}

 

 

 

 

client code in flashlite

-------------------------------

 

var theSocket:XMLSocket = new XMLSocket();
theSocket.connect("localhost",5000);
theSocket.onConnect = function(myStatus) {
    if (myStatus) {
conn_txt.text = "connection successful";
} else {
conn_txt.text = "no connection made";
}
};


sendButton.onRelease = function() {
    theSocket.send(send_msg.text);        
    };
    
   

New Developer
Posts: 14
Registered: ‎08-11-2009
My Device: Not Specified

Re: client/server

someone please reply
Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: client/server

What exactly you are trying. Are you trying to connect to server using socket connection from your BB device?

Use Search. "Accept Solution" If the problem is resolved.
New Developer
Posts: 14
Registered: ‎08-11-2009
My Device: Not Specified

Re: client/server

hey can't we make a blackberry device itself as a server to accept messages from a client?
Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: client/server

Device you can't make it as server.

 

If you looking at recieving SMS you can intercept the incoming SMS in your blackberry.


Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: client/server

The only incoming connections that can be accepted by a wireless BlackBerry are BIS or BES push.  WiFI?  I'm not so sure.

 

See here for more, especially the second post:

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=29104

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

Re: client/server

Check whether your BlackBerry code throws an exception (and prints it in the Debugger Output output). I'm suspicious about the "serversocket://:5000" Connector.open() URL -- I've never seen "serversocket" protocol on BlackBerry. You probably want the usual "socket://:5000;deviceside=true".

 

In addition to it all, the above code will attempt to listen on the far end of the IP endpoint for TCP connections to port 5000. Do you really want that? When the IP tunnel goes via the cellular operator/carrier (as is the case in your case) the IP tunnel usually terminates in a private network inside the carrier's network. Connections from the outside to this network are usually firewalled off and, on top of it all, you'll get a private non-routable IP. If you were to use the WLAN inteface (append the interface=wifi connection URL parameter) it'll definitely work. However, even in that case, you might have trouble receiving the data in real-time from the input stream. On some OSes the data only appears out of the input stream after the connection is closed.