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.examples.unix;
019
020import java.io.IOException;
021import java.net.InetAddress;
022
023import org.apache.commons.net.daytime.DaytimeTCPClient;
024import org.apache.commons.net.daytime.DaytimeUDPClient;
025
026/**
027 * This is an example program demonstrating how to use the DaytimeTCP and DaytimeUDP classes. This program connects to the default daytime service port of a
028 * specified server, retrieves the daytime, and prints it to standard output. The default is to use the TCP port. Use the -udp flag to use the UDP port.
029 * <p>
030 * Usage: daytime [-udp] <hostname>
031 */
032public final class daytime {
033
034    public static void daytimeTCP(final String host) throws IOException {
035        final DaytimeTCPClient client = new DaytimeTCPClient();
036
037        // We want to timeout if a response takes longer than 60 seconds
038        client.setDefaultTimeout(60000);
039        client.connect(host);
040        System.out.println(client.getTime().trim());
041        client.disconnect();
042    }
043
044    public static void daytimeUDP(final String host) throws IOException {
045        final DaytimeUDPClient client = new DaytimeUDPClient();
046
047        // We want to timeout if a response takes longer than 60 seconds
048        client.setDefaultTimeout(60000);
049        client.open();
050        System.out.println(client.getTime(InetAddress.getByName(host)).trim());
051        client.close();
052    }
053
054    public static void main(final String[] args) {
055
056        if (args.length == 1) {
057            try {
058                daytimeTCP(args[0]);
059            } catch (final IOException e) {
060                e.printStackTrace();
061                System.exit(1);
062            }
063        } else if (args.length == 2 && args[0].equals("-udp")) {
064            try {
065                daytimeUDP(args[1]);
066            } catch (final IOException e) {
067                e.printStackTrace();
068                System.exit(1);
069            }
070        } else {
071            System.err.println("Usage: daytime [-udp] <hostname>");
072            System.exit(1);
073        }
074
075    }
076
077}