Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that implements a simple file server. Program Implement a C+

ID: 3570181 • Letter: W

Question

Write a C++ program that implements a simple file server.

Program

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:

the port number to listen on,

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

For example:

The requests received by the program are of the form:

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 contructed and sent back to the client.

Other Points

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

you can find the source code for the basicClient program  >> here << ;

make sure that your assignment is contained in a single file called "z123456.cxx" based on your Z-id;

make sure that your program compiles, links and runs fine on your Linux system, turing or hopper.

Submission

Submit your C++ source code file via Blackboard below.

Extra Credit

If you implement this part of the assignment, please indicate it in the file header comment.

For an additional 20 extra credit points, implement a second request command:

It will return the current time and date in text format to the client.

this is what i have so far....

Explanation / Answer

include <stdio.h>

#define DATA "connection request. . ."

/*

* This program creating pipe, then forks leading to the child communication to the parent with the help of pipe with one way communication.

* socket (sockets[1], the second socket of the array returned by pipe()) and read from the

* input socket (sockets[0]), but not vice versa.

*/

main()

{

int sockets[2], child;

/* Create a pipe */

if (pipe (sockets) < 0) {

perror("opening stream socket pair");

exit(1);

}

if ((child = fork()) = = -1)

perror("fork");

elseif (child) {

char buf[1024];

/* This is still the parent. It reads the child's message. */

close (sockets[1]);

if (read(sockets[0], buf, 1024) < 0)

perror("reading message");

printf(" -->%s ", buf);

close(sockets[0]);

}else {

/* This is the child. It writes a message to its parent. */

close (sockets[0]);

if (write (sockets[1], DATA, sizeof(DATA)) < 0)

perror("writing message");

close(sockets[1]);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote