Uploaded image for project: 'Commons Net'
  1. Commons Net
  2. NET-284

problem with TelnetClient.setSoTimeout() : not the proper behaviour

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Invalid
    • Affects Version/s: 2.0
    • Fix Version/s: None
    • Component/s: Telnet
    • Labels:
      None
    • Environment:

      Windows XP, Sun JRE 6

      Description

      Hi all,
      Correct me if I'm wrong but I think that the telnetClient is not handling properly timeouts.

      Just look at this junit testCase :

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.io.PrintWriter;
      import java.net.SocketException;
      import java.net.SocketTimeoutException;
      
      import junit.framework.TestCase;
      import org.apache.commons.net.telnet.*;
      
      import org.junit.Test;
      
      public class TelnetClientTest extends TestCase {
      	private TelnetClient telnetClient;
      	private BufferedReader in= null;
      	private PrintWriter out= null;
      	
      	@Test
      	public void testSetSoTimeout() {
      		
      		telnetClient = new TelnetClient();
      		
      		/*
      		 * FIRST SCRIPT
      		 */
      		try {
      			telnetClient.connect("192.168.0.95", 23);
      		} catch (SocketException e) {
      			e.printStackTrace();
      			this.disconnect();
      			fail("Error connect");
      		} catch (IOException e) {
      			e.printStackTrace();
      			this.disconnect();
      			fail("Error connect");
      		}
      		assertTrue ("telnet client not connected", telnetClient.isConnected());
      		
      		in = new BufferedReader(new InputStreamReader(telnetClient.getInputStream()));
      		out = new PrintWriter(telnetClient.getOutputStream());
      		String mes = null;
      		
      		// LOGIN
      		mes = getMessageFromTelnetServer(300);
      		assertNotNull("mes is null", mes);
      		assertTrue("Pas de prompt login", mes.endsWith("login: "));
      		out.println("root");
      		out.flush();
      		
      		// PASSWORD
      		mes = getMessageFromTelnetServer(300);
      		assertNotNull("mes is null", mes);
      		assertTrue("Pas de prompt password", mes.endsWith("Password: "));
      		out.println("rootpwd");
      		out.flush();
      		
      		// PROMPT
      		mes = getMessageFromTelnetServer(300);
      		assertNotNull("mes is null", mes);
      		assertTrue("no prompt", mes.endsWith("$ "));
      		
      		// SEND PWD
      		out.println("pwd");
      		out.flush();
      		
      		// WAIT A LITTLE BIT TOO MUCH
      		try {
      			Thread.sleep(1000);
      		} catch(InterruptedException e) {
      		}
      		
      		// READ RESULT OF PWD
      		mes = getMessageFromTelnetServer(300);
      		
      		// READ AGAIN ... YES !
      		String mes2 = getMessageFromTelnetServer(300);
      		
      		// CRASH HERE
      		assertTrue("no result to pwd", mes.endsWith("$ "));
      		
      		// AND HERE
      		assertEquals("should be empty but we get result of pwd", "" , mes2);
      		
      		this.disconnect();	
      		
      	}
      	
      	private void disconnect() {
      		try {
      			telnetClient.disconnect();
      		} catch (IOException e) {}
      	}
      	
      	private String getMessageFromTelnetServer(int timeout) {
      		int numberOfBytes = 0;
      		char[] msgChar = new char[2000];
      		
      		try {
      			telnetClient.setSoTimeout(timeout);
      		} catch (Exception e) {
      			return "";
      		}
      		
      		String res = "";
      		read_loop:
      		while (numberOfBytes >= 0) {
      			try {
      				numberOfBytes = in.read(msgChar, 0, msgChar.length);
      			} catch (SocketTimeoutException e) {
      				break read_loop;
      			} catch (IOException e) {
      				e.printStackTrace();
      				break read_loop;
      			}
      			res = res + String.copyValueOf(msgChar, 0, numberOfBytes);
      		}
      		return res;
      	}
      
      }
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              dalou dalouuu
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: