Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C, now that you learned how to use sockets API, it's time to implement a real mdb-lookup-server that does not rely on netcat tool.

In C, now that you learned how to use sockets API, it's time to implement a real mdb-lookup-server that does not rely on netcat tool.

Your job is to write mdb-lookup-server.c, a program that does the same thing as mdb-lookup.c from lab 4, except that it communicates with the client via a socket rather than standard I/O streams.

You start the mdb-lookup-server as follows:

$ ./mdb-lookup-server my-mdb 8888

where my-mdb is the database file name and 8888 is the port number that server should listen on. You can use netcat to connect to the server and use the lookup service.

For this part, it's okay for the server to handle only a single client at a time, so you don't need to fork().

This part is an exercise in combining existing code to make something new. There is actually very little code you need to write. You can basically combine tcp-recver.c that we studied in class and mdb-lookup.c from lab 4 solution. Here is how:

- Every time you get a socket from accept(), you basically run the code for mdb-lookup. That is, you open the database file, read all records into memory, run the lookup loop, and finally clean up resources when the client is done using the lookup service. When you're done with mdb-lookup code, you will go back to accept() in order to service the next client.

The main thing you need to do is to convert the mdb-lookup code to read and write using the socket rather than stdin and stdout.

- For reading, the easiest thing to do is to wrap the socket file descriptor with a FILE*, so that we can leave the fgets() code alone. fdopen() does exactly that:

FILE *input = fdopen(socket_descriptor, "r");

will give you a FILE* that you can use in place of stdin.

When you're done, you should fclose(input), which will close the underlying socket for you.

- For output, you need to use send() instead of printf(). Here, the easiest thing to do is to use snprintf() to first print to a char array and then send() what you just printed. Note that, on success, snprintf() returns the number of characters printed.

We will use this mdb-lookup-server in lab 7, where we write a web server which can not only serve static HTML pages, but also serve mdb-lookup results to web browsers. In order to use mdb-lookup-server in conjunction with a web server in lab 7, we need to do two things when we write mdb-lookup-server now:

- Do NOT send the "lookup:" prompt to the client. Simply read the input line (which contains the search word) from the socket, send all the search result lines to the socket, and finally send a blank line to the socket. The blank line at the end is important: the web server will use it to determine the end of a search result.

- Add the following lines in the beginning of your main():

// ignore SIGPIPE so that we don't terminate when we call // send() on a disconnected socket. if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) die("signal() failed");

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

How To Make A Database In Historical Studies

Authors: Tiago Luis Gil

1st Edition

3030782409, 978-3030782405

More Books

Students also viewed these Databases questions