In this assignment you will write a client for a simple file downloader client. I suggest you first start by using the server "by hand" -- that is, communicating with it using the nc command line utility so that you can get a feel for the protocol by typing the commands.

Protocol Specification
All server commands start with an uppercase command followed by arguments (if necessary) and are terminated by a newline (n). All responses from the server are a single line that either start with "+OK" or "-ERR". The commands are:

Returns a list of the files available to download.

Responds with: "+OK" followed by a list of the files and their sizes, one per line. The list is terminated with a period on a line by itself.

SIZE filename
Get the size of a file.

Responds with: "+OK" followed by the size of a file (in bytes) or "-ERR" if there is no such file.

GET filename
Retrieve a file.

Responds with: "+OK" followed by a newline, followed by the contents of the file. You can use the size of the file (obtained using the SIZE command) to determine when to stop reading from the network.

May respond with "-ERR" if the file does not exist.

Disconnect from the server.

Responds with "+OK" and a disconnect.

Example Session
$ nc 1234
+OK Greetings
43179592 firefox-42.exe
39307209 huge.txt
61643 madness.txt
53357063 rockyou.txt.gz
157467 sclibrary.jpg
48 small.txt
620 sonnet.txt
0 zero.txt
SIZE small.txt
+OK 48
GET small.txt
This is a small file. It is only one line.
+OK Goodbye

The Download Client
Write a C program that implements the file downloading protocol described above. It connects to the server and displays a menu:

L) List files
D) Download a file
Q) Quit

If the user types an L, a list of available files is shown. At minimum you can simply display to the user what the server sends. Preferably, you should make the output "pretty" by, say, omitting the file sizes or converting them to a more human-friendly format such as kilobytes (KB).

If the users types D, the program should prompt for the name of a file to download, then download that file and save it to the user's folder.

Q will obviously quit the program.


Add an "A" option to download all files automatically.
Display the list of filenames along with a number and allow the user to select a file by number instead of filename.
If the file to be downloaded already exists in the user's directory, ask the user if they want to overwrite it.
Display a progress bar as the file is downloading.

You can monitor the file server by opening a link in a new tab.

Every 5 seconds it will refresh. You can see the commands that you (and your classmates) are sending to the server.

Starter Code
#include <stdio.h>
#include <stdlib.h>

FILE * connect();
void menu();
char get_choice();
void list_files(FILE *s);
void download(FILE *s);
void quit(FILE *s);

int main()
    // Connect
    FILE * s = connect();
    // Menu
    // Get choice
    char choice = get_choice();
    // Handle choice
       case 'l':
       case 'L':
       case 'd':
       case 'D':
       case 'q':
       case 'Q':
            printf("Choice must be d, l, or qn");

FILE * connect()
    return NULL;

void menu()
    printf("List filesn");
    printf("Download a filen");

char get_choice()
    printf("Your choice: ");
    char buf[100];
    fgets(buf, 100, stdin);
    return buf[0];

void list_files(FILE *s)

void download(FILE *s)

void quit(FILE *s)

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define MESS_SIZE 512

* show error message
* @param msg
void error(const char *msg);

* send massage to server
* @param cmd command
* @param fd file descriptor
void sendMessage(char * cmd, int fd);

* get message from server
* @param fd file descriptor
* @param size size of file
* @param asterisk show progress bar
* @return received message
char * getMessage(int fd, int size, int asterisk);

* split string by delimiter
* @param cmd string to be split
* @param size size of split array
* @param delim delimiter
* @return array of split pointers
char ** split(char * cmd, int * size, char * delim);

* connect to server to get size of file
* @param fd file descriptor
* @param filename name of file
* @return size of the file
int size_file(int fd, char * filename);

* get file from server then write it to local directory
* @param fd file descriptor
* @param size size of the file
* @param name name of the file
* @param asterisk progress bar
void getFile(int fd, int size, char * name, int asterisk);

* get all file from server
* @param s
void getAll(FILE *s);...

By purchasing this solution you'll be able to access the following files:

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats