09-17-2009 03:20 AM
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("ServerSock
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);
};
09-17-2009 04:08 AM
09-17-2009 04:10 AM
09-17-2009 04:18 AM
09-17-2009 05:04 AM
Device you can't make it as server.
If you looking at recieving SMS you can intercept the incoming SMS in your blackberry.
09-17-2009 05:07 AM
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/mess
09-17-2009 08:34 AM
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.