This is Finger.java, my first useful Java program.

// command-line finger program in Java.  Try fingering [EMAIL PROTECTED]
import java.net.*;

public class Finger {
    private static final String USAGE_MSG = 
        "Usage: Finger user@host or Finger user host";
    public static void finger(String username, String host) throws Exception {
        InetAddress inaddr = InetAddress.getByName(host);
        java.io.InputStream is;
        java.io.OutputStream os;
        byte[] readbuf = new byte[4096];
        int readlen;
        Socket mysocket = new Socket(inaddr, 79);
        is = mysocket.getInputStream();
        os = mysocket.getOutputStream();
        // I wonder where these encoding names are listed?  They're not listed
        // in the JDK 1.2.2 API documentation under java.lang.String, which
        // is where I'd expect them to be.  I got a list of the ones supported
        // by my libgcj as follows:
        // objdump -T /usr/lib/libgcj.so.0 | egrep gnu.*gcj.*convert.*Output
        // because GCJ complained there was no class 
        // gnu.gcj.convert.Output_ASCII when I tried that.
        // This told me that my libgcj supports "SJIS", "8859_1", "EUCJIS", 
        // "UTF8", and "JavaSrc", and it happens that Kaffe also supports 
        // "8859_1".
        os.write(username.getBytes("8859_1"));
        os.write("\r\n".getBytes("8859_1"));
        while ((readlen = is.read(readbuf)) > 0) {
            System.out.write(readbuf, 0, readlen);
        }
        System.out.flush();
        mysocket.close();
    }

    public static void finger(String userathost) throws Exception {
        int atloc = userathost.indexOf('@');
        if (atloc == -1) {
            System.out.println(USAGE_MSG);
        } else {
            finger(userathost.substring(0, atloc), 
                   userathost.substring(atloc+1));
        }
    }

    public static void main(String[] arguments) throws Exception {
        if (arguments.length == 1) {
            finger(arguments[0]);
        } else if (arguments.length == 2) {
            finger(arguments[0], arguments[1]);
        } else {
            System.out.println(USAGE_MSG);
        }
    }
}

-- 
<[EMAIL PROTECTED]>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
We have always been quite clear that Win95 and Win98 are not the systems to 
use if you are in a hostile security environment.  -- Paul Leach
The Internet is hostile.                -- Paul Leach <[EMAIL PROTECTED]> 


Reply via email to