簡體   English   中英

C文件傳輸套接字

[英]C file transfer socket

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

'}'標記之前的語法錯誤
因早先的錯誤而感到困惑,紓困

我不知道該怎么辦......你能幫助我嗎? 我仍然是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

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

do...while塊之后你忘記了一個分號

更改

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

有2個錯誤。

  1. while (off < rval)應替換為while (off < rval);
  2. 你缺少一個while對於第一條語句do

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM