Return-Path: X-Original-To: apmail-commons-user-archive@www.apache.org Delivered-To: apmail-commons-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CC940DC8D for ; Mon, 15 Oct 2012 14:16:36 +0000 (UTC) Received: (qmail 85459 invoked by uid 500); 15 Oct 2012 14:16:35 -0000 Delivered-To: apmail-commons-user-archive@commons.apache.org Received: (qmail 85320 invoked by uid 500); 15 Oct 2012 14:16:35 -0000 Mailing-List: contact user-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Users List" Delivered-To: mailing list user@commons.apache.org Delivered-To: moderator for user@commons.apache.org Received: (qmail 28075 invoked by uid 99); 15 Oct 2012 13:26:22 -0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of mark@adinsight.com designates 87.237.62.236 as permitted sender) Message-ID: <507C0EDE.8060105@adinsight.com> Date: Mon, 15 Oct 2012 14:25:50 +0100 From: Mark Frimston User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120912 Thunderbird/15.0.1 MIME-Version: 1.0 To: user@commons.apache.org Subject: [net] TelnetClient dropping bytes? Content-Type: multipart/mixed; boundary="------------080806020203020206030208" X-Virus-Checked: Checked by ClamAV on apache.org --------------080806020203020206030208 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 --------------080806020203020206030208 Content-Type: text/x-java; name="Test.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Test.java" 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(); } } } --------------080806020203020206030208 Content-Type: text/plain; charset=us-ascii --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@commons.apache.org For additional commands, e-mail: user-help@commons.apache.org --------------080806020203020206030208--