/* * Java Network Programming * Copyright (C) 2000 by Jeffrey E. Care (carej@ieee.org) * David Hollinger (hollingd@cs.rpi.edu) * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) any * later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 * USA */ package edu.rpi.cs.netprog.tcp; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.Hashtable; public final class TCPClient { //---------------------------------------------------------------------------- // Public Constants //---------------------------------------------------------------------------- public static final int echo = 7; public static final int discard = 9; public static final int daytime = 13; public static final int chargen = 19; public static final int ftp = 21; public static final int ftpdata = 20; public static final int telnet = 23; public static final int smtp = 25; public static final int time = 37; public static final int dns = 53; public static final int gopher = 70; public static final int finger = 79; public static final int http = 80; public static final int rtelnet = 107; public static final int pop2 = 109; public static final int pop3 = 110; public static final int auth = 113; public static final int nntp = 119; public static final int ntp = 123; public static final int imap2 = 143; public static final int irc = 194; //---------------------------------------------------------------------------- // Private Data //---------------------------------------------------------------------------- private static Hashtable services; //---------------------------------------------------------------------------- // Static Initializer //---------------------------------------------------------------------------- static { services = new Hashtable (); services.put ("echo", new Integer (echo)); services.put ("discard", new Integer (discard)); services.put ("daytime", new Integer (daytime)); services.put ("chargen", new Integer (chargen)); services.put ("ftp", new Integer (ftp)); services.put ("ftpdata", new Integer (ftpdata)); services.put ("telnet", new Integer (telnet)); services.put ("smtp", new Integer (smtp)); services.put ("time", new Integer (time)); services.put ("dns", new Integer (dns)); services.put ("gopher", new Integer (gopher)); services.put ("finger", new Integer (finger)); services.put ("http", new Integer (http)); services.put ("rtelnet", new Integer (rtelnet)); services.put ("pop2", new Integer (pop2)); services.put ("pop3", new Integer (pop3)); services.put ("auth", new Integer (auth)); services.put ("nntp", new Integer (nntp)); services.put ("ntp", new Integer (ntp)); services.put ("imap2", new Integer (imap2)); services.put ("irc", new Integer (irc)); } //---------------------------------------------------------------------------- // Port extractor //---------------------------------------------------------------------------- private static int getPort (String arg) { arg = arg.toLowerCase (); // check if the argument was a service name // if (services.containsKey (arg)) { Integer i = (Integer) services.get (arg); return i.intValue (); } // check if the argument is a numeric value // try { int i = Integer.parseInt (arg); return i; } catch (Exception exp) { printUsage (); System.exit (0); } return -1; } //---------------------------------------------------------------------------- // Usage message //---------------------------------------------------------------------------- private static void printUsage () { System.out.println ("You must supply a valid hostname and port."); } //---------------------------------------------------------------------------- // Main //---------------------------------------------------------------------------- public static void main (String[] argv) { int argc = argv.length; // check the command line // if (argc < 2) printUsage (); String host = argv[0]; int port = getPort (argv[1]); // open a socket and talk to the server try { Socket sock = new Socket (host, port); InputManager im = new InputManager (System.in, sock); StringBuffer message = new StringBuffer ("Connected to "); message.append (sock.getInetAddress ().getHostName ()); message.append (":"); message.append (sock.getPort ()); message.append (" <"); message.append (sock.getInetAddress ().getHostAddress ()); message.append (":"); message.append (sock.getPort ()); message.append ("> on local port "); message.append (sock.getLocalPort ()); message.append ("\n"); System.out.println (message); BufferedReader in = new BufferedReader (new InputStreamReader (sock.getInputStream ())); String line; while ((line = in.readLine ()) != null) System.out.println (line); } catch (Exception exp) { exp.printStackTrace (); } } }