BlackBerry Forums Support Community

BlackBerry Forums Support Community (
-   Developer Forum (
-   -   How to Read from a socket ?? (

rammohanrg 11-20-2011 04:54 AM

How to Read from a socket ??
What is the way to read from a socket in Blackberry?

I have used the following code to read but it does not reads .

for(int i=0;i<length;++i)
input[i] = ((char);

System.out.println("OUPUT : " +input[i]);


It gets stalled.

I tried to use but it too does'nt work.

Any Suggestions....

hrbuckley 11-20-2011 02:40 PM

Re: How to Read from a socket ??
This is the code snipit from the OS 6 API:


  SocketConnection sc = (SocketConnection)
  sc.setSocketOption(SocketConnection.LINGER, 5);

  InputStream is  = sc.openInputStream();
  OutputStream os = sc.openOutputStream();

  int ch = 0;
  while(ch != -1) {
      ch =;


Any code that may block (such as socket reads or writes) must be run on a thread separate from the event thread.

rammohanrg 11-21-2011 12:20 AM

Re: How to Read from a socket ??
Yes , that was perfect (y).
I was trying to create a server socket in BB
Any ideas....

hrbuckley 11-21-2011 06:44 AM

Re: How to Read from a socket ??
If you leave out the host name part of the URI in the will return a ServerSocketConnection. You can then use acceptAndOpen to receive connections. The problem is for other systems finding your IP address and connecting. It should work on Wi-Fi, and I have sent UDP packets between BlackBerries on the same carrier, but (at least with my carrier) the phones end up NATed behind the carier's firewall.

rammohanrg 11-22-2011 04:57 AM

Re: How to Read from a socket ??
I have to create a server socket for accepting connections. For that how do i get the ip address of the phone (simulator) . I have used the following code to retrieve the ip.
scn = (ServerSocketConnection)"socket://:1234");
System.out.println("IP Address----" + scn.getLocalAddress().toString()+"\n");

But the value if ip its showing blank????? i.e. IP Address---- blank

Without the ip connec. can't be created......

hrbuckley 11-22-2011 07:42 AM

Re: How to Read from a socket ??
Not all methods of connection are actually implemented by TCP/IP stacks on the phone so there is no IP address associated with the phone in that case. You will need to select a connectiion method that supports an IP address. Not all the connections that support IP addresses are available for the simulator, in fact I'm not sure any are.

Ojasviharsola 04-24-2012 05:00 AM

Re: How to Read from a socket ??
Code Snippet

public void run() {

// Create the server listening socket for port 1234
ServerSocketConnection scn = (ServerSocketConnection)"socket://:1234");
// Wait for a connection.
SocketConnection sc = (SocketConnection) scn.acceptAndOpen();

// Set application specific hints on the socket.
sc.setSocketOption(SocketConnection.DELAY, 0);
sc.setSocketOption(SocketConnection.LINGER, 0);
sc.setSocketOption(SocketConnection.KEEPALIVE, 0);
sc.setSocketOption(SocketConnection.RCVBUF, 128);
sc.setSocketOption(SocketConnection.SNDBUF, 128)

// Get the input stream of the connection.
DataInputStream is = sc.openDataInputStream();

// Get the output stream of the connection.
DataOutputStream os = sc.openDataOutputStream();
} catch (Exception e) {

All times are GMT -5. The time now is 05:15 PM.

Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.