Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please finish the server of a client-server application. The client asks the use

ID: 3842259 • Letter: P

Question

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 A            
No 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 D            
Dir 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 open            
The 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 doing            
The 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

server_getFileByFirstLetter.c

===========================================================================

client_getFileByFirstLetter.c

===============================================================================

getFileByFirstLetter.h

============================================================================