previous | start | next

An Example Client.

Port 79 is the well-known port for the finger application protocol. If we send a text string containing a valid username on ironbark, we should receive some interesting facts about the user. Here's the Java code:
import java.io.*;
import java.net.*;
class FingerClient {
    public static void main(String argv[]) throws Exception
    {
        String user = "pscott";
        String response;
        Socket clientSock = new Socket("ironbark", 79);
        DataOutputStream toserver =
            new DataOutputStream(
                clientSock.getOutputStream());
        DataInputStream serverReply =
            new DataInputStream(
                clientSock.getInputStream());
        toserver.writeBytes(user + "\n");
        response = serverReply.readLine();
        System.out.println("Finger Reply: " + response);
        clientSock.close();
    }
}
Note that this program only outputs the first line of the data returned from the server. Important points: we first create a "connected" socket, then we send a line of text, and finally we receive a line of text in response -- the classic Internet application protocol scenario.
 
Lecture 9: Socket Programming Interface Copyright © 2005 P.Scott, La Trobe University Bendigo.


previous | start | next