BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 11-20-2011, 05:54 AM   #1
rammohanrg
New Member
 
Join Date: Nov 2011
Model: xxx
PIN: N/A
Carrier: XXX
Posts: 4
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, 03:40 PM   #2
hrbuckley
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
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, 01:20 AM   #3
rammohanrg
New Member
 
Join Date: Nov 2011
Model: xxx
PIN: N/A
Carrier: XXX
Posts: 4
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, 07:44 AM   #4
hrbuckley
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
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, 05:57 AM   #5
rammohanrg
New Member
 
Join Date: Nov 2011
Model: xxx
PIN: N/A
Carrier: XXX
Posts: 4
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, 08:42 AM   #6
hrbuckley
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
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
Ojasviharsola
New Member
 
Join Date: Apr 2012
Location: Indore
Model: 9700
PIN: N/A
Carrier: Airtel
Posts: 3
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



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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 08:00 AM
Socket writing and reading image on blackberry device satyarajasekhar Developer Forum 1 01-08-2010 06: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


128K RAM - APPLE - ORIGINAL APPLE prototype BOARD picture

128K RAM - APPLE - ORIGINAL APPLE prototype BOARD

$408.75



APPLE 630-0895-B  VRAM 128K X 8 BOARD CARD VINTAGE picture

APPLE 630-0895-B VRAM 128K X 8 BOARD CARD VINTAGE

$74.77



APPLE 820-0522-A 630-0895-B LITE VRAM 128K X 8 BOARD  picture

APPLE 820-0522-A 630-0895-B LITE VRAM 128K X 8 BOARD

$149.99







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