Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 23
Registered: ‎12-01-2009
My Device: Not Specified

Urgent::Reading socket response from inputstream taking long time

Hi All,

 

I am trying to create a SecureSocket connection on RIM device.and getting the response from the server.

I am trying to read InputStream from the socket connection, it is taking around 3mins to read complete response data.

 

Please help me; how I could resolve this.

 

Code sample is here:

 

String urlSSL = "ssl:.//" +get_host()+":"+get_Port()+";deviceside=true";

SecureConnection sc = (SecureConnection)Connector.open(urlSSL);

 

sc.setSocketOption(SocketConnection.LINGER, 5);                
OutputStream os = sc.openOutputStream();

 

byte[] data =getDataToSend();
                   
if(data != null)
         os.write(data);
os.flush();

 

                        //read from Socket
                        InputStream is  = sc.openInputStream();                
                   
                        ByteArrayOutputStream bout = new ByteArrayOutputStream();
                       
                        boolean aReadingStatus = false;
                       
                        try
                        {
                            int bytesToRead=1024;
                            byte[] buffer = new byte[bytesToRead];
                            while((is.read(buffer, 0, bytesToRead)) != -1)
                              bout.write(buffer);
                            aReadingStatus = true;
                        }
                        catch(Exception e)
                        {
                            System.out.println("Exception in reading response");
                            aReadingStatus = false;
                        }

 

I have tried to read one bye one char too, but still the result is same.

while i am trying to do "is.read()" it took almost 3mins to read complete data and process next stmt.

 

Please let me know what I am doing wrong here.

 

Thank you,

Regards,

Aparna

 

Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: Urgent::Reading socket response from inputstream taking long time

You have "ssl:.//", not "ssl://" -- might be something to look at.

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Urgent::Reading socket response from inputstream taking long time

Another issue is that you are the read method can read less that 1024 bytes, but you are always writing 1024 bytes into the output in every iteration of the loop...

Developer
Posts: 23
Registered: ‎12-01-2009
My Device: Not Specified

Re: Urgent::Reading socket response from inputstream taking long time

Hi,

 

First of all there was typing mistake "ssl:.//"..  it is "ssl://" only in my code.

 

I have tried code with :

int len=0;
                        int ch=0;
                        while(ch != -1)
                        {
                            ch = is.read();                           
                            if(ch != -1)
                            {
                                bout.write(ch);
                                len++;
                            }
                        }

 

and

 

 int bytesToRead=1024;
                                byte[] buffer = new byte[bytesToRead];
                                int readBytes = 0;
                                while((readBytes = (is.read(buffer, 0, bytesToRead))) != -1)
                                 bout.write(buffer, 0, readBytes);

 

Still not taking time.

 

Please provide me the code, where I am doing wrong.

 

Thank you,

Regards,

Aparna

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Urgent::Reading socket response from inputstream taking long time

You ask:

"Please provide me the code, where I am doing wrong."

However there is a lot of work to be done to determine where the problem actually is before anybody can suggest what changes  you might consider.

 

Here are some questions that will help tie this problem down:

 

1) You say this process is taking 3 minutes - is this on the device or on the Simulator.

 

2) Have you tested this code on the Simulator?  If you have not, please do.  Do you see the same problem there?

 

3) How much data are you trying to send. 

 

4) What connection method are you trying to use?

 

5) Have you attempted to get similar socket data from the Server from another platform (like a PC)?  If not, can you try this.  Does it take just as long?

 

Developer
Posts: 23
Registered: ‎12-01-2009
My Device: Not Specified

Re: Urgent::Reading socket response from inputstream taking long time

Hi,

 

1) I am testing on device.

2) Could not test on Simulator, because the data which I am sending calculated using device information.

3) The data I am sending alomost 300bytes.

4) I am using secure connection

 

I openned connection with "deviceside=true" with "ssl://", the protocol name returned from after connection as "TLS".

When I used "END_TO_END_REQUIRED" or "END_TO_END_DESIRED" it returns "SSL".

 

There is one more problem I am facing:

I want to write new data on same secure connection.

 

I have tried the following:

1. Open connection if it is null

2. Open output stream

3. Open input stream

4. Write data in OS

5. Read from IS.

6. Write data in same OS second time after sometime : Gives connectionclosed exception (I have not closed it yet)

 

If I closed OS and IS, and reopen OS it gives exception at secureconnection.openOutputStream(). IOException, connection closed.

 

How i could use the same SecureConnection to write data second time?

 

 

Regards,

Aparna

Developer
Posts: 23
Registered: ‎12-01-2009
My Device: Not Specified

Re: Urgent::Reading socket response from inputstream taking long time

I would like to add one more thing.

I am getting the ConnectionCloseException while doing outputStream.flush().

 

Code is like:

 

if(isConnectionOpen == false)

{

 // open SSLConnection

// Open output stream

// open inputstream

isConnectionOpen = true;

}

 

outputStream.write(mydata);

outputStream.flush();  // here I am getting conneciotn closed exception: Bad Socket ID while writing second time

 

//Reading from inputstream

 

 

I am not closing connection, the code for closing connection is called when 2nd time reading and writing completes.

 

Let me know any good solution for using same socket connection 2nd time to get response.

 

Regards,

Aparna

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Urgent::Reading socket response from inputstream taking long time

Have a look at the logs on your sever. It may well be that the connection is closed by the server, say, after a certain timeout.

Developer
Posts: 23
Registered: ‎12-01-2009
My Device: Not Specified

Re: Urgent::Reading socket response from inputstream taking long time

I have checked on server logs and as par logs the server closes the connection after 2min, and my application is taking longer time to send next request.

I have debug the code and log time in a log file.Most of the time is consuming by the application in reading first response data.

So I am again stuck on my first problem ie how do I make reading fast.

 

Regards,

Aparna