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