Categories

Main
Database
Java
Microsoft.NET
Adabas
DB2
Informix
Microsoft SQL Server
MySQL
Oracle
Pervasive.SQL
PostgreSQL
Sybase
Other
ASP
ColdFusion
Crystal Reports
Delphi, C etc
JAVA
Microsoft.NET
Perl and the DBI
PHP
ANSI SQL
Unix Shell Scripts
Visual Basic
XML & XSLT
Corel Paradox
FileMaker
Microsoft Access
Microsoft Excel
Other PC Databases
Applications & Tools
Database Concepts & Design
EJB programming & troubleshooting
EJB design
General J2EE
XML & Web services
Web tier: servlets, JSP, Web frameworks
Performance and scalability
Industry news
TSS feedback
Mobicents Contributors
Mobicents Users
JSLEE Resource Adaptor Types
Planning JavaOne 2008
Sun Tech Days
Other Java conferences
Binary Web Services and XML
Metro and JAXB
GlassFish
GlassFish Plugins
Project jMaki
GlassFish WebTier
Mural
Java Development Tools
Java WS & XML Community News
JAXP
Java SE
6uN Early Access
Java Quick Starter
Java SE Snapshots: Project Feedback
JCK Forum
Feedback and Suggestions
JDK Distros
JDK Open Source
General JSR Discussion
JCP 2.6
JXTA Community Forum
ME Interest
ME Cool Apps
ME General Help
ME Feedback & Suggestions
ME Application Developer Interest
Blu-ray Disc Java
ME Developer Days
Squawk
Mobile Developer Alliance
OpenCable
LWUIT
JavaFX Script Language Discussion
OpenJFX General Discussion
Scene Graph
General Performance Discussion
Your Java Career
NetBeans 6.0
Servlets
JSP
JSF
Portals and Portlets
EJB and Other Java EE Technologies
Distributed Java
Object Relational Mapping
JDBC
Web Services
Swing / AWT / SWT / JFace
JNLP and Web Start
Java Micro Edition
Sockets and Internet Protocols
Threads and Synchronization
Performance
Applets
I/O and Streams
Other Java APIs
Game Development
Java in General (beginner)
Java in General (intermediate)
Java in General (advanced)
Programmer Certification (SCJP)
Developer Certification (SCJD)
Associate Certification (SCJA)
Web Component Certification (SCWCD)
EJB Certification (SCBCD)
Mobile Application Certification (SCMAD)
Architect Certification (SCEA)
Web Services Certification (SCDJWS)
XML Certification
Product and Other Certifications
Mock Exam Errata
Sun Certification Results
Authors' Corral
Book Reviews
Events
Bunkhouse Porch
Teachers' Lounge
Testing
OO, Patterns, UML and Refactoring
IDEs, Version Control and other tools
Ant, Maven and Other Build Tools
Linux / UNIX
Mac OS
HTML and JavaScript
XML and Related Technologies
Agile and Other Processes
General Computing
Security
Groovy
Scala
Other Languages
Struts
Application Frameworks
Other Open Source Projects
BEA/Weblogic
IBM/Websphere
Oracle/OAS
Apache/Tomcat
JBoss
Other Java Products and Servers
JavaRanch
Cattle Drive (java college)
Moderators Only
Trash Can
Jobs Offered
Jobs Wanted
Jobs Discussion
Meaningless Drivel
Programming Diversions
Blatant Advertising
Java Announcements
New To Java
Advanced Java
Java Applets
Networking
Threads and Synchronization
Java 2D
AWT / Swing
SWT / JFace
CLDC and MIDP
CDC and Personal Profile
Sun Java Wireless Toolkit
Enterprise JavaBeans
JavaServer Pages (JSP) and JSTL
Java Servlet
JavaServer Faces
Web Frameworks
Database
XML
Lucene
NetBeans
Eclipse
IntelliJ IDEA
JCreator
Other IDEs
Java Tutorials
Java Tips
Jobs Discussion
Jobs Offered
Jobs Wanted
Professional Certification
Forum Lobby
Java Blogs
Introductions
Reviews / Advertising
Suggestions & Feedback

Resources

Java Database
Linux
Coding
Mobile
Hardware
Software Development
Software Development
iOS,OS X
iOS,OS X
ORACLE
IBM DEVELOPER
IBM DEVELOPER
MSDN
MSDN


Tags

Sockets and Internet Protocols


Java trying to read Telnet banner (help appreciated)


I'm trying to use Java to read the banner from a telnet session. Currently, when I try to "talk" with the telnet machine I end up with nothing.Please do not hesitate to email me with any comments or suggetions. Thanks,Landon Manningyandonman@hotmail.comIn my method I do the following:...TelnetModule myTelnetModule = new TelnetModule()myTelnetModule.connect("HostNameWithTelnetRunning");myTelnetModule.getBanner()...Here's the rest of the code://    // Used to connect to the remote machine and initiate comunication.    // This method only takes care of establishing the control socket.    // No atempt is made by this method to login.    //    // @param hostName Name of the host to connect to.    //    // @return Returns true on success.    //    public boolean connect( String hostname )    {            boolean isSuccessful = false;                        if( State == CONNECTED )            {                // Disconnect the current connection first                this.disconnect();            }try            {                controlSocket = new Socket(hostname, telnetPort);                controlSocket.setSoTimeout(5000);                controlInputStream  = new BufferedReader(new InputStreamReader(controlSocket.getInputStream()));                controlOutputStream = new BufferedWriter(new OutputStreamWriter(controlSocket.getOutputStream()));                State = CONNECTED;                isSuccessful = true;                banner = getMoreInput();                            } catch (UnknownHostException e) {                if( logging != null )                    logging.writeLog( LoggingType.INFORMATION, "Problem connecting - host unknown." );            } catch (IOException e) {                if( logging != null )                    logging.writeLog( LoggingType.INFORMATION, "Problem connecting - IO problem." );            }return isSuccessful;    }/*     *  Method:     getMoreInput()     *  Purpose:    gets data from connection but limits the buffer and prevents hanging     *  Parameters: None     *  Return:     returns the string from of what was read     *  Exception:  throws ConstantInputConnectionNotExpected when reads more data than     *              MAX_BUFFER_SIZE     */    public String getMoreInput() throws com.exceptions.ConstantInputConnectionNotExpected    {        int count = 0;        int ifCount = 0;        int charRead;        StringBuffer buffer = new StringBuffer();                try        {            while( (count < MAX_BUFFER_SIZE) && (ifCount < 10) )            {                charRead = (int) controlInputStream.read();                if ( charRead == 255 )                {                    handleOption( controlInputStream,controlOutputStream );                     ifCount++;                }                else                {                    buffer.append( (char) charRead );                    count++;                    ifCount = 0;                }            }        }        catch( IOException e )        {            //  occurs when we are done reading or some other IO error        }                //  if banner is too large        if( count >= MAX_BUFFER_SIZE )        {            State = UNEXPECTED_STREAM;            disconnect();            throw new ConstantInputConnectionNotExpected();        }                return buffer.toString();    }//The part of the code that "talks" with the telnet session    private void handleOption(BufferedReader myReader, BufferedWriter myWriter)    {        int command=0, option=0;try        {            command = (int)myReader.read();            option = (int)myReader.read();            switch (command)            {                case 251:                case 252:                    myWriter.write(255);                    myWriter.write(254);                    myWriter.write(option);                    break;                case 253:                case 254:                    myWriter.write(255);                    myWriter.write(252);                    myWriter.write(option);                    break;                default:                    break;            }            myWriter.flush();        } catch( Exception e )        {            if( logging != null )                logging.writeLog( LoggingType.ERROR, "TelnetModule: Error.", e );        }    }Thanks (agian),Landon Manningyandonman@hotmail.com

   
   

   
   
      The following code works, except for one minor error. The ifCount variable keeps track of the number of consecutive INTERRUPT_AS_COMMANDs. It was exiting the function once it had more than 10 of them. Well, some connetions negotiate their options for more than 10 IACs.I modified the code so that the handleOption method throws an IOException upon finding a IAC (255) in both the command and option reads on the stream. I use this condition (the two IAC reads) as the indicator that we are done negotiating with the telnet server.quote:Originally posted by landon manning:I'm trying to use Java to read the banner from a telnet session. Currently, when I try to "talk" with the telnet machine I end up with nothing.Please do not hesitate to email me with any comments or suggetions. Thanks,Landon Manningyandonman@hotmail.comIn my method I do the following:...TelnetModule myTelnetModule = new TelnetModule()myTelnetModule.connect("HostNameWithTelnetRunning");myTelnetModule.getBanner()...Here's the rest of the code://    // Used to connect to the remote machine and initiate comunication.    // This method only takes care of establishing the control socket.    // No atempt is made by this method to login.    //    // @param hostName Name of the host to connect to.    //    // @return Returns true on success.    //    public boolean connect( String hostname )    {            boolean isSuccessful = false;                        if( State == CONNECTED )            {                // Disconnect the current connection first                this.disconnect();            }try            {                controlSocket = new Socket(hostname, telnetPort);                controlSocket.setSoTimeout(5000);                controlInputStream  = new BufferedReader(new InputStreamReader(controlSocket.getInputStream()));                controlOutputStream = new BufferedWriter(new OutputStreamWriter(controlSocket.getOutputStream()));                State = CONNECTED;                isSuccessful = true;                banner = getMoreInput();                            } catch (UnknownHostException e) {                if( logging != null )                    logging.writeLog( LoggingType.INFORMATION, "Problem connecting - host unknown." );            } catch (IOException e) {                if( logging != null )                    logging.writeLog( LoggingType.INFORMATION, "Problem connecting - IO problem." );            }return isSuccessful;    }/*     *  Method:     getMoreInput()     *  Purpose:    gets data from connection but limits the buffer and prevents hanging     *  Parameters: None     *  Return:     returns the string from of what was read     *  Exception:  throws ConstantInputConnectionNotExpected when reads more data than     *              MAX_BUFFER_SIZE     */    public String getMoreInput() throws com.exceptions.ConstantInputConnectionNotExpected    {        int count = 0;        int ifCount = 0;        int charRead;        StringBuffer buffer = new StringBuffer();                try        {            while( (count < MAX_BUFFER_SIZE) && (ifCount < 10) )            {                charRead = (int) controlInputStream.read();                if ( charRead == 255 )                {                    handleOption( controlInputStream,controlOutputStream );                     ifCount++;                }                else                {                    buffer.append( (char) charRead );                    count++;                    ifCount = 0;                }            }        }        catch( IOException e )        {            //  occurs when we are done reading or some other IO error        }                //  if banner is too large        if( count >= MAX_BUFFER_SIZE )        {            State = UNEXPECTED_STREAM;            disconnect();            throw new ConstantInputConnectionNotExpected();        }                return buffer.toString();    }//The part of the code that "talks" with the telnet session    private void handleOption(BufferedReader myReader, BufferedWriter myWriter)    {        int command=0, option=0;try        {            command = (int)myReader.read();            option = (int)myReader.read();            switch (command)            {                case 251:                case 252:                    myWriter.write(255);                    myWriter.write(254);                    myWriter.write(option);                    break;                case 253:                case 254:                    myWriter.write(255);                    myWriter.write(252);                    myWriter.write(option);                    break;                default:                    break;            }            myWriter.flush();        } catch( Exception e )        {            if( logging != null )                logging.writeLog( LoggingType.ERROR, "TelnetModule: Error.", e );        }    }Thanks (agian),Landon Manningyandonman@hotmail.com


Related Links

How to find out cgi variables in java?
Socket connections on LAN?
ServerSocket port and the Socket port
let's discuss my application architecture,and give me your opinions
Hi Help me
Raw Socket In Java
networking and threading!
How can my program tunnel
downloading a file from the internet
Voice Chat Open Source
Voice Recording
Layer 3 communication
which more performant
but I don't want to read from the URL!
send email with java.net.URL
ServerSocket.accept(); problem