codeninja.net
my projects
my personal site
CSH
vim powered
GeoURL
|
This is a simple Finger client, and works much like
Finger(1). It is also a simple demonstration of doing TCP
socket communication in Java.
Here is the Source Code.
©2000, Sean Graham.
License Information
Finger.java.html
import java.io.*;
import java.net.*;
/**
* The Finger application connects to a TCP Finger daemon as a client.
*
* @version 1.0
* @author Sean M. Graham <grahams {of} csh [dot] rit [dot] educational>
*
*/
public class Finger
{
/**
* This is a simple app, so it only really requires a main().
*
* @param args The command line arguments
*/
public static void main( String[] args ) {
String command; // The finger command to send to the remote server
String host; // The host we will connect to
int atIndex; // The index of the @ in args[0] (-1 if none)
command = null;
host = null;
atIndex = -1;
// If there were no command-line arguments, we are looking for a
// listing of users on the local machine.
if( args.length > 1 ) {
System.out.println( "Finger: Too Many Arguments" );
System.exit(-1);
}
else if( args.length > 0 ) {
command = args[0];
// An interesting "feature" of the finger protocol is that
// according to the RFC, the following is valid:
//
// finger grahams@example.com@iron.cs.rit.edu
//
// As odd as this sounds, most finger servers support this, so
// we will look for the index of the LAST @ sign to determine
// where the hostname starts.
atIndex = command.lastIndexOf( "@" );
command = command.trim();
}
else {
command = "";
atIndex = -1;
}
// If there is an '@' in the command string, then we need to query a
// remote server, otherwise we are querying the local machine
if( atIndex == -1 ) {
host = "localhost";
}
else {
host = command.substring( atIndex + 1 );
command = command.substring( 0, atIndex );
}
try {
Socket s = new Socket( host, 79 );
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()));
boolean more = true;
// Send the 'finger command' to the remote side.
out.println( command );
// For as long as the other side shoves data at us, print it.
while( more ) {
String line = in.readLine();
if( line == null ) {
more = false;
}
else {
System.out.println( line );
}
}
// Be a good little application and close our streams
out.close();
in.close();
s.close();
}
catch( UnknownHostException e) {
System.out.println( "Finger: Invalid Machine Name (" +
args[0] + ")" );
}
catch( IOException e ) {
System.out.println( "Finger: " + e );
}
}
}
|
|