You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Sebestyén Zoltán <Zo...@netvisor.hu> on 2003/12/18 00:44:50 UTC

socket timed out BEFORE timeout exceed with commons net telnet

Hi,

 I've met the following problem while using commons net telnet's TelnetClient class:

I've set TelnetClient's timeout to 5000 millisecs to make sure that I have enought time to read  when I read some input char by char from the server I've connected to. *Most* of the time it works, however sometimes the socket throws a java.net.SocketTimeoutException *before* the actual timeout exceed, mostly instantly after the read method has been called. Please tell me what do think the problem can be. Here's the stack trace ('aTimeout' and 'aSoTimeout' are actually read right when printing out the exception's message):

2003-12-18 00:14:27,706 INFO  [com.netvisor.jadmin.telnet.PhysicalAccess] readUntil(bash-2.03$ )
2003-12-18 00:14:27,707 INFO  [com.netvisor.jadmin.telnet.PhysicalAccess] mazsola: Read problem at pgrep -f ipxcaudit text read so far: '', aTimeout: 5000, aSoTimeout: 5000
java.net.SocketTimeoutException: Read timed out
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(SocketInputStream.java:129)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:183)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:201)
        at java.io.FilterInputStream.read(FilterInputStream.java:66)
        at java.io.PushbackInputStream.read(PushbackInputStream.java:120)
        at org.apache.commons.net.io.FromNetASCIIInputStream.__read(FromNetASCIIInputStream.java:114)
        at org.apache.commons.net.io.FromNetASCIIInputStream.read(FromNetASCIIInputStream.java:209)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:183)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:201)
        at org.apache.commons.net.telnet.TelnetInputStream.__read(TelnetInputStream.java:140)
        at org.apache.commons.net.telnet.TelnetInputStream.run(TelnetInputStream.java:464)
        at java.lang.Thread.run(Thread.java:534)


And here's the code:

/** Reads until Pattern. */
	public String readUntil(String pattern) throws TelnetException {
		log.info("readUntil(" + pattern + ")");
		StringBuffer sb = new StringBuffer();
		try {
			char lastChar = pattern.charAt(pattern.length() - 1);
			//boolean found = false;
			char ch = (char) in.read();
			while (true) {
				//log.error(ch);
				sb.append(ch);
				if (ch == lastChar) {
					if (sb.toString().endsWith(pattern)) {
						log.info(
							"Got the "
								+ pattern
								+ ", text read: "
								+ sb);
						return sb.toString();
					}
				}
				ch = (char) in.read();
			}
		} catch (Exception e) {
			int aTimeout = telnet.getDefaultTimeout();
			int aSoTimeout = -1;
			try {
				aSoTimeout = telnet.getSoTimeout();
			} catch (Exception e2) {
				// TODO: handle exception
			}
			String message =
				host
					+ ": Read problem at "
					+ section
					+ ", text read so far: '"
					+ sb.toString()
					+ "', aTimeout: " + aTimeout + ", aSoTimeout: " + aSoTimeout;
			log.info(message, e);

			throw new TelnetException(message, e);
		}
	}

Regards,

Zoltan

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org