Source Code Cross Referenced for echo.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 2001-2005 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.BufferedReader;
019:        import java.io.IOException;
020:        import java.io.InputStreamReader;
021:        import java.io.InterruptedIOException;
022:        import java.io.OutputStreamWriter;
023:        import java.io.PrintWriter;
024:        import java.net.InetAddress;
025:        import java.net.SocketException;
026:        import org.apache.commons.net.EchoTCPClient;
027:        import org.apache.commons.net.EchoUDPClient;
028:
029:        /***
030:         * This is an example program demonstrating how to use the EchoTCPClient
031:         * and EchoUDPClient classes.  This program connects to the default echo
032:         * service port of a specified server, then reads lines from standard
033:         * input, writing them to the echo server, and then printing the echo.
034:         * The default is to use the TCP port.  Use the -udp flag to use the UDP
035:         * port.
036:         * <p>
037:         * Usage: echo [-udp] <hostname>
038:         * <p>
039:         ***/
040:        public final class echo {
041:
042:            public static final void echoTCP(String host) throws IOException {
043:                EchoTCPClient client = new EchoTCPClient();
044:                BufferedReader input, echoInput;
045:                PrintWriter echoOutput;
046:                String line;
047:
048:                // We want to timeout if a response takes longer than 60 seconds
049:                client.setDefaultTimeout(60000);
050:                client.connect(host);
051:                System.out.println("Connected to " + host + ".");
052:                input = new BufferedReader(new InputStreamReader(System.in));
053:                echoOutput = new PrintWriter(new OutputStreamWriter(client
054:                        .getOutputStream()), true);
055:                echoInput = new BufferedReader(new InputStreamReader(client
056:                        .getInputStream()));
057:
058:                while ((line = input.readLine()) != null) {
059:                    echoOutput.println(line);
060:                    System.out.println(echoInput.readLine());
061:                }
062:
063:                client.disconnect();
064:            }
065:
066:            public static final void echoUDP(String host) throws IOException {
067:                int length, count;
068:                byte[] data;
069:                String line;
070:                BufferedReader input;
071:                InetAddress address;
072:                EchoUDPClient client;
073:
074:                input = new BufferedReader(new InputStreamReader(System.in));
075:                address = InetAddress.getByName(host);
076:                client = new EchoUDPClient();
077:
078:                client.open();
079:                // If we don't receive an echo within 5 seconds, assume the packet is lost.
080:                client.setSoTimeout(5000);
081:                System.out.println("Ready to echo to " + host + ".");
082:
083:                // Remember, there are no guarantees about the ordering of returned
084:                // UDP packets, so there is a chance the output may be jumbled.
085:                while ((line = input.readLine()) != null) {
086:                    data = line.getBytes();
087:                    client.send(data, address);
088:                    count = 0;
089:                    do {
090:                        try {
091:                            length = client.receive(data);
092:                        }
093:                        // Here we catch both SocketException and InterruptedIOException,
094:                        // because even though the JDK 1.1 docs claim that
095:                        // InterruptedIOException is thrown on a timeout, it seems
096:                        // SocketException is also thrown.
097:                        catch (SocketException e) {
098:                            // We timed out and assume the packet is lost.
099:                            System.err
100:                                    .println("SocketException: Timed out and dropped packet");
101:                            break;
102:                        } catch (InterruptedIOException e) {
103:                            // We timed out and assume the packet is lost.
104:                            System.err
105:                                    .println("InterruptedIOException: Timed out and dropped packet");
106:                            break;
107:                        }
108:                        System.out.print(new String(data, 0, length));
109:                        count += length;
110:                    } while (count < data.length);
111:
112:                    System.out.println();
113:                }
114:
115:                client.close();
116:            }
117:
118:            public static final void main(String[] args) {
119:
120:                if (args.length == 1) {
121:                    try {
122:                        echoTCP(args[0]);
123:                    } catch (IOException e) {
124:                        e.printStackTrace();
125:                        System.exit(1);
126:                    }
127:                } else if (args.length == 2 && args[0].equals("-udp")) {
128:                    try {
129:                        echoUDP(args[1]);
130:                    } catch (IOException e) {
131:                        e.printStackTrace();
132:                        System.exit(1);
133:                    }
134:                } else {
135:                    System.err.println("Usage: echo [-udp] <hostname>");
136:                    System.exit(1);
137:                }
138:
139:            }
140:
141:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.