Distributed Systems Lab/06


Lab 1 / Simple Sockets File Transfer Client

Introduction

Most interprocess communication uses the client server model. These terms refer to the two processes which will be communicating with each other. One of the two processes, the client, connects to the other process, the server, typically to make a request for information. A good analogy is a person who makes a phone call to another person.

The system calls for establishing a connection are somewhat different for the client and the server, but both involve the basic construct of a socket. A socket is one end of an interprocess communication channel. The two processes each establish their own socket.

The basic steps involved in establishing and using a socket on the client side are as follows:

  1. Create a socket with the Socket Java class and connect the socket to the address of the server
  2. Send and receive data. There are a number of ways to do this, but the simplest is to use the readLine() and write() methods of the BufferedReader and BufferedWriter classes.

Detailed Description

Your objective is to develop a simple sockets-based text file transfer client in Java (version 1.5). This client can connect to a file server and enables the user to look for ASCII TEXT files and download them (i.e., you do not need to worry about transfering binary files such as images and programs).

The functionality of your file transfer client should be coded into the class Client, which is stored in the file Client.java. The class should be a stand-alone program that will be directly invoked by java. Therefore, it must contain a static main() function.

Your program is supposed to take exactly two command-line arguments. If more or less than two command-line arguments are passed to your program, or some error happens (e.g., a host ist not found) you have to exit immediately with exit code 1 (i.e., System.exit(1)). In all other cases, your program should exit with exit code 0. The synopsis of the program is as follows:

java Client <server> <port>

where

  1. server is the name of the simple file transfer server that your program has to connect to (for example, www.dslab.tuwien.ac.at).
  2. port is the port number you have to connect to (for example, 10000).

When your program is invoked with two arguments, it should first connect to the file transfer server. Then, your client should read commands from the standard input (System.in in Java).

Your interactive client should understand and process the following commands (note that all commands are case-sensitive... i.e., LS is an unknown command):

  1. ls <file path>. Where if file path points to a file, your program returns the length of the file and if file path points to a directory, your program returns the listing of the directory.
  2. get <path>. Downloads the file indicated by file path and stores it locally into a file download.dsg.
  3. quit Terminates the connection to the server and exits the program with exit code 0.

Note that the our server uses the UNIX file separator "/" in file and directory names (e.g., "ls /funny-stuff").

The file transfer server uses a simple protocol to communicate with its clients. Here is the protocol description (to give you some "hands-on" experience reading protocol descriptions, we made it look like a typical protocol specification).

After your client connects to the server, it should print:

Connection established.

to the standard output. Then, it should print a command prompt:

>:

and wait for a user command from the standard input. In case the server returns an error message (e.g., 2XX error codes such as 213 Access denied), then the error code and the error message should be printed to the standard output.

Here is an input file that you can use for testing. Download this file and invoke your client as follows:

java Client www.dslab.tuwien.ac.at 10000 < input.txt > output.txt

In any case, the results in your output.txt file have to be identical to the contents of the dump in our output file in the lab environment (Note that these files were created on Linux, you might have problems under Windows so copy them to the lab environment and test them there). This is how the grading robot will also test your solution so make sure that at least this test works before submitting.

If you are testing interactively, here is the output you should see on the display:

dslab900@pizza:~% java Client www.dslab.tuwien.ac.at 10000
Connection established.
>: ls
emptyfile.txt
funny-stuff
nopermission
password-cracker
readme.txt
x-rated.txt
>: bah bah
200 Command not recognised
>: get funny-stuff
214 You can't get a directory
>: get nopermission
214 You can't get a directory
>: get readme.txt
>: ls funny-stuff
chicken.txt
os_comparison.txt
religions.txt
somedirectory
>: ls funny-stuff/somedirectory
somefile.txt
>: quit
dslab900@pizza:~%

In case of any errors (null pointer exception, host not found, etc.), terminate your program with exit code 1.

Hints for Solving the Lab

  1. If you are new to network programming, you might like to read the sockets tutorial.
  2. In order to test your client, you can use our file transfer server. The DSLab file transfer server is running at www.dslab.tuwien.ac.at at port 10000.
  3. telnet is a useful tool in network programming and debugging. Try connecting to the file transfer server using telnet and giving commands manually. For example, like this:

    ek@pizza:~> telnet www.dslab.tuwien.ac.at 10000
    Trying 128.131.172.139...
    Connected to www.dslab.tuwien.ac.at.
    Escape character is '^]'.
    100 Welcome to the DSLab File Server
    QUIT
    107 Sayonara, take good care now
    Connection closed by foreign host.
    ek@pizza:~>

    Using telnet will allow you to see the messages being exchanged between the server and the client.
  4. When implementing your client, all you need to do is to pass the commands ls, get and quit to the server.
  5. Make sure that you do not print anything to standard output or standard error other than what we have specified. Otherwise, the grading robots will have problems "understanding" your work and will think that it is wrong.
  6. Note that if no parameters are given to the server with the LS and GET commands, it assumes that the / directory (i.e., root) ist meant.

Deliverables

To submit your solution to us, you need to follow these steps:

  1. Develop your solution in Java (version 1.5), writing the code into the Client class in a file called Client.java (e.g., using vi, emacs, whatever). Make sure that your Java program compiles and runs in the lab environment.
  2. If you have created this file on some other machine, copy it over using scp (secure copy).
  3. In the directory where your Client.java file is located, on pizza.dslab.tuwien.ac.at or pasta.dslab.tuwien.ac.at, call submit1
  4. Read any error or success messages
  5. It might take up to an hour for our grading robot to check your program. Don't forget to read your e-mail to check the results of the automatic grading robot. If you have errors, correct them and try again!

Deadline

You need to submit your solution until November the 2nd, 17:59.

Closing words

Have fun ;-)


Last Modified: Fre Nov 10 15:32:01 CET 2006


Distributed Systems Group, Technical University of Vienna, Argentinierstrasse 8 / 184-1, 1040 Vienna, Austria, www.infosys.tuwien.ac.at