Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ program that implements a simple file server. Implement a C++ program that in a loop listens on a port for incoming TCP requests from

C++ program that implements a simple file server.

Implement a C++ program that in a loop listens on a port for incoming TCP requests from clients. For each accepted incoming request it forks a child to read and process the request. The parent process continues to listen and accept incoming TCP requests in an endless loop.

The program accepts 2 command line parameters:

1.) the port number to listen on,

2.) the pathname to a directory that serves as root to all requested files or directories.

For example:

% ./z1234567 9001 www

The requests received by the program are of the form:

GET pathname

where the pathname refers to a file or directory to be sent back to the client. The file/directory will be found in the directory specified on the command line. The following rules apply to the pathname:

it must start with a "/"

it may contain additional "/" path separators to access subdirectories

a single "/" character refers to the directory specified on the command line

a trailing "/" in the pathname can be ignored if the pathname refers to a directory

any data in the request beyond the pathname should be ignored

it may not contain the substring ".."

If the pathname refers to a file, then the content of the file is returned to the client.

If the pathname refers to a directory, then: if a file "index.html" exists in that directory, it will be returned;

else, a list of files in that directory will be returned (not including any file that starts with ".").

Error Checking If the command line arguments are incomplete, or if the path to the root directory is invalid, print an error message and exit. If any of the system calls fail, the program should use "perror" to report and exit. If the pathname in the GET request is invalid or a file/directory cannot be accessed, then an appropriate error message should be constructed and sent back to the client.

Other Points you can test your server program with the TCPClient command we used in class, as in:

image text in transcribed
% /TCPClient localhost 9001 "GET/" file One.html file Two.html % ./TCPClient localhost 9001 "GET /fileOne" Error: fileOne not found % /TCPClient localhost 9001 "GET /fileOne.html" ... content of file One.html %

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

Students also viewed these Programming questions

Question

understand the basics of segmenting across countries

Answered: 1 week ago

Question

Describe two different types of natural classification in taxonomy

Answered: 1 week ago

Question

Distinguish between Eubacteria and cyanobacteria?

Answered: 1 week ago

Question

Describe the characteristics used in classification

Answered: 1 week ago