Below is the code for our Fileclient and Fileserver. (Additional code is needed to compile these and they were downloaded/obtained from Hand-On Networking with Internet Technologies) The server opens up a connection and waits for the client to connect. When the client connects it sends a file name to the server. The server then gets the file contents and loads it into memory and sends the contents of the file to the client. On the client side the file contents is printed on the screen and stored in a file. We are trying to modify the code so the connection stays open for multiple file transfers instead of just one file exchange. The if statement doesn't keep the connection open on the server and the do while clause on the client does not help keep the connection open. Any help/tips are greatly appreciated!!

fileserver.c
Code:
#include <stdio.h>
#include <stdlib.h>                          
#include "cnaiapi.h"

#define MAX_NAME               12                 

int recvln(connection, char *, int);       
int readln(char *, int);                   

int                                        
main(int argc, char *argv[])           
{
    int        len;                 
    computer comp;                               
    connection  conn;                                
    FILE *fp;
    char ch;
        char * buffer;
        size_t result;
    char   filename[MAX_NAME];
    

    if (argc != 2) {                                                       
        (void) fprintf(stderr, "usage: %s <appnum>\n", argv[0]);       
        exit(1);                              
    }                                     


    (void) printf("Chat Server Waiting For Connection.\n");     

    /* wait for a connection from a chatclient */                

    conn = await_contact((appnum) atoi(argv[1]));           
    if (conn < 0)                           
        exit(1);                        
    
    (void) printf("Chat Connection Established.\n");

    //Received the file name from Client
    recv(conn, filename, MAX_NAME, 0);
    printf("%s \n", filename);

    if (filename != "exit"){


    // OPEN THE FILE => LOAD THE CONTENT OF THE FILE IN MEMORY
    fp = fopen(filename, "r");
    if (fp == NULL){
        printf("We were not able to open the file\n");
        exit(0);
    }
 

     // obtain file size:
      fseek (fp , 0 , SEEK_END);
      len = ftell (fp);
      rewind (fp);

      // allocate memory to contain the whole file:
      buffer = (char*) malloc (sizeof(char)*len);
      if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

      // copy the file into the buffer:
      result = fread (buffer,1,len,fp);
      if (result != len) {fputs ("Reading error",stderr); exit (3);}

    
    send(conn, buffer,len, 0);
    
    }
    fclose(fp);   
    return 1;
}
Fileclient Code
Code:
#include <stdio.h>
#include <stdlib.h>            
#include "cnaiapi.h"            


#define MAX_NAME               12
#define BUFFSIZE        256                   


int recvln(connection, char *, int);        
int readln(char *, int);            

int                                         
main(int argc, char *argv[])            
{
    FILE *fp;
    char ch;
    computer    comp;               
    connection    conn;               
    char            filename[MAX_NAME];   
    int        len, received, expect;    
    char        buff[BUFFSIZE];       

    if (argc != 3) {                                                         
        (void) fprintf(stderr, "usage: %s <compname> <appnum>\n",        
                   argv[0]);                     
        exit(1);                             
    }                                      

    /* convert the compname to binary form comp */                           

    comp = cname_to_comp(argv[1]);                         
    if (comp == -1)                                 
        exit(1);                             
                    

    /* 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"); 

    do {
    
    /* Input File Name to use */    
    printf ("Enter the file name that you would like to obtain from the server. \n");
    scanf("%s",&filename);
    //printf("%s \n", filename);

    if (filename == "exit"){
        exit(1);
    }

    //Sends the File name to Server
    send(conn, filename, MAX_NAME, 0);    
                        
    printf ("The contents of the file you want to retrieve are below: \n");                
    len = 255; //readln( cname, MAX_CNAME);                    //This line does not do anything for now sice we are only using 1 file (log.txt)

    
    /* read and print same no. of bytes from file server */  

        expect = len;
        for (received = 0; received < expect;) {
           len = recv(conn, buff, (expect - received) < BUFFSIZE ?
                 (expect - received) : BUFFSIZE, 0);
            if (len < 0) {
                send_eof(conn);
                return 1;
            }
            (void) write(STDOUT_FILENO, buff, len);
            received += len;


         fp = fopen ( filename , "w+" );
          fwrite (buff , len, sizeof(char) , fp );               //fwrite (buff , 1 , sizeof(len) , fp );
               if (fp==NULL)//{

        printf("We are not able to save.\n");
        exit(0);

        }

        (void) printf("\n");
        (void) fflush(stdout);

          fclose (fp);

        } while (1);

          return 0;
        }