001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net;
017:
018: import java.io.IOException;
019: import java.net.DatagramPacket;
020: import java.net.InetAddress;
021:
022: /***
023: * The CharGenUDPClient class is a UDP implementation of a client for the
024: * character generator protocol described in RFC 864. It can also be
025: * used for Systat (RFC 866), Quote of the Day (RFC 865), and netstat
026: * (port 15). All of these protocols involve sending a datagram to the
027: * appropriate port, and reading data contained in one or more reply
028: * datagrams. The chargen and quote of the day protocols only send
029: * one reply datagram containing 512 bytes or less of data. The other
030: * protocols may reply with more than one datagram, in which case you
031: * must wait for a timeout to determine that all reply datagrams have
032: * been sent.
033: * <p>
034: * To use the CharGenUDPClient class, just open a local UDP port
035: * with {@link org.apache.commons.net.DatagramSocketClient#open open }
036: * and call {@link #send send } to send the datagram that will
037: * initiate the data reply. For chargen or quote of the day, just
038: * call {@link #receive receive }, and you're done. For netstat and
039: * systat, call receive in a while loop, and catch a SocketException and
040: * InterruptedIOException to detect a timeout (don't forget to set the
041: * timeout duration beforehand). Don't forget to call
042: * {@link org.apache.commons.net.DatagramSocketClient#close close() }
043: * to clean up properly.
044: * <p>
045: * <p>
046: * @author Daniel F. Savarese
047: * @see CharGenTCPClient
048: ***/
049:
050: public final class CharGenUDPClient extends DatagramSocketClient {
051: /*** The systat port value of 11 according to RFC 866. ***/
052: public static final int SYSTAT_PORT = 11;
053: /*** The netstat port value of 19. ***/
054: public static final int NETSTAT_PORT = 15;
055: /*** The quote of the day port value of 17 according to RFC 865. ***/
056: public static final int QUOTE_OF_DAY_PORT = 17;
057: /*** The character generator port value of 19 according to RFC 864. ***/
058: public static final int CHARGEN_PORT = 19;
059: /*** The default chargen port. It is set to 19 according to RFC 864. ***/
060: public static final int DEFAULT_PORT = 19;
061:
062: private byte[] __receiveData;
063: private DatagramPacket __receivePacket;
064: private DatagramPacket __sendPacket;
065:
066: /***
067: * The default CharGenUDPClient constructor. It initializes some internal
068: * data structures for sending and receiving the necessary datagrams for
069: * the chargen and related protocols.
070: ***/
071: public CharGenUDPClient() {
072: // CharGen return packets have a maximum length of 512
073: __receiveData = new byte[512];
074: __receivePacket = new DatagramPacket(__receiveData, 512);
075: __sendPacket = new DatagramPacket(new byte[0], 0);
076: }
077:
078: /***
079: * Sends the data initiation datagram. This data in the packet is ignored
080: * by the server, and merely serves to signal that the server should send
081: * its reply.
082: * <p>
083: * @param host The address of the server.
084: * @param port The port of the service.
085: * @exception IOException If an error occurs while sending the datagram.
086: ***/
087: public void send(InetAddress host, int port) throws IOException {
088: __sendPacket.setAddress(host);
089: __sendPacket.setPort(port);
090: _socket_.send(__sendPacket);
091: }
092:
093: /*** Same as <code>send(host, CharGenUDPClient.DEFAULT_PORT);</code> ***/
094: public void send(InetAddress host) throws IOException {
095: send(host, DEFAULT_PORT);
096: }
097:
098: /***
099: * Receive the reply data from the server. This will always be 512 bytes
100: * or less. Chargen and quote of the day only return one packet. Netstat
101: * and systat require multiple calls to receive() with timeout detection.
102: * <p>
103: * @return The reply data from the server.
104: * @exception IOException If an error occurs while receiving the datagram.
105: ***/
106: public byte[] receive() throws IOException {
107: int length;
108: byte[] result;
109:
110: _socket_.receive(__receivePacket);
111:
112: result = new byte[length = __receivePacket.getLength()];
113: System.arraycopy(__receiveData, 0, result, 0, length);
114:
115: return result;
116: }
117:
118: }
|