01: /*
02: * Copyright 2001-2005 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: /*** The default discard port. It is set to 9 according to RFC 863. ***/
39: public static final int DEFAULT_PORT = 9;
40:
41: DatagramPacket _sendPacket;
42:
43: public DiscardUDPClient() {
44: _sendPacket = new DatagramPacket(new byte[0], 0);
45: }
46:
47: /***
48: * Sends the specified data to the specified server at the specified port.
49: * <p>
50: * @param data The discard data to send.
51: * @param length The length of the data to send. Should be less than
52: * or equal to the length of the data byte array.
53: * @param host The address of the server.
54: * @param port The service port.
55: * @exception IOException If an error occurs during the datagram send
56: * operation.
57: ***/
58: public void send(byte[] data, int length, InetAddress host, int port)
59: throws IOException {
60: _sendPacket.setData(data);
61: _sendPacket.setLength(length);
62: _sendPacket.setAddress(host);
63: _sendPacket.setPort(port);
64: _socket_.send(_sendPacket);
65: }
66:
67: /***
68: * Same as
69: * <code>send(data, length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
70: ***/
71: public void send(byte[] data, int length, InetAddress host)
72: throws IOException {
73: send(data, length, host, DEFAULT_PORT);
74: }
75:
76: /***
77: * Same as
78: * <code>send(data, data.length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
79: ***/
80: public void send(byte[] data, InetAddress host) throws IOException {
81: send(data, data.length, host, DEFAULT_PORT);
82: }
83:
84: }
|