Coverage report

  %line %branch
examples.chargen
0% 
0% 

 1  
 /*
 2  
  * Copyright 2001-2005 The Apache Software Foundation
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package examples;
 17  
 
 18  
 import java.io.BufferedReader;
 19  
 import java.io.IOException;
 20  
 import java.io.InputStreamReader;
 21  
 import java.io.InterruptedIOException;
 22  
 import java.net.InetAddress;
 23  
 import java.net.SocketException;
 24  
 import org.apache.commons.net.CharGenTCPClient;
 25  
 import org.apache.commons.net.CharGenUDPClient;
 26  
 
 27  
 /***
 28  
  * This is an example program demonstrating how to use the CharGenTCPClient
 29  
  * and CharGenUDPClient classes.  This program connects to the default
 30  
  * chargen service port of a specified server, then reads 100 lines from
 31  
  * of generated output, writing each line to standard output, and then
 32  
  * closes the connection.  The UDP invocation of the program sends 50
 33  
  * datagrams, printing the reply to each.
 34  
  * The default is to use the TCP port.  Use the -udp flag to use the UDP
 35  
  * port.
 36  
  * <p>
 37  
  * Usage: chargen [-udp] <hostname>
 38  
  * <p>
 39  
  ***/
 40  0
 public final class chargen
 41  
 {
 42  
 
 43  
     public static final void chargenTCP(String host) throws IOException
 44  
     {
 45  0
         int lines = 100;
 46  
         String line;
 47  0
         CharGenTCPClient client = new CharGenTCPClient();
 48  
         BufferedReader chargenInput;
 49  
 
 50  
         // We want to timeout if a response takes longer than 60 seconds
 51  0
         client.setDefaultTimeout(60000);
 52  0
         client.connect(host);
 53  0
         chargenInput =
 54  
             new BufferedReader(class="keyword">new InputStreamReader(client.getInputStream()));
 55  
 
 56  
         // We assume the chargen service outputs lines, but it really doesn't
 57  
         // have to, so this code might actually not work if no newlines are
 58  
         // present.
 59  0
         while (lines-- > 0)
 60  
         {
 61  0
             if ((line = chargenInput.readLine()) == null)
 62  0
                 break;
 63  0
             System.out.println(line);
 64  
         }
 65  
 
 66  0
         client.disconnect();
 67  0
     }
 68  
 
 69  
     public static final void chargenUDP(String host) throws IOException
 70  
     {
 71  0
         int packets = 50;
 72  
         byte[] data;
 73  
         InetAddress address;
 74  
         CharGenUDPClient client;
 75  
 
 76  0
         address = InetAddress.getByName(host);
 77  0
         client = new CharGenUDPClient();
 78  
 
 79  0
         client.open();
 80  
         // If we don't receive a return packet within 5 seconds, assume
 81  
         // the packet is lost.
 82  0
         client.setSoTimeout(5000);
 83  
 
 84  0
         while (packets-- > 0)
 85  
         {
 86  0
             client.send(address);
 87  
 
 88  
             try
 89  
             {
 90  0
                 data = client.receive();
 91  
             }
 92  
             // Here we catch both SocketException and InterruptedIOException,
 93  
             // because even though the JDK 1.1 docs claim that
 94  
             // InterruptedIOException is thrown on a timeout, it seems
 95  
             // SocketException is also thrown.
 96  0
             catch (SocketException e)
 97  
             {
 98  
                 // We timed out and assume the packet is lost.
 99  0
                 System.err.println("SocketException: Timed out and dropped packet");
 100  0
                 continue;
 101  
             }
 102  0
             catch (InterruptedIOException e)
 103  
             {
 104  
                 // We timed out and assume the packet is lost.
 105  0
                 System.err.println(
 106  
                     "InterruptedIOException: Timed out and dropped packet");
 107  0
                 continue;
 108  0
             }
 109  0
             System.out.write(data);
 110  0
             System.out.flush();
 111  
         }
 112  
 
 113  0
         client.close();
 114  0
     }
 115  
 
 116  
 
 117  
     public static final void main(String[] args)
 118  
     {
 119  
 
 120  0
         if (args.length == 1)
 121  
         {
 122  
             try
 123  
             {
 124  0
                 chargenTCP(args[0]);
 125  
             }
 126  0
             catch (IOException e)
 127  
             {
 128  0
                 e.printStackTrace();
 129  0
                 System.exit(1);
 130  0
             }
 131  
         }
 132  0
         else if (args.length == 2 && args[0].equals("-udp"))
 133  
         {
 134  
             try
 135  
             {
 136  0
                 chargenUDP(args[1]);
 137  
             }
 138  0
             catch (IOException e)
 139  
             {
 140  0
                 e.printStackTrace();
 141  0
                 System.exit(1);
 142  0
             }
 143  
         }
 144  
         else
 145  
         {
 146  0
             System.err.println("Usage: chargen [-udp] <hostname>");
 147  0
             System.exit(1);
 148  
         }
 149  
 
 150  0
     }
 151  
 
 152  
 }
 153  
 

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.