08-29-2011 09:53 AM
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;
}
}
Solved! Go to Solution.
08-29-2011 05:44 PM
" suddenly stops "
Throws an exception or hangs.
Anything else you can tell us, unfortunately not a lot to go on here.
08-29-2011 06:04 PM
08-29-2011 06:31 PM
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.
08-31-2011 10:30 AM
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.
08-31-2011 07:17 PM
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;
}
09-01-2011 09:02 AM