hi all!
can anybody help me on this, please!!!
i'm using the commons-net package to connect to a switch by telnet, and
using the readUntil method to read the telnet output... it looks like
this:
------------------------------ readUntil ---------------------------
public String readUntil( String pattern ) {
try {
char lastChar = pattern.charAt( pattern.length() - 1 );
StringBuffer sb = new StringBuffer();
char ch = (char) in.read();
int a = (-1);
while( ch != (char) a ) {
// System.out.print( ch );
sb.append( ch );
if( ch == lastChar ) {
if( sb.toString().endsWith( pattern ) ) {
return sb.toString();
}
}
ch = (char) in.read();
}
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
return null;
}
-------------------------------------------------------------------------------
so... the problem is, that if the pattern does not match any of the
output,
the method gets blocked... on (ch = (char) in.read();) inside the
loop...
it keeps waiting for the InputStream.read()...
dont know how to overcome this... tried to use sockedTimeout... like...
setting the socket timeout for the connection to 4 seconds, and
catching
SocketTimeoutException....
but is there any wiser solution to this???
thanks in advance!! appreciate!!!
Roedy Green - 23 Mar 2006 17:47 GMT
> char ch = (char) in.read();
> int a = (-1);
> while( ch != (char) a ) {
just use in int instead of a char. This is a c-ism that will read more
simply if you write it like this:
int ch;
while ( ( ch = in.read() ) >= 0 )

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.