TCP Programming Project: File Server on Internet

The above TCP server-client program can be used as a basis for TCP-based network programming. It can be adapted to different applications by simply changing the data contents and the ways they process the data. For example, instead of numbers, the client may send file operation commands to the server, which processes the commands and send results back to the client. This programming project is to develop a pair of TCP server and client to do file operations across the Internet. The project specification is as follows.

1. Project Specification

Design and implement a TCP server and a TCP client to do file operations across the Internet. The following charts depict the algorithms of the server and the client.

In a command line, depending on the command, pathname may be either a file or a directory. Valid commands are

mkdir:     make a directory with pathname

rmdir:     remove directory named pathname

rm:          remove file named pathname

cd :         change Current Working Directory (CWD) to pathname

pwd:       show absolute pathname of CWD

Is:           list CWD or pathname in the same format as ls -l of Linux

get:        download the pathname file from server

put:        upload the pathname file to server

2. Helps and Hints

  •  When running on an Internet host, the server must publish its hostname or IP address and port number to allow clients to connect. For security reasons, the server should set the virtual root to CWD of the server process to prevent the client from accessing files above the virtual root.
  •  Most commands are simple to implement. For example, each of the first 5 commands requires only a single Linux system call, as shown below.

For the ls command, the reader may consult the ls.c program in Section 8.6.7 of Chapter 8. For the get filename command, it is essentially the same as a file copy program split into two parts. The server opens the file for READ, read the file contents and send them to the client. The client opens a file for WRITE, receives data from the server and writes the received data to the file. For the put filename command, simply reverse the roles of the server and client.

  •  When using TCP, data are continuous streams. To ensure the client and server can send/receive commands and simple replies, it is better for both sides to write/read lines of fixed size, e.g. 256 bytes.
  •  The reader must design user-level protocols to ensure correct data transfer between the server and client. The following describes the commands which need user-level data transfer protocols For the ls command, the server has two options. In the first option, for each entry in a directory, the server generates a line of the form

-rw-r–r– link gid uid size date name

and saves the line into a temporary file. After accumulating all the lines, the server sends the entire temporary file to the client. In this case, the server must be able to tell the client where the file begins and where it ends. Since the ls command generates only text lines, the reader may use special ASCII chars as file begin and end marks.

In the second option, the server may generate a line and send it immediately to the client before generating and sending the next line. In this case, the server must be able to tell the client when it starts to send lines and also when there are no more lines to come.

For the get/put commands, which transfer file contents, it is not possible to use special ASCII chars as file begin and end marks. This is because a binary file may contain any ASCII code. The standard solution to this problem is by bit stuffing [SDLC, IBM, 1979]. In this scheme, while sending data, the sender inserts an extra 0 bit after each sequence of 5 or more consecutive 1 bits, so that the transmitted data never contains any 6 or more consecutive 1’s. At the receiving end, the receiver strips off the extra 0 bit after every sequence of 5 consecutive 1’s. This allows both sides to use the special flag bits pattern 01111110 as file begin and end marks. Bit-stuffing is inefficient without hardware assistance. The reader must think of other ways to synchronize the server and the client.

Hint: by file size.

  • . The project is suitable for 2-person team work. During development, the team members may discuss how to design user-level protocols and divide the implementation work between them. During testing, one member can run the server on an Internet host while the other runs the client on a different host.

Figure 13.9 shows the sample outputs of running the project program.

As the figure shows, the server runs on the host at port 56809. The client runs on a different host with IP= at port 48774. In addition to the specified commands, the client side also implements local file operation commands, which are performed by the client directly.

3. Multi-threaded TCP Server

In the TCP server-client programming project, the server can only serve one client at a time. In a multi­threaded server, it can accept connections from multiple clients and serve them at the same time or concurrently. This can be done either by fork-exec of new server processes or by multiple threads in the same server process. We leave this extension as another possible programming project.

Source: Wang K.C. (2018), Systems Programming in Unix/Linux, Springer; 1st ed. 2018 edition.

Leave a Reply

Your email address will not be published. Required fields are marked *