C文件传输套接字

时间:2013-03-03 14:47:24

标签: c sockets file-transfer

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语法错误   被早先的错误弄糊涂了,拯救

我不知道该怎么办......你能帮帮我吗? 我仍然是c编程的新手。 我在代码中插入了错误行。 我无法理解的是为什么这个错误发生了......你们能解释一下为什么吗?

4 个答案:

答案 0 :(得分:2)

实际上你在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

答案 1 :(得分:0)

在第175行,你应该有一个while语句,因为你在开头有一个相应的do语句。您的代码中的另一个do-while语句也缺少分号。

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 //******Where is the while for this do statement?*****
{
    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) // *** A semicolon is needed here ***
} //line 175

fclose(file); 
}

答案 2 :(得分:0)

do...while阻止

之后忘记了分号

更改

while (off < rval)while (off < rval);

答案 3 :(得分:0)

有2个错误。

  1. while (off < rval)应替换为while (off < rval);
  2. 您缺少第一个while
  3. do声明