Who.java Source code

Java tutorial

Introduction

Here is the source code for Who.java

Source

import java.applet.Applet;
import java.awt.Button;
import java.awt.Font;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class Who {
    public static void main(String[] v) {
        Socket s = null;
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            // Connect to port 79 (the standard finger port) on the host.
            String hostname = "www.java2s.com";
            s = new Socket(hostname, 79);
            // Set up the streams
            out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));

            // Send a blank line to the finger server, telling it that we want
            // a listing of everyone logged on instead of information about an
            // individual user.
            out.print("\n");
            out.flush(); // Send it out

            // Now read the server's response
            // The server should send lines terminated with \n or \r.
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            System.out.println("Who's Logged On: " + hostname);
        } catch (IOException e) {
            System.out.println("Who's Logged On: Error");
        }
        // Close the streams!
        finally {
            try {
                in.close();
                out.close();
                s.close();
            } catch (Exception e) {
            }
        }
    }
}