Client and Server code that sends contents of a file.

This is a discussion on Client and Server code that sends contents of a file. within the C Programming forums, part of the General Programming Boards category; I am working on Experiment 5.3 from the book Hands-On Networking with Internet Technologies. We have to modify client and ...

Client and Server code that sends contents of a file.

I am working on Experiment 5.3 from the book Hands-On Networking with Internet Technologies. We have to modify client and server files. We want these files to form a connection, then the client will input a file that is wants from the server. This file is sent to the server, and the server sends what is stored within that file to the client. The client stores it in a file of the same name and prints the contents of the file to the terminal. The two files below are run with other files that were provided from the book, but only the files below need to be modified. Any help is appreciated or useful links to point me in the right direction. I am stuck on the send function line towards the end of fileserver.c.
Thanks!!!

Don't ignore any return values from your sockets or IO function calls. That means don't cast the return value to (void) for send, or anything else. Read the documentation for such functions (Google helps here, as do the man pages). If the functions fail, print a sensible error messages using perror() or strerror() & errno. This is immensely helpful for debugging.

Also note, it is conventional to return 0 from main to indicate success, or EXIT_SUCCESS. 1 typically means failure.

In the server:
send() doesn't magically read the contents of a FILE * and write them to the socket, just like printf(fp), would not print out the contents of the file referred to by fp. What your code does is send the contents of the fp variable (the details of the FILE struct used to maintain info about the file, not the file contents) over the socket. It's useless on the other end. Furthermore, you send len bytes, but len is uninitialized, so you may very well be asking it to send gigabytes of data, certainly an issue. You need to read from fp in a loop and send the data you read each time over the socket. Since you opened the file in text mode, I would use fgets to read a line into a buffer, then pass that buffer to send(), along with the length to write (use strlen). Examples of that are all over the web.

In the client:
Since the client must store the file, it must write the data it got from the server. Therefore, you should open the file in write mode: fopen("log.txt", "w").

Oh, and those "//Added" comments really make your code annoying to read, especially since they don't line up well. I would remove them all, but if you must keep them, make sure they line up on the // part, and keep the spacing (1-2 spaces should be good) between the // and the Added the same everywhere.

That's what I got at a quick glance. There's probably some more stuff, but that should keep you going for a while.

Thanks for your help!! However I am still struggling to get it to work. Now my biggest issue is keeping the connection open after I enter the file name in the client. See code below.

Code:

/* make a connection to the fileserver */
conn = make_contact(comp, (appnum) atoi(argv[2]));
if (conn < 0)
exit(1);
(void) printf("Chat Connection Established.\n");
/* Input File Name to use */
printf ("Enter The name of the file to retrieve. \n");
len = readln( cname, MAX_CNAME); // CODE STOPS WORKING AFTER THIS LINE!?!?!?!
// OPEN THE FILE => LOAD THE CONTENT OF THE FILE IN MEMORY
fp = fopen("log.txt", "w"); //Here we have to open the file that was entered to retrieve above and len instead of log.txt does not work
if (fp == NULL){
printf("We were not able to open the file\n");
exit(0);
}

Issue 2: What needs to be changed for this line to work at the end of fileserver to send the contents of the file to the client?
(void) send(conn, fp,len, 0);

The connection should stay open so long as you don't actually close it, or change some aspect of the socket you shouldn't. It would help if you posted the complete, updated versions of your client and server code, along with any helper functions (await_contact, make_contact, etc).

What do you mean the code "stops working" after that line? How do you know it stops working? How do you know that is the exact spot? Does it print garbage? Does it just sit there doing nothing? Does it crash? Is there any error output? You need to give a detailed description if you want detailed help.

I might be confusing multiple pieces of code that do the same thing in my client and server code. After the sever establishes a connection with the client (client then asks for the file name) the server closes its connection but the client stays open. This is my biggest concern right now. Then another concern is what code do I need (might already have it) that accepts the contents from the server and prints it to the terminal?