C套接字文件传输错误

时间:2011-08-01 21:23:51

标签: c sockets winsock

我有一个使用Windows套接字用C编写的文件传输代码,它似乎可以正常工作但传输的文件数量少于所需的字节数。

有什么想法吗?

发送代码:

while(!feof(fp))
{
   bzero(bufferin,256);
   fread(bufferin,sizeof(char),255,fp);
   send(remsock,bufferin,255,0);

   if(n < 0)
      error("ERROR writing to socket");
}
send(remsock,"done",255,0);

接受代码:

while(1)
{
    recv(sockfd,buffer,255,0);
    if(compare(buffer,"done") == 0)
        break;  
    fwrite(buffer,1,255,fplog);
}
printf("File Transfer complete\n\n");

4 个答案:

答案 0 :(得分:3)

问题在于sendrecv的工作方式与您的预期不同。

send(remsock,bufferin,255,0); /* Write MAX 255. May write less than 255. */
recv(sockfd,buffer,255,0); /* Read MAX 255. May read less than 255. */

通常问题出在接收方。所以你应该循环阅读。

while (bytes_to_go) {
    bytes = recv(...);
    /* Check bytes. */
    bytes_to_go -= bytes;
}

答案 1 :(得分:1)

正如其他人所说,你必须查看你正在调用的所有函数的返回值,例如:

发送:

while (!feof(fp))
{
    int nRead = fread(bufferin, sizeof(char), 256, fp);
    if (nRead <= 0)
        error("ERROR reading file");

    char *pBuf = bufferin;
    while (nRead > 0)
    {
        int nSent = send(remsock, pBuf, nRead, 0);
        if (nSent == SOCKET_ERROR)
        {
            if (WSAGetLastError() != WSAEWOULDBLOCK)
                error("ERROR writing to socket");

            fd_set writefd;
            FD_ZERO(&writefd);
            FD_SET(remsock, &writefd);

            if (select(0, NULL, &writefd, NULL, NULL) != 1)
                error("ERROR waiting to write to socket");

            continue;
        }

        if (nSent == 0)
            error("DISCONNECTED writing to socket");

        pBuf += nSent;
        nRead -= nSent;
    }
}

closesocket(remsock);

接收:

while(1)
{
    int nRecv = recv(sockfd, buffer, 256, 0);
    if (nRecv == SOCKET_ERROR)
    {
        if (WSAGetLastError() != WSAEWOULDBLOCK)
            error("ERROR reading from socket");

        fd_set readfd;
        FD_ZERO(&readfd);
        FD_SET(sockfd, &readfd);

        if (select(0, &readfd, NULL, NULL, NULL) != 1)
            error("ERROR waiting for data from socket");

        continue;
    }

    if (nRecv == 0)
        break;

    char *pBuf = buffer;
    while (nRecv > 0)
    {
        int nWritten = fwrite(pBuf, sizeof(char), nRecv, fplog);
        if (nWritten <= 0)
            error("ERROR writing to file");

        pBuf += nWritten;
        nRecv -= nWritten;
    }
}

printf("File Transfer complete\n\n"); 

答案 2 :(得分:0)

查看sendfile()recv()的{​​{3}}。

答案 3 :(得分:0)

看起来这种组合有效,并为文件提供了正确的校验和。最后使用fclose()并通过捕获返回值发送正确的字节数非常重要。感谢你的回复,虽然当人们提到send和rcv函数在我的字节情况下发送最多255并且可能发送更少时,我确实有点担心。我已经进行了多次试验,并没有注意到转移问题。

发送代码:

FILE *fp;
fp = fopen("c:\\file.txt", "rb");

while(!feof(fp))
{
   bzero(buffer,256);
   n = fread(buffer,sizeof(char),255,fp);
   send(remsock,buffer,n,0);

   if(n < 0)
      error("ERROR writing to socket");
}
send(remsock,"done",255,0);
fclose(fp);

接收代码:

FILE *fp;
fp = fopen("c:\\receivedfile.txt", "wb");

while(1)
{
    n = recv(sockfd,buffer,255,0);
    if(compare(buffer,"done") == 0)
        break;  
    fwrite(buffer,1,n,fp);
}
printf("File Transfer complete\n\n");
fclose(fp);