BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 11-20-2011, 04:54 AM   #1 (permalink)
New Member
 
Join Date: Nov 2011
Model: xxx
PIN: N/A
Carrier: XXX
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Question How to Read from a socket ??

Please Login to Remove!

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....
Offline  
Old 11-20-2011, 02:40 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 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.
__________________
My other Blackberry is a PlayBook.
Offline  
Old 11-21-2011, 12:20 AM   #3 (permalink)
New Member
 
Join Date: Nov 2011
Model: xxx
PIN: N/A
Carrier: XXX
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: How to Read from a socket ??

Yes , that was perfect .
I was trying to create a server socket in BB
Any ideas....
Offline  
Old 11-21-2011, 06:44 AM   #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 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.
__________________
My other Blackberry is a PlayBook.
Offline  
Old 11-22-2011, 04:57 AM   #5 (permalink)
New Member
 
Join Date: Nov 2011
Model: xxx
PIN: N/A
Carrier: XXX
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default 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......
Offline  
Old 11-22-2011, 07:42 AM   #6 (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 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.
__________________
My other Blackberry is a PlayBook.
Offline  
Old 04-24-2012, 05:00 AM   #7 (permalink)
New Member
 
Join Date: Apr 2012
Location: Indore
Model: 9700
PIN: N/A
Carrier: Airtel
Posts: 3
Post Thanks: 0
Thanked 0 Times in 0 Posts
Post 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());
}
}
}

Last edited by Ojasviharsola : 04-24-2012 at 05:02 AM.
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

Similar Threads for: How to Read from a socket ??
Thread Thread Starter Forum Replies Last Post
RSS and 9700: What is the best way to read RSS on 9700? What feeds do you read? plefkow General 9700 Series Discussion - Bold 2 27 10-01-2010 07:31 AM
Block Read Receipts kurizma BES Admin Corner 7 01-09-2010 07:00 AM
Socket writing and reading image on blackberry device satyarajasekhar Developer Forum 1 01-08-2010 05:22 AM
SYNC error oat1560 General 8300 Series Discussion - Curve 20 06-18-2009 03:56 PM
8gb micro sdhc card will not read jenb720 Media Center 2 06-01-2009 02:03 PM





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