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.chargen; 019 020import java.io.IOException; 021import java.net.DatagramPacket; 022import java.net.InetAddress; 023 024import org.apache.commons.net.DatagramSocketClient; 025 026/*** 027 * The CharGenUDPClient class is a UDP implementation of a client for the 028 * character generator protocol described in RFC 864. It can also be 029 * used for Systat (RFC 866), Quote of the Day (RFC 865), and netstat 030 * (port 15). All of these protocols involve sending a datagram to the 031 * appropriate port, and reading data contained in one or more reply 032 * datagrams. The chargen and quote of the day protocols only send 033 * one reply datagram containing 512 bytes or less of data. The other 034 * protocols may reply with more than one datagram, in which case you 035 * must wait for a timeout to determine that all reply datagrams have 036 * been sent. 037 * <p> 038 * To use the CharGenUDPClient class, just open a local UDP port 039 * with {@link org.apache.commons.net.DatagramSocketClient#open open } 040 * and call {@link #send send } to send the datagram that will 041 * initiate the data reply. For chargen or quote of the day, just 042 * call {@link #receive receive }, and you're done. For netstat and 043 * systat, call receive in a while loop, and catch a SocketException and 044 * InterruptedIOException to detect a timeout (don't forget to set the 045 * timeout duration beforehand). Don't forget to call 046 * {@link org.apache.commons.net.DatagramSocketClient#close close() } 047 * to clean up properly. 048 * 049 * @see CharGenTCPClient 050 ***/ 051 052public final class CharGenUDPClient extends DatagramSocketClient 053{ 054 /*** The systat port value of 11 according to RFC 866. ***/ 055 public static final int SYSTAT_PORT = 11; 056 /*** The netstat port value of 19. ***/ 057 public static final int NETSTAT_PORT = 15; 058 /*** The quote of the day port value of 17 according to RFC 865. ***/ 059 public static final int QUOTE_OF_DAY_PORT = 17; 060 /*** The character generator port value of 19 according to RFC 864. ***/ 061 public static final int CHARGEN_PORT = 19; 062 /*** The default chargen port. It is set to 19 according to RFC 864. ***/ 063 public static final int DEFAULT_PORT = 19; 064 065 private final byte[] __receiveData; 066 private final DatagramPacket __receivePacket; 067 private final DatagramPacket __sendPacket; 068 069 /*** 070 * The default CharGenUDPClient constructor. It initializes some internal 071 * data structures for sending and receiving the necessary datagrams for 072 * the chargen and related protocols. 073 ***/ 074 public CharGenUDPClient() 075 { 076 // CharGen return packets have a maximum length of 512 077 __receiveData = new byte[512]; 078 __receivePacket = new DatagramPacket(__receiveData, __receiveData.length); 079 __sendPacket = new DatagramPacket(new byte[0], 0); 080 } 081 082 083 /*** 084 * Sends the data initiation datagram. This data in the packet is ignored 085 * by the server, and merely serves to signal that the server should send 086 * its reply. 087 * 088 * @param host The address of the server. 089 * @param port The port of the service. 090 * @throws IOException If an error occurs while sending the datagram. 091 ***/ 092 public void send(InetAddress host, int port) throws IOException 093 { 094 __sendPacket.setAddress(host); 095 __sendPacket.setPort(port); 096 _socket_.send(__sendPacket); 097 } 098 099 /*** Same as <code>send(host, CharGenUDPClient.DEFAULT_PORT);</code> 100 * @param host the destination host 101 * @throws IOException on error 102 ***/ 103 public void send(InetAddress host) throws IOException 104 { 105 send(host, DEFAULT_PORT); 106 } 107 108 /*** 109 * Receive the reply data from the server. This will always be 512 bytes 110 * or less. Chargen and quote of the day only return one packet. Netstat 111 * and systat require multiple calls to receive() with timeout detection. 112 * 113 * @return The reply data from the server. 114 * @throws IOException If an error occurs while receiving the datagram. 115 ***/ 116 public byte[] receive() throws IOException 117 { 118 int length; 119 byte[] result; 120 121 _socket_.receive(__receivePacket); 122 123 result = new byte[length = __receivePacket.getLength()]; 124 System.arraycopy(__receiveData, 0, result, 0, length); 125 126 return result; 127 } 128 129} 130