0

I'm learning some examples about C++ socket. One of the code here has an error : "expect token while got fclose" at the line above the last line

The code seems fine with me, so I can't figure out what is wrong here.

Any ideas are appreciated.

void RecvFile(int sock, const char* filename) 
{ 
    int rval; 
    char buf[0x1000]; 
    FILE *file = fopen(filename, "wb"); 
    if (!file)
    {
        printf("Can't open file for writing");
        return;
    }

    do
    {
        rval = recv(sock, buf, sizeof(buf), 0);
        if (rval < 0)
        {
            // if the socket is non-blocking, then check
            // the socket error for WSAEWOULDBLOCK/EAGAIN
            // (depending on platform) and if true then
            // use select() to wait for a small period of
            // time to see if the socket becomes readable
            // again before failing the transfer...

            printf("Can't read from socket");
            fclose(file);
            return;
        }

        if (rval == 0)
            break;

        int off = 0;
        do
        {
            int written = fwrite(&buf[off], 1, rval - off, file);
            if (written < 1)
            {
                printf("Can't write to file");
                fclose(file);
                return;
            }

            off += written;
        }
        while (off < rval);
    } 

    fclose(file); 
} 

3 Answers 3

3

You have a do with no corresponding while:

do
{
    // ...
    do
    {
        // ...
    }
    while (off < rval);
} 
// No while here

fclose(file); 

It appears that it should just be while (true), which you might as well just stick at the top, instead of doing a do while. Execution will break from the loop if recv returns 0 or less, which indicate an orderly shutdown and an error respectively. So change it to:

while (true)
{
    // ...
    do
    {
        // ...
    }
    while (off < rval);
}

fclose(file); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, your answer is helpful. Voted!
2

You have a do statement without a corresponding while:

do // <== THERE IS NO CORRESPONDING while FOR THIS do
{
    rval = recv(sock, buf, sizeof(buf), 0);
    if (rval < 0)
    {
        // ...
    }

    // ...

    do
    {
        // ...
    }
    while (off < rval); // <== This is OK: the "do" has a matching "while"
}
// Nothing here! Should have a "while (condition)"

If you just want to repeat your loop indefinitely, then you should use while (true) - either replacing the do keyword (preferably), or adding it where the missing while should go (as indicated by the above comments).

Comments

1

You started a do without actually supplying a while();

do
{
    rval = recv(sock, buf, sizeof(buf), 0);
    if (rval < 0)
    {
        // if the socket is non-blocking, then check
        // the socket error for WSAEWOULDBLOCK/EAGAIN
        // (depending on platform) and if true then
        // use select() to wait for a small period of
        // time to see if the socket becomes readable
        // again before failing the transfer...

        printf("Can't read from socket");
        fclose(file);
        return;
    }

    if (rval == 0)
        break;

    int off = 0;
    do
    {
        int written = fwrite(&buf[off], 1, rval - off, file);
        if (written < 1)
        {
            printf("Can't write to file");
            fclose(file);
            return;
        }

        off += written;
    }
    while (off < rval);
} //while() Needs to go here

fclose(file); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.