1

So, I'm a relative newbie to network programming and programming in general, and I'm attempting to write a simple client/server text file transfer service. My code asks for the user to choose an upload or download option. When the user selects upload, the new file is created on the server's end, but isn't written with the data until the socket is closed. Also the string "upload" is appended onto the end of the text in the file.

I can't seem to find where my errors are, so any help would be greatly appreciated!

server.cpp

#define SIZE 1024

void write_file(int sockfd)           // writing data to file function
{
    int n;
    FILE *fp;
    char const *filename = "recv.txt";
    char buffer[SIZE];
    fp = fopen(filename, "w");
    
    while (1)
    {
        n = recv(sockfd, buffer, SIZE, 0);

        if (n <= 0)
        {
            break;
        }
        fprintf(fp, "%s", buffer);
        bzero(buffer, SIZE);
    }
    fclose(fp);
    return;
}


// in main
char msgRecv[10];
    
int n = 10;
    
while (n > 0)
    {
        rcv = read(connected_sd, &msgRecv, 10);
        
        n -= rcv;
    }
    
char msgUpload[10] = "upload";

if(strcmp(msgUpload, msgRecv) == 0)
    {
        write_file(connected_sd);
    }

client.cpp

void send_file(FILE *points, int sockfd)          // sending file through socket function
{
    char bytes[SIZE] = {0};
    bzero(bytes, SIZE);

    while(fgets(bytes, SIZE, points) != NULL)
    {
        if(send(sockfd, bytes, sizeof(bytes), 0) == -1)
        {
            perror("Error in sending file.");
            exit(1);
        }
        
        bzero(bytes, SIZE);
    }
}


// in main

char msgUpload[10] = "upload";
            
send(sd, msgUpload, sizeof(msgUpload), 0);

string fileN;
cout << "What is the name of the file you wish to upload?\n";
cin >> fileN;
bzero(msgUpload, sizeof(msgUpload));

FILE *file;
char const *filename = fileN.c_str();
file = fopen(filename, "r");
if (file == NULL)
   {
       perror("Error in reading file.\n");
       exit(1);
   }
            
send_file(file, sd);
printf("File data sent successfully.\n\n");
fclose(file);
christ_
  • 23
  • 4
  • 1
    Hi, just to make clear for me: The problem is, that the file is only written by the server when the socket closes and that the text "upload" is appended to the file? If so: The file is only written by the server, because recv() is a blocking call and when using TCP (stream) returns 0 when the connection drops for example. Thus your call for fprintf() only gets called then because the recv() is blocking when there is nothing more to receive but the connection is still there. You should call fflush() to write the buffer immediately. – luv4bytes Oct 12 '20 at 11:52
  • @luv4bytes awesome! That took care of the writing of the residual data in the socket. So my ultimate goal is to have the program be able to take multiple calls from the client consecutively, without closing the socket. Is this possible or should I just write a loop and have it reopen every call? – christ_ Oct 12 '20 at 20:20
  • 1
    I'm not really sure what you mean with "reopen every call", but if we assume you want to implement a TCP server that listens for connections and writes recieved data to a file there are several things you need to consider and need to implement. For example how to tell when a message has actually ended, how long it is etc., meaning implementing some kind of protocol, that tells you this. This should be done due to the nature of how TCP works. https://stackoverflow.com/questions/30655002/socket-programming-recv-is-not-receiving-data-correctly Hope this points you in the right direction! – luv4bytes Oct 13 '20 at 11:02

0 Answers0