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

                 Key: NET-284
                 URL: https://issues.apache.org/jira/browse/NET-284
             Project: Commons Net
          Issue Type: Bug
    Affects Versions: 2.0
         Environment: Windows XP, Sun JRE 6
            Reporter: dalouuu


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("gptoadm");
                out.flush();
                
                // PASSWORD
                mes = getMessageFromTelnetServer(300);
                assertNotNull("mes is null", mes);
                assertTrue("Pas de prompt password", mes.endsWith("Password: 
"));
                out.println("anatole");
                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;
        }

}


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to