Source code: com/act365/net/echo/EchoClient.java
1 /*
2 * JSocket Wrench
3 *
4 * Copyright (C) act365.com October 2003
5 *
6 * Web site: http://www.act365.com/wrench
7 * E-mail: developers@act365.com
8 *
9 * The JSocket Wrench library adds support for low-level Internet protocols
10 * to the Java programming language.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
20 * Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27 package com.act365.net.echo ;
28
29 import com.act365.net.* ;
30
31 import java.io.*;
32 import java.net.*;
33
34 /**
35 EchoClient acts as the client for the TCP echo service.
36 Usage: <code>EchoClient -p protocol -i inputfile -o outputfile -l localhost localport hostname hostport</code>.
37 <p><code>-p protocol</code> (optional) defines the socket protocol to be used.
38 By default, the JDK TCP implementation will be used. The alternatives are
39 TCPJ, RawTCP or RawTCPJ. (NB The RawTCP protocol will behave identically to TCP
40 because no <code>DatagramSocket</code> objects will be instantiated. However, a
41 <code>DatagramSocket</code> is always used by TCPJ, so the behaviour of TCPJ
42 and RawTCPJ will differ).
43 <p><code>-i inputfile</code> (optional) defines the file from which input will
44 be read. By default, standard input will be used.
45 <p><code>-o outfile</code> (optional) defines the file to which all output will
46 be written. By default, standard output will be used.
47 <p><code>-l localhost localport</code> (optional) should be specified if the protocol
48 has been set to RawTCPJ. The information will be used to construct the IP header.
49 <p><code>hostname port</code> define the remote echo server.
50 */
51
52 class EchoClient {
53
54 public static void main( String[] args ){
55
56 final String errortext = "EchoClient -p protocol -i inputfile -o outputfile -l localhost localport hostname port";
57
58 if( args.length < 2 ){
59 System.err.println( errortext );
60 System.exit( 1 );
61 }
62
63 int i = -1 ,
64 port = 0 ,
65 localport = 0 ;
66
67 String hostname = args[ args.length - 2 ],
68 protocollabel = "" ,
69 localhost = null ,
70 inputFile = null ,
71 outputFile = null ;
72
73 try {
74 port = Integer.parseInt( args[ args.length - 1 ] );
75 } catch( NumberFormatException e ){
76 System.err.println("Invalid port number");
77 System.exit( 3 );
78 }
79
80 while( ++ i < args.length - 2 ){
81 if( args[ i ].equals("-i") && i < args.length - 3 ){
82 inputFile = args[ ++ i ];
83 } else if( args[ i ].equals("-o") && i < args.length - 3 ){
84 outputFile = args[ ++ i ];
85 } else if( args[ i ].equals("-p") && i < args.length - 3 ){
86 protocollabel = args[ ++ i ];
87 if( ! protocollabel.equalsIgnoreCase("TCP") &&
88 ! protocollabel.equalsIgnoreCase("TCPJ") &&
89 ! protocollabel.equalsIgnoreCase("RawTCP") &&
90 ! protocollabel.equalsIgnoreCase("RawTCPJ") ){
91 System.err.println("Unsupported protocol");
92 System.exit( 2 );
93 }
94 } else if( args[ i ].equals("-l") && i < args.length - 4 ){
95 localhost = args[ ++ i ];
96 try {
97 localport = Integer.parseInt( args[ ++ i ] );
98 } catch( NumberFormatException e ){
99 System.err.println("Invalid localport number");
100 System.exit( 3 );
101 }
102 } else {
103 System.err.println( errortext );
104 System.exit( 1 );
105 }
106 }
107
108 try {
109 SocketUtils.setProtocol( protocollabel );
110 } catch ( java.io.IOException e ) {
111 System.err.println("Unsupported protocol");
112 System.exit( 2 );
113 }
114
115 final int protocol = SocketUtils.getProtocol();
116
117 boolean includeheader = SocketUtils.includeHeader();
118
119 new SocketWrenchSession();
120
121 InetAddress dstaddr = null ,
122 localaddr = null ;
123
124 try {
125 dstaddr = InetAddress.getByName( hostname );
126 if( localhost instanceof String ){
127 localaddr = InetAddress.getByName( localhost );
128 }
129 } catch( UnknownHostException e ){
130 System.err.println("Address " + e.getMessage() + " is unknown");
131 System.exit( 4 );
132 }
133
134 Socket client = null ;
135
136 try {
137 if( localaddr instanceof InetAddress ){
138 client = new Socket( dstaddr , port , localaddr , localport );
139 } else {
140 client = new Socket( dstaddr , port );
141 }
142 } catch( UnknownHostException e ){
143 System.err.println( e.getMessage() );
144 System.exit( 5 );
145 } catch( IOException e ){
146 System.err.println( e.getMessage() );
147 System.exit( 6 );
148 }
149
150 InputStream localIn = null ,
151 serverIn = null ;
152
153 OutputStream localOut = null ,
154 serverOut = null ;
155
156 if( inputFile instanceof String ){
157 try {
158 localIn = new FileInputStream( inputFile );
159 } catch ( FileNotFoundException e ) {
160 System.err.println( e.getMessage() );
161 System.exit( 7 );
162 }
163 } else {
164 localIn = System.in ;
165 }
166
167 try {
168 serverIn = client.getInputStream();
169 } catch( IOException e ){
170 System.err.println( e.getMessage() );
171 System.exit( 8 );
172 }
173
174 if( outputFile instanceof String ){
175 try {
176 localOut = new FileOutputStream( outputFile );
177 } catch ( IOException e ) {
178 System.err.println( e.getMessage() );
179 System.exit( 9 );
180 }
181 } else {
182 localOut = System.out ;
183 }
184
185 try {
186 serverOut = client.getOutputStream();
187 } catch( IOException e ){
188 System.err.println( e.getMessage() );
189 System.exit( 10 );
190 }
191
192 new EchoClient( new InputStreamReader( localIn ) ,
193 new OutputStreamWriter( serverOut ),
194 serverIn ,
195 new OutputStreamWriter( localOut ) );
196
197 System.exit( 0 );
198 }
199
200 public EchoClient( InputStreamReader localIn ,
201 OutputStreamWriter serverOut ,
202 InputStream serverIn ,
203 OutputStreamWriter localOut ){
204
205 try {
206
207 BufferedReader localReader = new BufferedReader( localIn );
208
209 String line ;
210
211 StringBuffer buffer = new StringBuffer();
212
213 int ch ;
214
215 while( ( line = localReader.readLine() ) != null ){
216
217 serverOut.write( line );
218 serverOut.write( '\n' );
219 serverOut.flush();
220
221 ch = serverIn.read();
222
223 while( ch > -1 ){
224 if( ch != '\n' ){
225 buffer.append((char) ch );
226 } else {
227 localOut.write( buffer.toString() );
228 localOut.write( '\n' );
229 localOut.flush();
230
231 buffer = new StringBuffer();
232
233 break;
234 }
235 ch = serverIn.read();
236 }
237 }
238
239 localReader.close();
240 serverIn.close();
241
242 } catch( Exception e ){
243 System.err.println( e.getMessage() );
244 }
245 }
246 }
247
248