Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment Write a C++ program that implements a simple file server. Program Implement a C++ program that in a loop listens on a port for

Assignment

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:

  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:

 % ./z123456 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 beconstructedand sent back to the client.

Other Points

  • you can test your server program with theTCPClientcommand we used in class, as in:
% ./TCPClient localhost 9001 "GET /" fileOne.html fileTwo.html % ./TCPClient localhost 9001 "GET /fileOne" Error: fileOne not found % ./TCPClient localhost 9001 "GET /fileOne.html" ... content of fileOne.html ... 

HERES WHAT I HAVE WRITTEN

#include /*socket definitions*/

#include /*socket types*/

#include /*for waitpid()*/

#include /*inet (3) funtions*/

#include /*misc. UNIX functions*/

#include

#include

#include

#include

#include

using namespace std;

void clientRequest(int newConnection) {

char path[1024], buffer[1024];

int received = 0;

//read the process

if((received = read(newConnection, path, sizeof(path))) < 0) {

cout << path << endl;

}

//open directory

DIR *dirp = opendir(path);

if (dirp == 0) {

//give error

strcpy(buffer,path);

strcat(buffer, ": could not open");

if(write(newConnection,buffer,strlen(buffer)) <0) {

perror("write");

exit(EXIT_FAILURE);

}

exit(EXIT_SUCCESS);

}

//read directory

struct dirent *dirEntry;

while((dirEntry = readdir(dirp)) != NULL) {

strcpy(buffer, dirEntry->d_name);

strcat(buffer," ");

if(write(newConnection, buffer, strlen(buffer)) < 0) {

perror("write");

exit(EXIT_FAILURE);

}

cout << "sent: " << buffer;

}

closedir(dirp);

close(newConnection);

exit(EXIT_SUCCESS);

}

int main(int argc, char *argv[]) {

if (argc != 3) {

cerr << "USAGE: server port ";

exit(EXIT_FAILURE);

}

intsockfd, newConnection;

/*Create TCP socket*/

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if(sockfd < 0) {

perror("Couldn't create socket.");

exit(EXIT_FAILURE);

}

struct sockaddr_in servaddr;

struct sockaddr_in clientaddr;

unsigned int addrlen = sizeof(clientaddr);

int backlog = 64;

/*Populate socket address structure*/

memset(&servaddr, 0, sizeof(servaddr)); //clear struct

servaddr.sin_family= AF_INET; // internet/IP

servaddr.sin_addr.s_addr = INADDR_ANY; //takes any IP

servaddr.sin_port= htons(atoi(argv[1])); //argument 1 takes the port

/*Assign socket address to socket*/

if ( bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 )

{

perror("Couldn't bind");

exit(EXIT_FAILURE);

}

/*Make socket passive and set length of queue */

if ( listen(sockfd, backlog) < 0 )

{

perror("listen failed.");

exit(EXIT_FAILURE);

}

cout << "Server host now on port: " << argv[1]<< endl;

/*Loop infinitely to accept and service connections*/

while ( true ) {

//for accept

newConnection = accept(sockfd, (struct sockaddr *) &clientaddr , &addrlen);

if(newConnection < 0) {

perror("accept");

exit(EXIT_FAILURE);

}

//fork

if (fork()) { // parent process

close(newConnection);

}

else { //child process

clientRequest(newConnection);

}

return 0;/*We shouldn't get here*/

}

}

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_2

Step: 3

blur-text-image_3

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions