Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/act365/net/echo/DatagramEchoServer.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   DatagramEchoServer acts as the server for the UDP/IP echo service.
36  */
37  
38  public class DatagramEchoServer {
39  
40    public static void main( String[] args ){
41      
42      int i    = -1 ,
43          port = 0 ,
44          maxDatagramLength = 512 ;
45  
46      while( ++ i < args.length ){
47        if( args[ i ].equals("-p") && i < args.length - 1 ){
48          try {
49            port = Integer.parseInt( args[ ++ i ] );
50          } catch( NumberFormatException e ){
51            System.err.println("Invalid port number");
52            System.exit( 1 );
53          }
54        } else if( args[ i ].equals("-l") && i < args.length - 1 ){
55          try {
56            maxDatagramLength = Integer.parseInt( args[ ++ i ] );
57          } catch( NumberFormatException e ){
58            System.err.println("Invalid datagram length");
59            System.exit( 2 );
60          }
61        } else {
62          System.err.println("DatagramEchoServer -p port -l datagramlength");
63          System.err.println("Use java -Dimpl.prefix in order to specify socket type e.g. java -Dimpl.prefix=UDP");
64          System.exit( 2 );
65        }
66      }
67  
68      new SocketWrenchSession();
69  
70      new DatagramEchoServer( maxDatagramLength , port );
71    }
72  
73    DatagramEchoServer( int maxDatagramLength ,
74                        int port ){
75  
76      try {
77  
78        DatagramSocket server = new DatagramSocket( port , InetAddress.getByName( null ) );
79    
80        System.err.println("Local port: " + server.getLocalPort() );
81  
82        int bytesRead ;
83  
84        byte[] buffer = new byte[ maxDatagramLength ];
85  
86        DatagramPacket received = null ;
87  
88        while( true ){
89  
90          received = new DatagramPacket( buffer , maxDatagramLength );
91  
92          server.receive( received );
93  
94          server.send( new DatagramPacket( received.getData() , 
95                                           received.getLength() , 
96                                           received.getAddress() ,
97                                           received.getPort() ) );
98        }
99  
100     } catch ( SocketException se ) {
101       System.err.println( se.getMessage() );
102       System.exit( 4 );
103     } catch ( IOException ioe ) {
104       System.err.println( ioe.getMessage() );
105       System.exit( 5 );
106     } catch( Exception e ){
107       System.err.println( e.getMessage() );
108       System.exit( 6 );
109     }
110   }
111 }