View Javadoc

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 org.apache.commons.net;
17  
18  import java.io.IOException;
19  import java.net.DatagramPacket;
20  import java.net.InetAddress;
21  
22  /***
23   * The DiscardUDPClient class is a UDP implementation of a client for the
24   * Discard protocol described in RFC 863.  To use the class,
25   * just open a local UDP port
26   * with {@link org.apache.commons.net.DatagramSocketClient#open  open }
27   * and call {@link #send  send } to send datagrams to the server
28   * After you're done sending discard data, call
29   * {@link org.apache.commons.net.DatagramSocketClient#close  close() }
30   * to clean up properly.
31   * <p>
32   * <p>
33   * @author Daniel F. Savarese
34   * @see DiscardTCPClient
35   ***/
36  
37  public class DiscardUDPClient extends DatagramSocketClient
38  {
39      /*** The default discard port.  It is set to 9 according to RFC 863. ***/
40      public static final int DEFAULT_PORT = 9;
41  
42      DatagramPacket _sendPacket;
43  
44      public DiscardUDPClient()
45      {
46          _sendPacket = new DatagramPacket(new byte[0], 0);
47      }
48  
49  
50      /***
51       * Sends the specified data to the specified server at the specified port.
52       * <p>
53       * @param data  The discard data to send.
54       * @param length  The length of the data to send.  Should be less than
55       *    or equal to the length of the data byte array.
56       * @param host  The address of the server.
57       * @param port  The service port.
58       * @exception IOException If an error occurs during the datagram send
59       *            operation.
60       ***/
61      public void send(byte[] data, int length, InetAddress host, int port)
62      throws IOException
63      {
64          _sendPacket.setData(data);
65          _sendPacket.setLength(length);
66          _sendPacket.setAddress(host);
67          _sendPacket.setPort(port);
68          _socket_.send(_sendPacket);
69      }
70  
71  
72      /***
73       * Same as
74       * <code>send(data, length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
75       ***/
76      public void send(byte[] data, int length, InetAddress host)
77      throws IOException
78      {
79          send(data, length, host, DEFAULT_PORT);
80      }
81  
82  
83      /***
84       * Same as
85       * <code>send(data, data.length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
86       ***/
87      public void send(byte[] data, InetAddress host) throws IOException
88      {
89          send(data, data.length, host, DEFAULT_PORT);
90      }
91  
92  }
93