BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 09-15-2007, 07:14 PM   #1 (permalink)
New Member
 
Join Date: Sep 2007
Model: 8830
PIN: N/A
Carrier: Sprint
Posts: 14
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Trouble with socketconnection, Data transmits ok but EOF times out

Please Login to Remove!

I'm using a SocketConnection to send commands to a server and receive responses. The data sent and recieved by the server is correct. What's wrong is the EOF is timing out causing the data transmission to take much longer than it should.

I timed the response sent by the server and it takes ~300 miliseconds to recieve ~800 bytes. The EOF then takes 120 seconds (default timeout) before it is recieved. InputStream.read() blocks, so my program hangs for 2 minutes every time it communicates with the server.

I tested this in a regular midlet with the sun emulator and it worked correctly. I'm only seeing this bug in my blackberry emulator.

any ideas?

Code:
String url = "socket://"+ host +":"+port+";deviceside=true";
SocketConnection sc = (SocketConnection) Connector.open(url);

// open streams
is = sc.openInputStream();
os = sc.openOutputStream();

// to read input from server
byte[] buffer = new byte[2048];

os.write(output)
is.read(buffer);
Offline  
Old 09-16-2007, 07:50 PM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: Jun 2007
Model: 8800
PIN: N/A
Carrier: GPRS
Posts: 68
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

If deviceside=true is specified in the connection URI, a direct TCP connection is opened from the handheld, without using the BlackBerry MDS Connection Service.
If deviceside=false is specified in the connection URI, a proxy TCP connection is opened using the BlackBerry MDS Connection Service.
The code from RIM device Java Library,Maybe you can get something from it
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();
Offline  
Old 09-16-2007, 09:46 PM   #3 (permalink)
New Member
 
Join Date: Sep 2007
Model: 8830
PIN: N/A
Carrier: Sprint
Posts: 14
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by streamh View Post
If deviceside=true is specified in the connection URI, a direct TCP connection is opened from the handheld, without using the BlackBerry MDS Connection Service.
If deviceside=false is specified in the connection URI, a proxy TCP connection is opened using the BlackBerry MDS Connection Service.
The code from RIM device Java Library,Maybe you can get something from it
i tried both MDS and direct tcp. I'll probably add an option to my app to toggle it but i'm more concerned about direct tcp since thats how ill be using the app.
  • direct tcp (deviceside=true) - works as described above. data is sent, then EOF is eventually sent after 120seconds.
  • MDS - timed out exception thrown after 120 seconds. I used code similar to your example to read 1 byte at a time and it does actually receive the data, but the EOF times out causing the exception.


I tried the things shown in your example. reading 1 byte at a time and setting the LINGER parameter but no change in results. I also set KEEPALIVE to 2 seconds and it had no effect.
Offline  
Old 09-20-2007, 08:50 AM   #4 (permalink)
iwr
New Member
 
Join Date: Sep 2007
Model: 8800
PIN: N/A
Carrier: Orange
Posts: 14
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

---

Last edited by iwr : 09-20-2007 at 04:34 PM.
Offline  
Old 09-20-2007, 09:23 AM   #5 (permalink)
iwr
New Member
 
Join Date: Sep 2007
Model: 8800
PIN: N/A
Carrier: Orange
Posts: 14
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by kreneskyp View Post
I tried the things shown in your example. reading 1 byte at a time and setting the LINGER parameter but no change in results.
Same here, even worse: I receive "eof" only when the server closes connection

UPDATE:
I solved it usin InputStream.available(), like this:
do
{
ch = (byte)is.read();
}
while (is.available() > 0);

Last edited by iwr : 09-20-2007 at 10:39 AM.
Offline  
Old 09-20-2007, 01:29 PM   #6 (permalink)
iwr
New Member
 
Join Date: Sep 2007
Model: 8800
PIN: N/A
Carrier: Orange
Posts: 14
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

kreneskyp,
do you succeed to open ServerSocketConnection? For some reason the function Connector.open("socket://") blocks and never returns here.
by the way - is it possible to define "direct" server socket? i mean something like this: "socket://;deviceside=true" (i don't have bes/mds)

Last edited by iwr : 09-20-2007 at 04:35 PM.
Offline  
Old 09-20-2007, 09:10 PM   #7 (permalink)
New Member
 
Join Date: Sep 2007
Model: 8830
PIN: N/A
Carrier: Sprint
Posts: 14
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm using a SocketConnection and opening both and inputstream and outputstream. Both work except for the EOF thing (i havent tried your fix yet). I'm also using a direct tcp connection.

Code:
String url = "socket://"+ host +":"+port+";deviceside=true";
SocketConnection sc = (SocketConnection) Connector.open(url);
Offline  
Old 09-20-2007, 10:19 PM   #8 (permalink)
New Member
 
Join Date: Sep 2007
Model: 8830
PIN: N/A
Carrier: Sprint
Posts: 14
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by iwr
UPDATE:
I solved it usin InputStream.available(), like this:
do
{
ch = (byte)is.read();
}
while (is.available() > 0);

are you using InputStream or something else?

Quote:
The available method for class InputStream always returns 0
Offline  
Old 09-21-2007, 03:45 AM   #9 (permalink)
iwr
New Member
 
Join Date: Sep 2007
Model: 8800
PIN: N/A
Carrier: Orange
Posts: 14
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

kreneskyp,
yes, it's InputStream. For some reason, you have to read from the stream _before_ you call available() - then it returns good value, not zero. That's why I used do...while()

"The available method for class InputStream always returns 0 "

well, you actually use a class derived from InputStream, so it's overriden there

Last edited by iwr : 09-21-2007 at 03:53 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





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