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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
New Contributor
gsantos
Posts: 8
Registered: ‎08-29-2011
My Carrier: Claro
Accepted Solution

Transferring bytes from socket server stops suddenly

I was trying to get an response from a socket server (VB6) but when It is Transferring bytes, suddenly stops after the byte number 590, and this all has 626 bytes. There is other way to improve this transaction ? Please if you have no time don't waste your time here only show me the point is sufficient. so, It's very important to me

 

Thanks in advance..

 

Main code below:

 

byte[] LeBytes(int tam) {

int i = 0,

lebyte = 0;

byte[] bytearray = new byte[tam];

 

try {

  while(i< tam){

     lebyte = is.read();

     bytearray[i] = (byte)lebyte;
     i++;

 }

 

 bytearray=Lib_Arquivos.RC4_Crypto(bytearray, Lib_Arquivos.chave);

 return bytearray;


} catch (IOException e) {

      System.out.println(e.getMessage());

      e.printStackTrace();

      return null;

}

}

Please use plain text.
Developer
peter_strange
Posts: 17,664
Registered: ‎07-14-2008

Re: Transferring bytes from socket server stops suddenly

" suddenly stops "

 

Throws an exception or hangs.

 

Anything else you can tell us, unfortunately not a lot to go on here. 

Please use plain text.
New Contributor
gsantos
Posts: 8
Registered: ‎08-29-2011
My Carrier: Claro

Re: Transferring bytes from socket server stops suddenly

I saw in normal execution and at debug. It hangs just in that position 590 and nothing happens. There is a limitation or maybe i need to put some new code in that point ? I know that the server is okay because he serves other clients.
Please use plain text.
Developer
peter_strange
Posts: 17,664
Registered: ‎07-14-2008

Re: Transferring bytes from socket server stops suddenly

I have never seen a socket connection hang.  At most they will hang round for 2 minutes then time out.  So I am surprised by your comment.  Are you sure that the code did not just get an IOException?

 

I have sent many more bytes that 590 through a socket. 

Please use plain text.
New Contributor
gsantos
Posts: 8
Registered: ‎08-29-2011
My Carrier: Claro

Re: Transferring bytes from socket server stops suddenly

Peter,

 

yes,but that's it. I appreciated your help. I will rewrite this code to do some testes.

 

Do you have a link with more informations or maybe some code to share ?

 

Thanks again.

Please use plain text.
Developer
peter_strange
Posts: 17,664
Registered: ‎07-14-2008

Re: Transferring bytes from socket server stops suddenly

Here is some code.

 

This is edited version of code that I use to read data off a socket connection.  I've editted to simplify it slightly, I hope I have not broken it. 

 

I always know how much data to read, and I call this routine to read it all.  I used to have something simpler but I found that I needed to repeat reads sometimes, especially with the newer OS's.  In other words, I would get some of the bytes I expected, then the read would complete , without an error.  Where I know there is more data expected, I re-issue the read. 

 

I say I always know how much data.  I make sure of this in my communication with any server.  Typically the server will send me a length in 4 bytes, and then the data.  So I will call this routine twice, the first for 4 bytes, and then I convert those bytes to a int, and call this routine again for the actual data. 

 

Hope this helps.

 

    private static final int readLength(final InputStream _in, final byte [] buffer, final int startIndex, final int lengthExpected) throws IOException {
        int indexOfNextByte = startIndex;
        int lengthLeftToRead = lengthExpected;
        int lengthActuallyRead = 0;
        while ( lengthLeftToRead > 0 ) {
            lengthActuallyRead = _in.read(buffer, indexOfNextByte, lengthLeftToRead);
            if ( lengthActuallyRead < 0 ) {
                System.out.println("Unexpected EOF, " + lengthLeftToRead + " bytes to read.");
                throw new RuntimeException("Unexpected Receive EOF");
            } else
            if ( lengthActuallyRead == 0 ) {
                System.out.println("Expected data, got 0 bytes, " + lengthLeftToRead + " bytes to read.");
                throw new RuntimeException("Unexpected Receive 0 bytes");
            } else {
                lengthLeftToRead = lengthLeftToRead - lengthActuallyRead;
                indexOfNextByte = indexOfNextByte + lengthActuallyRead;
                if ( lengthLeftToRead > 0 ) {
                    System.out.println("Repeat Read request, got " + lengthActuallyRead + ", remaining " + lengthLeftToRead);
                }
            }
        }
        byte [] partialBuffer = Arrays.copy(buffer, startIndex, lengthExpected);
        System.out.println("Read:X\"" + byteArrayToHexString(partialBuffer) + "\".");
        return lengthExpected - lengthLeftToRead;
    }

 

Please use plain text.
New Contributor
gsantos
Posts: 8
Registered: ‎08-29-2011
My Carrier: Claro

Re: Transferring bytes from socket server stops suddenly

Amazing, thanks! I saw where is the error after have a look in your code. Anyway there is a strange behave: is.avaliable always returning zero and the last byte have been never -1.
Please use plain text.