0

my problem is that when trying to send a .txt file from the server to the client, when the client receives the file only the first line of code in the txt is shown when in fact the entire file has to be shown, does anyone know how can i fix this problem?

Server

        FILE * Log = fopen("Log.txt", "r");
        while(!feof(Log)){
            char ms[T_MAX];
            fgets(ms, T_MAX, Log);
            if(send(client_socket, ms, T_MAX, 0) < 0){
                printf("\n[SERVER] ERROR -> %d\n",WSAGetLastError());
                            closesocket(servidor_socket);
                            closesocket(client_socket);
                            WSACleanup();
            return(1);
            }
        }
        fclose(Log);
this is what the server reads and would have to send to the client.
**
2022-10-24-17:18 :=============================
2022-10-24-17:19 :=======Example=======
2022-10-24-17:20 :=============================
**


Client


        if(recv(socket_aplication, ms, T_MAX, 0) <= 0){
                printf(" ERROR -> %d\n",WSAGetLastError());
                            closesocket(socket_aplication);
                            WSACleanup();
            return (1); 
        }
            //here we show in the client console the result of what the server gave us
            printf(ms);

this is what the client console shows
**
2022-10-24-17:18 :=============================
**
  • 2
    [Why `while(!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Oct 25 '22 at 20:15
  • 1
    You have to account for `send()` and `recv()` not processing the entire length of data in one call. – Shawn Oct 25 '22 at 20:18
  • 1
    Don't assume you can read everything in one go. You need to read from the socket repeatedly until it's complete, somehow. For that, you should at least incorporate a simple protocol that can be as simple as first sending a `size` field, followed by the contents of the file. Ideally you would also incorporate a checksum to check the integrity. – Cheatah Oct 25 '22 at 20:18
  • 1
    Also, don't send `T_MAX` characters. You are sending strings so don't send more than you read from the file. `strlen(ms)`, possibly `+1` if you want to send the terminating `\0` character too. – Ted Lyngmo Oct 25 '22 at 20:22

0 Answers0