Source code: com/act365/net/echo/DatagramEchoClient.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 DatagramEchoClient acts as the client for the UDP/IP echo service.
36 */
37
38 class DatagramEchoClient {
39
40 public static void main( String[] args ){
41
42 int i = -1 ,
43 port = 0 ,
44 localport = 0 ,
45 maxDatagramLength = 512 ;
46
47 String hostname = "localhost",
48 inputFile = null ,
49 outputFile = null ;
50
51 while( ++ i < args.length ){
52 if( args[ i ].equals("-p") && i < args.length - 1 ){
53 try {
54 port = Integer.parseInt( args[ ++ i ] );
55 } catch( NumberFormatException e ){
56 System.err.println("Invalid port number");
57 System.exit( 1 );
58 }
59 } else if( args[ i ].equals("-lp") && i < args.length - 1 ){
60 try {
61 localport = Integer.parseInt( args[ ++ i ] );
62 } catch( NumberFormatException e ){
63 System.err.println("Invalid localport number");
64 System.exit( 2 );
65 }
66 } else if( args[ i ].equals("-l") && i < args.length - 1 ){
67 try {
68 maxDatagramLength = Integer.parseInt( args[ ++ i ] );
69 } catch( NumberFormatException e ){
70 System.err.println("Invalid datagram length");
71 System.exit( 3 );
72 }
73 } else if( args[ i ].equals("-i") && i < args.length - 1 ){
74 inputFile = args[ ++ i ];
75 } else if( args[ i ].equals("-o") && i < args.length - 1 ){
76 outputFile = args[ ++ i ];
77 } else if( args[ i ].equals("-h") && i < args.length - 1 ){
78 hostname = args[ ++ i ];
79 } else {
80 System.err.println("DatagramEchoClient -p port -lp localport -l datagramlength -i inputfile -o outputfile -h hostname");
81 System.err.println("Use java -Dimpl.prefix in order to specify socket type e.g. java -Dimpl.prefix=UDP");
82 System.exit( 4 );
83 }
84 }
85
86 InetAddress dest = null ;
87
88 try {
89 dest = InetAddress.getByName( hostname );
90 } catch ( UnknownHostException e ) {
91 System.err.println( e.getMessage() );
92 System.exit( 5 );
93 }
94
95 new SocketWrenchSession();
96
97 DatagramSocket socket = null ;
98
99 try {
100 socket = new DatagramSocket( localport , InetAddress.getByName( null ) );
101 } catch( SocketException se ){
102 System.err.println( se.getMessage() );
103 System.exit( 6 );
104 } catch( UnknownHostException uhe ){
105 System.err.println( uhe.getMessage() );
106 System.exit( 7 );
107 }
108
109 System.err.println("Local port: " + socket.getLocalPort() );
110
111 InputStream localIn = null ;
112
113 OutputStream localOut = null ;
114
115 if( inputFile instanceof String ){
116 try {
117 localIn = new FileInputStream( inputFile );
118 } catch ( FileNotFoundException e ) {
119 System.err.println( e.getMessage() );
120 System.exit( 8 );
121 }
122 } else {
123 localIn = System.in ;
124 }
125
126 if( outputFile instanceof String ){
127 try {
128 localOut = new FileOutputStream( outputFile );
129 } catch ( IOException e ) {
130 System.err.println( e.getMessage() );
131 System.exit( 9 );
132 }
133 } else {
134 localOut = System.out ;
135 }
136
137 new DatagramEchoClient( socket , maxDatagramLength , dest , port , localIn , localOut );
138
139 System.exit( 0 );
140 }
141
142 public DatagramEchoClient( DatagramSocket socket ,
143 int maxDatagramLength ,
144 InetAddress dest ,
145 int port ,
146 InputStream localIn ,
147 OutputStream localOut ){
148 try {
149
150 int bytesRead ;
151
152 byte[] buffer = new byte[ maxDatagramLength ];
153
154 while( ( bytesRead = localIn.read( buffer ) ) > -1 ){
155
156 socket.send( new DatagramPacket( buffer , bytesRead , dest , port ) );
157
158 DatagramPacket received = new DatagramPacket( buffer , maxDatagramLength );
159
160 socket.receive( received );
161
162 localOut.write( received.getData() , 0 , received.getLength() );
163 }
164
165 } catch( IOException e ){
166 System.err.println( e.getMessage() );
167 }
168 }
169 }
170
171