Source Code Cross Referenced for TelnetClientExample.java in  » Net » Apache-commons-net-1.4.1 » examples » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Apache commons net 1.4.1 » examples 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2003-2004 The Apache Software Foundation
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package examples;
017:
018:        import java.io.InputStream;
019:        import java.io.OutputStream;
020:        import java.io.FileOutputStream;
021:        import java.io.IOException;
022:        import org.apache.commons.net.telnet.TelnetClient;
023:        import org.apache.commons.net.telnet.TelnetNotificationHandler;
024:        import org.apache.commons.net.telnet.SimpleOptionHandler;
025:        import org.apache.commons.net.telnet.EchoOptionHandler;
026:        import org.apache.commons.net.telnet.TerminalTypeOptionHandler;
027:        import org.apache.commons.net.telnet.SuppressGAOptionHandler;
028:        import org.apache.commons.net.telnet.InvalidTelnetOptionException;
029:        import java.util.StringTokenizer;
030:
031:        /***
032:         * This is a simple example of use of TelnetClient.
033:         * An external option handler (SimpleTelnetOptionHandler) is used.
034:         * Initial configuration requested by TelnetClient will be:
035:         * WILL ECHO, WILL SUPPRESS-GA, DO SUPPRESS-GA.
036:         * VT100 terminal type will be subnegotiated.
037:         * <p>
038:         * Also, use of the sendAYT(), getLocalOptionState(), getRemoteOptionState()
039:         * is demonstrated.
040:         * When connected, type AYT to send an AYT command to the server and see
041:         * the result.
042:         * Type OPT to see a report of the state of the first 25 options.
043:         * <p>
044:         * @author Bruno D'Avanzo
045:         ***/
046:        public class TelnetClientExample implements  Runnable,
047:                TelnetNotificationHandler {
048:            static TelnetClient tc = null;
049:
050:            /***
051:             * Main for the TelnetClientExample.
052:             ***/
053:            public static void main(String[] args) throws IOException {
054:                FileOutputStream fout = null;
055:
056:                if (args.length < 1) {
057:                    System.err
058:                            .println("Usage: TelnetClientExample1 <remote-ip> [<remote-port>]");
059:                    System.exit(1);
060:                }
061:
062:                String remoteip = args[0];
063:
064:                int remoteport;
065:
066:                if (args.length > 1) {
067:                    remoteport = (new Integer(args[1])).intValue();
068:                } else {
069:                    remoteport = 23;
070:                }
071:
072:                try {
073:                    fout = new FileOutputStream("spy.log", true);
074:                } catch (Exception e) {
075:                    System.err.println("Exception while opening the spy file: "
076:                            + e.getMessage());
077:                }
078:
079:                tc = new TelnetClient();
080:
081:                TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler(
082:                        "VT100", false, false, true, false);
083:                EchoOptionHandler echoopt = new EchoOptionHandler(true, false,
084:                        true, false);
085:                SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(
086:                        true, true, true, true);
087:
088:                try {
089:                    tc.addOptionHandler(ttopt);
090:                    tc.addOptionHandler(echoopt);
091:                    tc.addOptionHandler(gaopt);
092:                } catch (InvalidTelnetOptionException e) {
093:                    System.err.println("Error registering option handlers: "
094:                            + e.getMessage());
095:                }
096:
097:                while (true) {
098:                    boolean end_loop = false;
099:                    try {
100:                        tc.connect(remoteip, remoteport);
101:
102:                        Thread reader = new Thread(new TelnetClientExample());
103:                        tc.registerNotifHandler(new TelnetClientExample());
104:                        System.out.println("TelnetClientExample");
105:                        System.out
106:                                .println("Type AYT to send an AYT telnet command");
107:                        System.out
108:                                .println("Type OPT to print a report of status of options (0-24)");
109:                        System.out
110:                                .println("Type REGISTER to register a new SimpleOptionHandler");
111:                        System.out
112:                                .println("Type UNREGISTER to unregister an OptionHandler");
113:                        System.out
114:                                .println("Type SPY to register the spy (connect to port 3333 to spy)");
115:                        System.out
116:                                .println("Type UNSPY to stop spying the connection");
117:
118:                        reader.start();
119:                        OutputStream outstr = tc.getOutputStream();
120:
121:                        byte[] buff = new byte[1024];
122:                        int ret_read = 0;
123:
124:                        do {
125:                            try {
126:                                ret_read = System.in.read(buff);
127:                                if (ret_read > 0) {
128:                                    if ((new String(buff, 0, ret_read))
129:                                            .startsWith("AYT")) {
130:                                        try {
131:                                            System.out.println("Sending AYT");
132:
133:                                            System.out.println("AYT response:"
134:                                                    + tc.sendAYT(5000));
135:                                        } catch (Exception e) {
136:                                            System.err
137:                                                    .println("Exception waiting AYT response: "
138:                                                            + e.getMessage());
139:                                        }
140:                                    } else if ((new String(buff, 0, ret_read))
141:                                            .startsWith("OPT")) {
142:                                        System.out
143:                                                .println("Status of options:");
144:                                        for (int ii = 0; ii < 25; ii++)
145:                                            System.out
146:                                                    .println("Local Option "
147:                                                            + ii
148:                                                            + ":"
149:                                                            + tc
150:                                                                    .getLocalOptionState(ii)
151:                                                            + " Remote Option "
152:                                                            + ii
153:                                                            + ":"
154:                                                            + tc
155:                                                                    .getRemoteOptionState(ii));
156:                                    } else if ((new String(buff, 0, ret_read))
157:                                            .startsWith("REGISTER")) {
158:                                        StringTokenizer st = new StringTokenizer(
159:                                                new String(buff));
160:                                        try {
161:                                            st.nextToken();
162:                                            int opcode = (new Integer(st
163:                                                    .nextToken())).intValue();
164:                                            boolean initlocal = (new Boolean(st
165:                                                    .nextToken()))
166:                                                    .booleanValue();
167:                                            boolean initremote = (new Boolean(
168:                                                    st.nextToken()))
169:                                                    .booleanValue();
170:                                            boolean acceptlocal = (new Boolean(
171:                                                    st.nextToken()))
172:                                                    .booleanValue();
173:                                            boolean acceptremote = (new Boolean(
174:                                                    st.nextToken()))
175:                                                    .booleanValue();
176:                                            SimpleOptionHandler opthand = new SimpleOptionHandler(
177:                                                    opcode, initlocal,
178:                                                    initremote, acceptlocal,
179:                                                    acceptremote);
180:                                            tc.addOptionHandler(opthand);
181:                                        } catch (Exception e) {
182:                                            if (e instanceof  InvalidTelnetOptionException) {
183:                                                System.err
184:                                                        .println("Error registering option: "
185:                                                                + e
186:                                                                        .getMessage());
187:                                            } else {
188:                                                System.err
189:                                                        .println("Invalid REGISTER command.");
190:                                                System.err
191:                                                        .println("Use REGISTER optcode initlocal initremote acceptlocal acceptremote");
192:                                                System.err
193:                                                        .println("(optcode is an integer.)");
194:                                                System.err
195:                                                        .println("(initlocal, initremote, acceptlocal, acceptremote are boolean)");
196:                                            }
197:                                        }
198:                                    } else if ((new String(buff, 0, ret_read))
199:                                            .startsWith("UNREGISTER")) {
200:                                        StringTokenizer st = new StringTokenizer(
201:                                                new String(buff));
202:                                        try {
203:                                            st.nextToken();
204:                                            int opcode = (new Integer(st
205:                                                    .nextToken())).intValue();
206:                                            tc.deleteOptionHandler(opcode);
207:                                        } catch (Exception e) {
208:                                            if (e instanceof  InvalidTelnetOptionException) {
209:                                                System.err
210:                                                        .println("Error unregistering option: "
211:                                                                + e
212:                                                                        .getMessage());
213:                                            } else {
214:                                                System.err
215:                                                        .println("Invalid UNREGISTER command.");
216:                                                System.err
217:                                                        .println("Use UNREGISTER optcode");
218:                                                System.err
219:                                                        .println("(optcode is an integer)");
220:                                            }
221:                                        }
222:                                    } else if ((new String(buff, 0, ret_read))
223:                                            .startsWith("SPY")) {
224:                                        try {
225:                                            tc.registerSpyStream(fout);
226:                                        } catch (Exception e) {
227:                                            System.err
228:                                                    .println("Error registering the spy");
229:                                        }
230:                                    } else if ((new String(buff, 0, ret_read))
231:                                            .startsWith("UNSPY")) {
232:                                        tc.stopSpyStream();
233:                                    } else {
234:                                        try {
235:                                            outstr.write(buff, 0, ret_read);
236:                                            outstr.flush();
237:                                        } catch (Exception e) {
238:                                            end_loop = true;
239:                                        }
240:                                    }
241:                                }
242:                            } catch (Exception e) {
243:                                System.err
244:                                        .println("Exception while reading keyboard:"
245:                                                + e.getMessage());
246:                                end_loop = true;
247:                            }
248:                        } while ((ret_read > 0) && (end_loop == false));
249:
250:                        try {
251:                            tc.disconnect();
252:                        } catch (Exception e) {
253:                            System.err.println("Exception while connecting:"
254:                                    + e.getMessage());
255:                        }
256:                    } catch (Exception e) {
257:                        System.err.println("Exception while connecting:"
258:                                + e.getMessage());
259:                        System.exit(1);
260:                    }
261:                }
262:            }
263:
264:            /***
265:             * Callback method called when TelnetClient receives an option
266:             * negotiation command.
267:             * <p>
268:             * @param negotiation_code - type of negotiation command received
269:             * (RECEIVED_DO, RECEIVED_DONT, RECEIVED_WILL, RECEIVED_WONT)
270:             * <p>
271:             * @param option_code - code of the option negotiated
272:             * <p>
273:             ***/
274:            public void receivedNegotiation(int negotiation_code,
275:                    int option_code) {
276:                String command = null;
277:                if (negotiation_code == TelnetNotificationHandler.RECEIVED_DO) {
278:                    command = "DO";
279:                } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_DONT) {
280:                    command = "DONT";
281:                } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WILL) {
282:                    command = "WILL";
283:                } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WONT) {
284:                    command = "WONT";
285:                }
286:                System.out.println("Received " + command + " for option code "
287:                        + option_code);
288:            }
289:
290:            /***
291:             * Reader thread.
292:             * Reads lines from the TelnetClient and echoes them
293:             * on the screen.
294:             ***/
295:            public void run() {
296:                InputStream instr = tc.getInputStream();
297:
298:                try {
299:                    byte[] buff = new byte[1024];
300:                    int ret_read = 0;
301:
302:                    do {
303:                        ret_read = instr.read(buff);
304:                        if (ret_read > 0) {
305:                            System.out.print(new String(buff, 0, ret_read));
306:                        }
307:                    } while (ret_read >= 0);
308:                } catch (Exception e) {
309:                    System.err.println("Exception while reading socket:"
310:                            + e.getMessage());
311:                }
312:
313:                try {
314:                    tc.disconnect();
315:                } catch (Exception e) {
316:                    System.err.println("Exception while closing telnet:"
317:                            + e.getMessage());
318:                }
319:            }
320:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.