【问题标题】:C file transfer socketC文件传输套接字
【发布时间】:2013-03-03 22:58:32
【问题描述】:
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 编程方面还是个新手。 我在代码中插入了错误行。 我不明白为什么会出现这个错误......你们能解释一下为什么吗?

【问题讨论】:

  • 是的,我该如何解决?我不能直接删除它可以吗?我认为这是一个错误检查...
  • 这是一种相当可怕的缩进 do-while 循环的方式...尝试将do { 或至少} while(...); 作为一行,不要拆分它们。您可能会通过不同的缩进自己发现错误...
  • 谢谢你们……现在我明白为什么了……
  • OT:存储来自read()/recv()write()/send()的结果的首选类型是ssize_t。对于fread()fwrite(),它是size_t

标签: c sockets file-transfer


【解决方案1】:

其实你在while循环中缺少;

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

第二个外循环没有while

do{

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

我不是 100% 确定但我认为你需要在外部无限循环(下面的粗略代码) 读取 cmets:

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

【讨论】:

    【解决方案2】:

    在第 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); 
    }
    

    【讨论】:

      【解决方案3】:

      您忘记了do...while 块后的分号

      改变

      while (off &lt; rval)while (off &lt; rval);

      【讨论】:

        【解决方案4】:

        有两个错误。

        1. while (off &lt; rval) 应替换为 while (off &lt; rval);
        2. 您缺少第一个 dowhile 语句

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-10-18
          • 2017-11-03
          • 1970-01-01
          • 2011-10-13
          • 2015-03-06
          • 1970-01-01
          • 1970-01-01
          • 2011-09-22
          相关资源
          最近更新 更多