Hi

I'm using TelnetClient to send and receive commands from a server, each command consisting of character data followed by a newline. However, at seemingly random times the response obtained from TelnetClient's InputStream appears to have bytes missing, or sometimes includes extra bytes.

I was able to reproduce the problem on Ubuntu, Windows and OSX and I am using commons-net version 3.1 and Oracle's 1.6 JDK.

I'm not sure if this is a bug or whether I'm just not using TelnetClient correctly, as I was unable to find any previous reports of similar problems. I've attached the simple test program I used to recreate the issue. The program connects to a basic socket server in a loop until the message echoed back is not the same as the one sent, which I've seen happen anywhere between 2-400 iterations.

Can anybody confirm the problem or else point out what mistake I've made?


Many thanks,

Mark
package telnetbug.telnetbug;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.commons.net.telnet.TelnetClient;

public class Test 
{
	static class EchoServer extends Thread {
		
		boolean stop = false;
		ServerSocket ss;
		Socket cs;		
		
		public void run(){
			try{
				ss = new ServerSocket(4444);
				while(!stop){
					cs = ss.accept();
					InputStream is = cs.getInputStream();
					OutputStream os = cs.getOutputStream();
					while(!stop){
						try{
							String msg = read(is);
							if(msg==null) break;							
							write(os,msg);							
						}catch(IOException e){
							break;
						}
					}
				}
			}catch(IOException e){
				throw new RuntimeException(e);
			}
			finally{
				shutdown();
			}
		}
		
		public void shutdown(){
			stop = true;
			if(cs!=null) try{ cs.close(); }catch(IOException e){}
			if(ss!=null) try{ ss.close(); }catch(IOException e){}			
		}
	}
	
	static String read(InputStream is) throws IOException {
		String s = "";		
		while(true){
			int c = is.read();
			if(c==-1) return null;
			if(c==(int)'\n') break;
			s += (char)c;
		}
		return s;				
	}
	
	static void write(OutputStream os, String msg) throws IOException {
		os.write((msg+"\n").getBytes("US-ASCII"));
		os.flush();
	}
		
	public static void main(String[] args) throws Exception {
		
		String testString = "abcdefghijklmnopqrstuvwxyz";
		
		EchoServer s = new EchoServer();
		try{
			s.start();
													
			int i=0;
			while(true){
				TelnetClient c = null;
				try{
					c = new TelnetClient();
					c.connect("127.0.0.1",4444);					
					InputStream is = c.getInputStream();
					OutputStream os = c.getOutputStream();
					
					write(os,testString);
					String received = read(is);
					
					if(!received.equals(testString)){
						System.out.println(String.format("Failed on iteration %d:\n"+
								"sent\t\"%s\"\ngot\t\"%s\"",i,testString,received));
						break;					
					}					
					i++;
					try{ Thread.sleep(50); }catch(InterruptedException e){}
					System.out.print(".");
					
				}finally{
					if(c!=null) c.disconnect();
				}	
			}						
		}finally{
			s.shutdown();
		}
	}
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to