0

I need to transfer .txt file using sockets. I try this:

Server:

#define CHUNKSIZE 1024
FILE* fd = fopen("download.txt", "rb");
            size_t rret, wret;
            int bytes_read;
            char buffer[CHUNKSIZE];
            while (!feof(fd)) {
                if ((bytes_read = fread(&buffer, 1, sizeof(buffer), fd)) > 0)
                    send(Connections[index], buffer, bytes_read, 0);
                else
                    break;
            }
            fclose(fd);

Client:

char text[1024];

size_t datasize;
    FILE* fd = fopen("download.txt", "wb");
    while (true)
    {
        datasize = recv(Connection, (char*)&text, sizeof(text), NULL);
        fwrite(&text, 1, datasize, fd);
    }
    fclose(fd);

But the client does not write anything to download.txt What could be problem? (I am not pasting the full code because sending messages (recv, send) works well)

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 4
    Please read [Why is “while (!feof(file))” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – Some programmer dude Aug 05 '19 at 10:59
  • You should also add some error checking. And please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve]. – Some programmer dude Aug 05 '19 at 11:00
  • For an array, using the address-of operator is almost *always* wrong. The type of e.g. `&text` is `char (*)[1024]`, not the `char *` that is expected and which you get from `&text[0]` (which is what plain `text`, without address-of operator, decays to). While it shouldn't matter in most cases (`&text` and `&text[0]` points to the very same location) the types and therefore the semantics is wrong. – Some programmer dude Aug 05 '19 at 11:06
  • @VTT The code as posted, while perhaps confusing, does **NOT** misuse `while ( !feof( fd ) )...`. The comparison of `bytes_read > 0` will cause the loop to be broken when EOF is reached. Voting to reopen. The problem is elsewhere. – Andrew Henle Aug 05 '19 at 11:15
  • @Someprogrammerdude The `while ( !feof() )` in this question is extraneous and not incorrect. As currently coded, `while ( 1 )` would probably be better. – Andrew Henle Aug 05 '19 at 11:16
  • The question is incomplete. From what's posted, there's no way to know what the problem is. Please post [a minimal, complete, and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). Note also your client code's `while (true)` loop is an infinite loop that will never exit. – Andrew Henle Aug 05 '19 at 11:21
  • Have you considered testing for end of stream? Or error? – user207421 Aug 05 '19 at 11:24
  • The loop in your client code does not have a condition to terminate, so it will never reach `fclose(fd);` In this case your data might not get actually written to the file. You might see an empty file if all the data is still in a buffer in RAM. BTW: Using the variable name `fd` for a `FILE*` is misleading because `fd` is commonly used for "file descriptor" as returned by `open`. – Bodo Aug 05 '19 at 11:36

0 Answers0