C文件传输套接字

C文件传输套接字

void RecvFile()
{
int rval;
char buf[0x1000];
FILE *file = fopen("C:\\pic.bmp", "wb");
if (!file)
{
    printf("Can't open file for writing");
    return;
}

do
{
    rval = recv(winsock, 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; //line 159

    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)
} //line 175

fclose(file);
}



  '}'标记之前的175个语法错误
  159由较早的错误混淆,无法使用


我不知道该怎么办...您能帮我吗?
我还是C编程的新手。
我在代码中插入了错误行。
我不明白的是为什么会发生此错误...你们能解释一下为什么吗?

最佳答案

实际上,您在while循环中缺少;

while (off < rval);   // 174 line
                  ^


第二个外循环没有了

do{

}// 175 line
while()  // this is missing ???


我不确定100%,但是我认为您需要在外部无限循环(如下所示)
阅读评论:

do{

   // rev = recv(....
   if(rev <){
    // you return from here that is reason i believe you need infinite loop
    // code
   }
   //code
   do{
       // your code
   }while (off < rval); // at like 174
}while(1); // line 175

关于c - C文件传输套接字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15186858/

10-11 06:30