PLEASE ANSWER THE QUESTION, DO NOT POST UNRELATED SH*T! DO NOT CHANGE FUNCTION N
ID: 3843887 • Letter: P
Question
PLEASE ANSWER THE QUESTION, DO NOT POST UNRELATED SH*T!
DO NOT CHANGE FUNCTION NAMES. THERE ARE ONLY TWO PARTS TO BE COMPLETED. IT SAYS "YOUR CODE HERE"! DON'T CHANGE ANYTHING ELSE!!
Please finish the server of a client-server application. The client asks the user for a letter (a character in {A-Z,a-z}), and then sends that letter to the server. The server malloc()s memory to store both the loop variable i and the file descriptor from accept() for talking to the client. It then pthread_create()s a child to handle the client and gives it the malloc()ed memory. All of the threads except the last (i == (NUM_CLIENTS_TO_SERVE-1)) should be detached threads. The last thread should be an ordinary, joinable thread, which the parent thread should join with outside the loop.
The child thread should:
Get the thread id (coming in as the loop variable) and file descriptor. It then free()s that malloc()ed memory.
Gets the letter from the client
Attempts to open the current directory ("."). If it cannot open that directory then it:
sends CANT_READ_DIR_CODE back to the client (in network endian),
prints its id and "id num: Cannot read directory ",
returns NULL.
Iterates thru the directory to looking for a file (not a directory or anything else) whose name starts with the letter obtained from the client.
If the server finds no matching file then it
sends NO_MATCH_CODE back to the client (in network endian),
prints its id and "id num: No matching file ",
returns NULL.
Attempts to open the file for reading. If it cannot open the file then it:
sends CANT_READ_FILE_CODE back to the client (in network endian),
prints its id and "id num: Cannot read file <filename> ",
returns NULL.
Prints its id and "id num: Sending <filename>, <numBytes> bytes "
Sends the size of the file as a uint32_t integer to the client (in network endian)
Sends the bytes of the file to the client. It should send the file in buffers of bytes of size BUFFER_LEN.
close()s what it should close.
returns NULL.
Code:
Sample output:
To properly test this program I made a directory called Dir:
I also made a file that I did have permission to read called holdYourHeadUp.txt, and one that I did not called youCantOpenMe.txt. The contents of the second file can be whatever you want, but to make it so you cannot open it say:
My directory now has:
Client output: Server output:$ ./server Please enter port number to monopolize [1025-65535]: 2000
$ ./client Machine name [localhost]? (I just pressed enter) Port number? 2000 Please enter a letter to look for: A No matching file found for ANo file begins with A:
0: No matching file
$ ./client Machine name [localhost]? (I just pressed enter) Port number? 2000 Please enter a letter to look for: D No matching file found for DDir begins with D, but it is not a file.
1: No matching file
$ ./client Machine name [localhost]? (I just pressed enter) Port number? 2000 Please enter a letter to look for: y Matching file found for y, but could not openThe server cannot open youCantOpenMe.txt.
2: Cannot read file youCantOpenMe.txt
$ ./client Machine name [localhost]? (I just pressed enter) Port number? 2000 Please enter a letter to look for: h The file that matches h has size 437 And if it's bad Don't let it get you down, you can take it And if it hurts Don't let them see you cry, you can make it Hold your head up, woman Hold your head up, woman Hold your head up, woman Hold your head high Hold your head up, woman Hold your head up, woman Hold your head up, woman Hold your head high And if they stare Just let them burn their eyes on you moving And if they shout Don't let it change a thing that you're doingThe server can open holdYourHeadUp.txt, so it sends it to the client.
3: Sending holdYourHeadUp.txt, 437 bytes And if it's bad Don't let it get you down, you can take it And if it hurts Don't let them see you cry, you can make it Hold your head up, woman Hold your head up, woman Hold your head up, woman Hold your head high Hold your head up, woman Hold your head up, woman Hold your head up, woman Hold your head high And if they stare Just let them burn their eyes on you moving And if they shout Don't let it change a thing that you're doing
Explanation / Answer
#include "getFileByFirstLetter.h"
#include <signal.h> // For sigaction()
#include <sys/wait.h> // For waitpid()
#include <dirent.h> // For opendir(), readdir(), closedir()
#include <unistd.h> // Had to include for R_OK permission
const int LO_LEGAL_PORT = 1025;
const int HI_LEGAL_PORT = 65535;
const int ERROR_FD = -1;
const int NUM_CLIENTS_TO_SERVE = 4;
typedef int BOOL;
// PURPOSE: To get zombie children. Ignores 'sig' (which will be 'SIGCHLD').
// No return value.
void sigChildHandler(int sig) {
// I. Application validity check:
// II. Get all zombie children:
// YOUR CODE HERE
int status;
pid_t pid;
while ( (pid = waitpid(-1, &status, WNOHANG)) > 0) printf("Zombie child %d finished. ", pid);
return;
// III. Finished:
}
// PURPOSE: To attempt to create and return a file-descriptor for listening
// to the OS telling this server when a client process has connect()-ed
// to 'port'. Returns that file-descriptor, or 'ERROR_FD' on failure.
int getServerFileDescriptor(int port, const char* progName) {
// I. Application validity check:
if (progName == NULL) {
fprintf(stderr,"Error: NULL ptr to file descriptor. ");
exit(EXIT_FAILURE);
}
// II. Attempt to get socket file descriptor and bind it to 'port':
// YOUR CODE HERE
int socketDesc = socket(AF_INET, /*AF_INET domain*/ SOCK_STREAM, /*Reliable TCP*/ 0); //Create socket
struct sockaddr_in socketInf; //Make sockaddr_in struct
memset(&socketInf,'',sizeof(socketInf)); //Populate with zeroes
socketInf.sin_family = AF_INET; //Using TCP/IP
socketInf.sin_port = htons(port); //Let port know in network endian
socketInf.sin_addr.s_addr = INADDR_ANY; //Allow machine to connect
int status = bind(socketDesc, (struct sockaddr*)&socketInf, sizeof(socketInf)); //Bind socket with port
if (status < 0) {
fprintf(stderr,"Error: Could not bind to port %d ",port);
exit(EXIT_FAILURE);
}
listen(socketDesc, 5);
// III. Finished:
return(socketDesc);
}
// PURPOSE: To install 'sigChildHandler()' as the signal simple handler for
// the 'SIGCHLD' signal. Tells OS to restarts system calls if receives
// 'SIGCHLD'.
void installSigChildHandler() {
// I. Application validity check:
// II. Install 'sigChildHandler()' as the 'SIGCHLD' handler:
// YOUR CODE HERE
struct sigaction actn; //Make new action struct
memset(&actn,'',sizeof(struct sigaction));
sigemptyset(&actn.sa_mask);
actn.sa_flags = SA_NOCLDSTOP | SA_RESTART;;
actn.sa_handler = sigChildHandler;
sigaction(SIGCHLD,&actn,NULL); //Install 'sigChildHandler()' as 'SIGCHLD' handler
// III. Finished:
}
// PURPOSE: To ask the user which port to attempt to monopolize, and to return
// entered port number.
int getPort() {
// I. Application validity check:
// II. Get port number
int port;
do {
char buffer[BUFFER_LEN];
printf("Please enter port number to monopolize [%d-%d]: ", LO_LEGAL_PORT, HI_LEGAL_PORT);
fgets(buffer, BUFFER_LEN, stdin);
port = strtol(buffer, NULL, 10);
} while ((port < LO_LEGAL_PORT) || (port > HI_LEGAL_PORT));
// III. Finished:
return (port);
}
// PURPOSE: To do the work of handling the client. Communication with the
// client take place using file-descriptor 'fd'. No return value.
void handleClient(int fd) {
// I. Application validity check:
if (fd < 0) {
fprintf(stderr, "Error: Illegal file descriptor. ");
exit(EXIT_FAILURE);
}
// II. Handle the client:
// YOUR CODE HERE
DIR* dir = NULL; //Create directory
FILE* fPtr = NULL; //Create file pointer
struct dirent *dirEntry = NULL; //Create directory entry
char buffer[BUFFER_LEN + 1]; //Create buffer
uint32_t fSize = 0;
uint32_t response = 0;
struct stat fStat;
BOOL foundFile = 0;
memset(buffer, 0, sizeof(buffer));
memset(&fStat, 0, sizeof(fStat));
printf(" ");
read(fd, buffer, 1); //Get char from client
dir = opendir(".");
if(NULL == dir) {
response = htonl(CANT_READ_DIR_CODE); //Send CANT_READ_DIR_CODE back to client
write(fd, &response, sizeof(response));
printf("Cannot read directory. ");
return;
}
BOOL regFile = 0;
while((dirEntry = readdir(dir)) != NULL ) { //Read directory
stat(dirEntry->d_name, &fStat);
if(dirEntry->d_name[0] == buffer[0]) {
foundFile = 1;
if(S_ISREG(fStat.st_mode)) {
regFile = 1;
break;
}
}
}
if(foundFile) { //Check file permissions
if(regFile) {
foundFile = 0;
int readPerm = access(dirEntry->d_name, R_OK);
if(readPerm == -1) {
response = htonl(CANT_READ_FILE_CODE); //Send CANT_READ_FILE_CODE to client
write(fd, &response, sizeof(response));
printf("Cannot read file %s .", dirEntry->d_name);
return;
} else foundFile = 1;
} else {
response = htonl(CANT_READ_DIR_CODE); //Send CANT_READ_DIR_CODE back to client
write(fd, &response, sizeof(response));
printf("%c is the right character but it's not a file. ",buffer[0]);
printf("No matching file. ");
return;
}
} else {
response = htonl(NO_MATCH_CODE); //Send NO_MATCH_CODE to client
write(fd, &response, sizeof(response));
printf("No file begins with %c: ",buffer[0]);
printf("No matching file. ");
return;
}
if(foundFile) { //All good, send file to client
uint32_t ffd = -1;
fPtr = fopen(dirEntry->d_name, "r"); //Find file in current directory
ffd = fileno(fPtr);
fseek(fPtr, 0L, SEEK_END);
fSize = ftell(fPtr);
fclose(fPtr);
fPtr = fopen(dirEntry->d_name, "r");
ffd = fileno(fPtr);
printf("Opening %s and sending to client. ",dirEntry->d_name);
printf("Sending %d bytes to %s. ", fSize, dirEntry->d_name);
fSize = htonl(fSize);
write(fd, &fSize, sizeof(fSize));
fSize = ntohl(fSize);
uint32_t totalBytesSent = 0;
int numBytesRead = 0;
int writtenBytes = 0;
while(totalBytesSent < fSize) {
writtenBytes = 0;
numBytesRead = read(ffd, buffer, BUFFER_LEN);
while(writtenBytes < numBytesRead) writtenBytes += write(fd, buffer + writtenBytes, numBytesRead - writtenBytes);
buffer[writtenBytes] = '';
printf("%s", buffer);
totalBytesSent += numBytesRead;
}
}
// III. Finished:
}
// PURPOSE: To serve the clients using file-descriptor 'listenFd' to tell
// when a client has connected. Each client is handled by its own child
// process. Both the parent and the child processes close unnecesary
// file-descriptorors. Serves 'NUM_CLIENTS_TO_SERVE' clients, then quits.
// No return value.
void doServer(int listenFd) {
// I. Application validity check:
if (listenFd < 0) {
fprintf(stderr, "Error: Illegal file descriptor. ");
exit(EXIT_FAILURE);
}
// II. Do the work of the server:
listen(listenFd, NUM_CLIENTS_TO_SERVE);
int i, clientLen;
pid_t zombieChildProcess;
int clientFd;
for (i = 0; i < NUM_CLIENTS_TO_SERVE; i++) {
// YOUR CODE HERE
if ((clientFd = accept(listenFd, NULL, NULL)) < 0) {
if (errno == EINTR) continue;
else perror("Could not accept request from client. ");
} else {
if ((zombieChildProcess = fork()) == 0) {
handleClient(clientFd);
close(clientFd);
exit(0);
}
close(clientFd);
}
}
// III. Finished:
}
// PURPOSE: To oversee the main work of the server. Ignores 'argc' but
// uses 'argv[0]' as the name of the program. Returns 'EXIT_SUCCESS' to
// OS on success or 'EXIT_FAILURE' otherwise.
int main(int argc, char* argv[]) {
// I. Application validity check:
// II. Do server:
int port = getPort();
int socketFd = getServerFileDescriptor(port, argv[0]);
installSigChildHandler();
doServer(socketFd);
printf(" ");
// III. Finished:
return (EXIT_SUCCESS);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.