BlackBerry Forums Support Community

BlackBerry Forums Support Community (http://www.blackberryforums.com/)
-   Developer Forum (http://www.blackberryforums.com/developer-forum/)
-   -   How to Read from a socket ?? (http://www.blackberryforums.com/developer-forum/255569-how-read-socket.html)

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)in.read());

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

}

It gets stalled.

I tried to use InpuStream.read() 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:

Code:

  SocketConnection sc = (SocketConnection)
                        Connector.open("socket://host.com:79");
  sc.setSocketOption(SocketConnection.LINGER, 5);

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

  os.write("\r\n".getBytes());
  int ch = 0;
  while(ch != -1) {
      ch = is.read();
  }

  is.close();
  os.close();
  sc.close();

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 Connector.open() 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 ??
 
Hi,
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)Connector.open("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
try{
ServerSocketConnection scn = (ServerSocketConnection)Connector.open("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) {
System.out.println(e.toString());
}
}
}


All times are GMT -5. The time now is 05:35 AM.

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