J
John Goche
Hello,
I am using the BlackBerry J2ME emulator to establish an
SSL connection. While connected I accept the certificate,
write some data, then read some data from the input stream
as follows. However, after reading is.available() number of
bytes (see code below), the input stream is starts returning
garbage (not the bytes I see when I connect from my C++
SSL library code).
What am I supposed to do so that is.getBytes() does not
return junk bytes after returning int is.available() number of
bytes? Am I supposed to close() and then reopen() the input
streams? Should I buffer them somehow using producer and
consumer threads? I tried increasing the receive buffer size
but it did not help. What should I do to fix this problem.
Alternatively, anyone know of any good SSL libraries
out there that work (given that I am not having much
luck with the BlackBerry one?). What am I
doing wrong?
class Foo {
private SecureConnection sc;
private InputStream is;
private OutputStream os;
boolean open(String host, String port) {
try {
String str = "ssl://" + host + ":" + port;
sc = (SecureConnection) Connector.open(str);
//sc.setSocketOption(SocketConnection.LINGER, 5);
//sc.setSocketOption(SocketConnection.KEEPALIVE, 1);
sc.setSocketOption(SocketConnection.RCVBUF, 6000);
is = sc.openInputStream();
os = sc.openOutputStream();
return true;
} catch (IOException e) {
Dialog.alert("Could not open secure connection.");
}
return false;
}
// ...
private synchronized byte[] getBytes(int size) {
if (size > 0) {
byte[] bytes = new byte[size];
int count = 0;
int val;
try {
while (count < size) {
if ((val = is.read(bytes, count, size - count)) == -1) {
connectionClosed();
}
count += val;
}
} catch (IOException e) {
networkReadError();
}
return bytes;
}
return null;
}
}
I am using the BlackBerry J2ME emulator to establish an
SSL connection. While connected I accept the certificate,
write some data, then read some data from the input stream
as follows. However, after reading is.available() number of
bytes (see code below), the input stream is starts returning
garbage (not the bytes I see when I connect from my C++
SSL library code).
What am I supposed to do so that is.getBytes() does not
return junk bytes after returning int is.available() number of
bytes? Am I supposed to close() and then reopen() the input
streams? Should I buffer them somehow using producer and
consumer threads? I tried increasing the receive buffer size
but it did not help. What should I do to fix this problem.
Alternatively, anyone know of any good SSL libraries
out there that work (given that I am not having much
luck with the BlackBerry one?). What am I
doing wrong?
class Foo {
private SecureConnection sc;
private InputStream is;
private OutputStream os;
boolean open(String host, String port) {
try {
String str = "ssl://" + host + ":" + port;
sc = (SecureConnection) Connector.open(str);
//sc.setSocketOption(SocketConnection.LINGER, 5);
//sc.setSocketOption(SocketConnection.KEEPALIVE, 1);
sc.setSocketOption(SocketConnection.RCVBUF, 6000);
is = sc.openInputStream();
os = sc.openOutputStream();
return true;
} catch (IOException e) {
Dialog.alert("Could not open secure connection.");
}
return false;
}
// ...
private synchronized byte[] getBytes(int size) {
if (size > 0) {
byte[] bytes = new byte[size];
int count = 0;
int val;
try {
while (count < size) {
if ((val = is.read(bytes, count, size - count)) == -1) {
connectionClosed();
}
count += val;
}
} catch (IOException e) {
networkReadError();
}
return bytes;
}
return null;
}
}