001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.net.discard; 019 020import java.io.IOException; 021import java.net.DatagramPacket; 022import java.net.InetAddress; 023 024import org.apache.commons.net.DatagramSocketClient; 025import org.apache.commons.net.util.NetConstants; 026 027/** 028 * The DiscardUDPClient class is a UDP implementation of a client for the Discard protocol described in RFC 863. To use the class, just open a local UDP port 029 * with {@link org.apache.commons.net.DatagramSocketClient#open open } and call {@link #send send } to send datagrams to the server After you're done sending 030 * discard data, call {@link org.apache.commons.net.DatagramSocketClient#close close() } to clean up properly. 031 * 032 * @see DiscardTCPClient 033 */ 034public class DiscardUDPClient extends DatagramSocketClient { 035 036 /** The default discard port. It is set to 9 according to RFC 863. */ 037 public static final int DEFAULT_PORT = 9; 038 039 private final DatagramPacket sendPacket; 040 041 public DiscardUDPClient() { 042 sendPacket = new DatagramPacket(NetConstants.EMPTY_BTYE_ARRAY, 0); 043 } 044 045 /** 046 * Same as <code>send(data, data.length, host. DiscardUDPClient.DEFAULT_PORT)</code>. 047 * 048 * @param data the buffer to send 049 * @param host the target host 050 * @see #send(byte[], int, InetAddress, int) 051 * @throws IOException if an error occurs 052 */ 053 public void send(final byte[] data, final InetAddress host) throws IOException { 054 send(data, data.length, host, DEFAULT_PORT); 055 } 056 057 /** 058 * Same as <code>send(data, length, host. DiscardUDPClient.DEFAULT_PORT)</code>. 059 * 060 * @param data the buffer to send 061 * @param length the length of the data in the buffer 062 * @param host the target host 063 * @see #send(byte[], int, InetAddress, int) 064 * @throws IOException if an error occurs 065 */ 066 public void send(final byte[] data, final int length, final InetAddress host) throws IOException { 067 send(data, length, host, DEFAULT_PORT); 068 } 069 070 /** 071 * Sends the specified data to the specified server at the specified port. 072 * 073 * @param data The discard data to send. 074 * @param length The length of the data to send. Should be less than or equal to the length of the data byte array. 075 * @param host The address of the server. 076 * @param port The service port. 077 * @throws IOException If an error occurs during the datagram send operation. 078 */ 079 public void send(final byte[] data, final int length, final InetAddress host, final int port) throws IOException { 080 sendPacket.setData(data); 081 sendPacket.setLength(length); 082 sendPacket.setAddress(host); 083 sendPacket.setPort(port); 084 checkOpen().send(sendPacket); 085 } 086 087}