I'm trying to log in to Windows Server 2008 Telnet Service. I have code, readUntil(), that gets an InputStream and does a read() until it finds 'ogin: ' or 'assword: ' and flags if it found it.
On 2.0 it works fine, but under 3.1 and 3.2 in one section or the other available() never returns anything more, and the "last part" of the data never gets read(): Working: tel: org.apache.commons.net.telnet.TelnetClient@17b2b2 is: java.io.BufferedInputStream@fe0fd9 os: org.apache.commons.net.io.ToNetASCIIOutputStream@1c293f8 readUntil() inStr: 'ogin: ' iAvail: 2 iByte: 87 iByte: 101 Dropped out iAvail: 45 iByte: 108 iByte: 99 iByte: 111 iByte: 109 iByte: 101 iByte: 32 iByte: 116 iByte: 111 iByte: 32 iByte: 77 iByte: 105 iByte: 99 iByte: 114 iByte: 111 iByte: 115 iByte: 111 iByte: 102 iByte: 116 iByte: 32 iByte: 84 iByte: 101 iByte: 108 iByte: 110 iByte: 101 iByte: 116 iByte: 32 iByte: 83 iByte: 101 iByte: 114 iByte: 118 iByte: 105 iByte: 99 iByte: 101 iByte: 32 iByte: 13 iByte: 10 iByte: 10 iByte: 13 iByte: 108 iByte: 111 iByte: 103 iByte: 105 iByte: 110 iByte: 58 iByte: 32 -- FOUND -- Dropped out .writeCmd() readUntil() inStr: 'assword: ' iAvail: 1 iByte: 65 Dropped out iAvail: 24 iByte: 100 iByte: 109 iByte: 105 iByte: 110 iByte: 105 iByte: 115 iByte: 116 iByte: 114 iByte: 97 iByte: 116 iByte: 111 iByte: 114 iByte: 10 iByte: 13 iByte: 112 iByte: 97 iByte: 115 iByte: 115 iByte: 119 iByte: 111 iByte: 114 iByte: 100 iByte: 58 iByte: 32 -- FOUND -- Dropped out .writeCmd() ==================================== Doesn't read last part of Password section: tel: org.apache.commons.net.telnet.TelnetClient@1f10a67 is: java.io.BufferedInputStream@b3b6a6 os: org.apache.commons.net.telnet.TelnetOutputStream@9c8c3f readUntil() inStr: 'ogin: ' iAvail: 1 iByte: 87 Dropped out iAvail: 46 iByte: 101 iByte: 108 iByte: 99 iByte: 111 iByte: 109 iByte: 101 iByte: 32 iByte: 116 iByte: 111 iByte: 32 iByte: 77 iByte: 105 iByte: 99 iByte: 114 iByte: 111 iByte: 115 iByte: 111 iByte: 102 iByte: 116 iByte: 32 iByte: 84 iByte: 101 iByte: 108 iByte: 110 iByte: 101 iByte: 116 iByte: 32 iByte: 83 iByte: 101 iByte: 114 iByte: 118 iByte: 105 iByte: 99 iByte: 101 iByte: 32 iByte: 13 iByte: 10 iByte: 10 iByte: 13 iByte: 108 iByte: 111 iByte: 103 iByte: 105 iByte: 110 iByte: 58 iByte: 32 -- FOUND -- Dropped out .writeCmd() readUntil() inStr: 'assword: ' iAvail: 13 iByte: 65 iByte: 100 iByte: 109 iByte: 105 iByte: 110 iByte: 105 iByte: 115 iByte: 116 iByte: 114 iByte: 97 iByte: 116 iByte: 111 iByte: 114 Dropped out ==================================== Doesn't read last part of Login section: tel: org.apache.commons.net.telnet.TelnetClient@1f5b4d1 is: java.io.BufferedInputStream@1cee792 os: org.apache.commons.net.telnet.TelnetOutputStream@c5577c readUntil() inStr: 'ogin: ' iAvail: 20 iByte: 87 iByte: 101 iByte: 108 iByte: 99 iByte: 111 iByte: 109 iByte: 101 iByte: 32 iByte: 116 iByte: 111 iByte: 32 iByte: 77 iByte: 105 iByte: 99 iByte: 114 iByte: 111 iByte: 115 iByte: 111 iByte: 102 iByte: 116 Dropped out Here's the code: //{{{ readUntil() // Note: // // This works under 2.0, but in 3.2 it never gets the // last part of 'assword:', and in 3.1 it never gets the // last part of 'ogin:' // protected boolean readUntil(InputStream is, String inStr) { if ( Util.DEBUG ) { System.out.println("readUntil()"); System.out.println("inStr: '"+inStr+"'"); } byte[] buffer = new byte[2048]; byte bByte; boolean bRet = false; int iAvail = 0; int iBytesRead = 0; int loc; int iBufP = 0; int iLoopCount = 0; int iByte = 0; int i; String blockStr = ""; for ( i = 0; i < 2048; i++ ) buffer[i] = (byte)0x00; while ( true ) { try { iAvail = is.available(); if ( iAvail > 0 ) { if ( Util.DEBUG ) System.out.println("iAvail: "+iAvail); while ( iAvail > 0 ) { iByte = is.read(); if ( iByte == -1 ) { // EOS.. if ( Util.DEBUG ) System.out.println("Hit EOS, breaking.."); break; } else { if ( Util.DEBUG ) System.out.println("iByte: "+iByte); buffer[iBufP] = (byte)iByte; iBufP++; iBytesRead++; iAvail--; blockStr = new String(buffer); loc = blockStr.indexOf(inStr); if ( loc != -1 ) { if ( Util.DEBUG ) System.out.println("-- FOUND --"); bRet = true; break; } } } if ( Util.DEBUG ) System.out.println("Dropped out"); } } catch (IOException ioe) { System.out.println("readUntil() Exception"); ioe.printStackTrace(); } if ( (iByte == -1) || (bRet == true) ) break; } // End while.. return bRet; } //}}}