View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.discard;
19  
20  import java.io.IOException;
21  import java.net.DatagramPacket;
22  import java.net.InetAddress;
23  
24  import org.apache.commons.net.DatagramSocketClient;
25  
26  /***
27   * The DiscardUDPClient class is a UDP implementation of a client for the
28   * Discard protocol described in RFC 863.  To use the class,
29   * just open a local UDP port
30   * with {@link org.apache.commons.net.DatagramSocketClient#open  open }
31   * and call {@link #send  send } to send datagrams to the server
32   * After you're done sending discard data, call
33   * {@link org.apache.commons.net.DatagramSocketClient#close  close() }
34   * to clean up properly.
35   *
36   * @see DiscardTCPClient
37   ***/
38  
39  public class DiscardUDPClient extends DatagramSocketClient
40  {
41      /*** The default discard port.  It is set to 9 according to RFC 863. ***/
42      public static final int DEFAULT_PORT = 9;
43  
44      DatagramPacket _sendPacket;
45  
46      public DiscardUDPClient()
47      {
48          _sendPacket = new DatagramPacket(new byte[0], 0);
49      }
50  
51  
52      /***
53       * Sends the specified data to the specified server at the specified port.
54       *
55       * @param data  The discard data to send.
56       * @param length  The length of the data to send.  Should be less than
57       *    or equal to the length of the data byte array.
58       * @param host  The address of the server.
59       * @param port  The service port.
60       * @exception IOException If an error occurs during the datagram send
61       *            operation.
62       ***/
63      public void send(byte[] data, int length, InetAddress host, int port)
64      throws IOException
65      {
66          _sendPacket.setData(data);
67          _sendPacket.setLength(length);
68          _sendPacket.setAddress(host);
69          _sendPacket.setPort(port);
70          _socket_.send(_sendPacket);
71      }
72  
73  
74      /***
75       * Same as
76       * <code>send(data, length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
77       * @param data the buffer to send
78       * @param length the length of the data in the buffer
79       * @param host the target host
80       * @see #send(byte[], int, InetAddress, int)
81       * @throws IOException if an error occurs
82       ***/
83      public void send(byte[] data, int length, InetAddress host)
84      throws IOException
85      {
86          send(data, length, host, DEFAULT_PORT);
87      }
88  
89  
90      /***
91       * Same as
92       * <code>send(data, data.length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
93       * @param data the buffer to send
94       * @param host the target host
95       * @see #send(byte[], int, InetAddress, int)
96       * @throws IOException if an error occurs
97       ***/
98      public void send(byte[] data, InetAddress host) throws IOException
99      {
100         send(data, data.length, host, DEFAULT_PORT);
101     }
102 
103 }
104