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