Starting from:

$30

NWEN241-Assignment 5 Network Server Program Solved

In this assignment, you will implement a network server program given a set of specifications and guidelines which are outlined in the tasks. Unlike the past four assignments, code framework and test codes will not be provided. You are free to design the program in any way you want and implement it in either pure C or C++.

Program Specifications

Upon execution, the program should perform the following:

1.   Load the CSV database file scifi.csv which contains the 25 greatest science fiction films of all time. You can use any data structure for holding the records in memory. Note that scifi.csv is provided under the files directory.

2.   Perform required socket operations to create and bind a socket.

3.   Listen for clients at TCP port 12345. (During testing, you may need to use a different port number to avoid conflicts with other students running tests in the same server.)

4.   When a client successfully establishes connection, send a message with contents HELLO.

5.   Wait for a message from the client.

6.   Perform the following based on the received message from the client:

(a)    If the message has the contents BYE (case-insensitive), close the connection to the client and go back to step 3.

(b)    If the message has the contents GET (case-insensitive), send a message back to the client containing the entire database. You may format the content in any way you want, but all the fields of every record must be printed. Go back to step 5.

(c)    If the message has the contents GET (case-insensitive) followed by a number (example: GET 12), treat the number as the row number of the record to be fetched. That is, send a message back to the client containing the record at the specified row number. You may format the content in any way you want, but all the fields of the record must be printed. If the specified row number does not exist, send back a message containing ERROR. Go back to step 5.

Testing

As you are using the Linux socket() and fork() system calls, you will need to test your program in any of the computers inside CO246. Alternatively, you can perform remote testing following the procedures in https:

Assignment 5

//ecs.victoria.ac.nz/foswiki/pub/Courses/NWEN241_2019T1/ LectureSchedule/NWEN241-Week02-Lab.pdf. For remote testing you can connect to any of the following Linux-based ECS servers:

•    barretts.ecs.vuw.ac.nz • embassy.ecs.vuw.ac.nz

•    greta-pt.ecs.vuw.ac.nz

•    regent.ecs.vuw.ac.nz

You do not need to write a client program to test your server program. You can use the Linux program nc to receive and send messages to your server program. To do this (assuming you are in CO246):

1.   Open a terminal. Compile and run your program.

2.   Open another terminal. Run nc localhost 12345 to establish a connection with your server program. You may need to replace 12345 with the actual port number that you specified in your code. Once nc is connected to the server program: whatever you type in this terminal will be sent to the server program, and whatever is sent by the server program will shown in this terminal.

If you are performing the test remotely, you will need to open 2 ssh/putty connections to the same server machine. In one ssh/putty terminal, you compile and run your program. In the other ssh/putty terminal, you run nc localhost 12345 to establish a connection with your server program. You may need to replace 12345 with the actual port number that you specified in your code.

Task 1.



Basics

Design the header file(s) to be used by your program. You can use any name for your header file(s), but their extension must either be .h (for pure C) or .hh for (C++).

Task 2.



Completion

Provide an implementation (C/C++ source files) of the server program as specified in the Program Specifications above. You can use any name for your source file(s), but their extension must either be .c (for pure C) or .cc for (C++).

Task 3.



Completion

Provide a Makefile to automatically build your program. The Makefile should have at least 2 rules:

1.   dbserver: This rule should compile the server program that you have implemented in Task 2.

2.   clean: This rule should delete any compiled file (object and executable files).

Important: If you opt not to perform this task, you must provide instructions on how to compile your code. Write these instructions in a text file named README.

Task 4.



Challenge

One of the drawbacks of the server program specified in the Program Specifications above is that when a client is connected with the server, no other client can connect to the server.

Provide an enhancement of the server program by using the fork() system call as follows: At step 4, when a client successfully establishes connection, fork a child process. Upon successful fork, the parent process should go back to step 3. Whereas, the child process should perform the following:

1.   Send a message to the client with contents HELLO.

2.   Wait for a message from the client.

3.   Perform the following based on the received message from the client:

(a)    If the message has the contents BYE (case-insensitive), close the connection to the client and exit the process.

(b)    If the message has the contents GET (case-insensitive), send a message back to the client containing the entire database. You may format the content in any way you want, but all the fields of every record must be printed. Go back to Step 2.

(c)    If the message has the contents GET (case-insensitive) followed by a number (example: GET 12), treat the number as the row number of the record to be fetched. That is, send a message back to the client containing the record at the specified row number. You may format the content in any way you want, but all the fields of the record must be printed. If the specified row number does not exist, send back a message containing ERROR. Go back to Step 2.

If the fork is not successful, the process should perform steps 4 and 5 (in the original Program Specifications).

More products