1

我有一个简单的客户端-服务器应用程序,用于将文件从客户端发送到服务器。客户端以预定义大小的块(例如 512)发送文件。服务器也以相同大小的块接收文件。客户端的套接字直到服务器在接收到整个文件后发送一个 ack 才会关闭。

会发生什么:

  1. 客户端设法发送(send() 调用)整个文件,意味着它的所有块。
  2. 服务器设法仅获取(recv() 调用)第一个块,这意味着 recv() 调用在第一次调用时返回 512,而在第二次调用时它会阻塞。

这种行为的原因可能是什么?

以下是相关部分。

发送\接收功能:

int sendBuf(int sockFd, char* buf, int size){
    int bytesSent = 0;
    while (bytesSent < size){
        bytesSent += send(sockFd, buf, size, 0);
    }
    return bytesSent;
}


int sendInt(int sockFd, int num){
    uint32_t nbo_num = htonl(num);
    return sendBuf(sockFd, (char*)&nbo_num, sizeof(nbo_num));
}


int getBuf(int sockFd, char* buf, int size){
    int bytesRecv = 0;
    while (bytesRecv < size){
        bytesRecv += recv(sockFd, buf, size, 0);
    }
    return bytesRecv;
}


int getInt(int sockFd, int *num){
    int temp, bytesRecv;
    bytesRecv = getBuf(sockFd, (char*)&temp, sizeof(int));
    *num = ntohl(temp);
    return bytesRecv;
}

服务器:

printf("%s     New file was created.\n",tStr);

/* get file data */
char buf[FILE_READ_CHUNK_SIZE] = {0}; // FILE_READ_CHUNK_SIZE is the maximal read amount
int bytesWritten = 0, bytesLeft = fileSize, _size=0, chunkNo=0;
bytesRecv = 0;

while (bytesLeft > 0){
    _size = (bytesLeft > FILE_READ_CHUNK_SIZE) ? FILE_READ_CHUNK_SIZE : bytesLeft;
    bytesRecv = getBuf(newsockfd, buf, _size);
    int _bytesLeft = bytesRecv;
    bytesWritten = 0;
    while (_bytesLeft > 0){
        bytesWritten = fileInfoWrite(fileInfo, buf, _bytesLeft);
        _bytesLeft -= bytesWritten;
    }
    chunkNo++;
    printf("%s     chunk #%d: received %d bytes\n",tStr , chunkNo, bytesRecv);
    bytesLeft -= bytesRecv;
}
printf("%s   Finished getting file #%d.\n",tStr ,i+1);

/* send ack to the client */
bytesSent = sendInt(newsockfd, 1);
printf("%s   Sent the client an  ack for file #%d.\n",tStr ,i+1);

客户:

/* send the entire data of the file */
printf("  Sending file data\t\t... ");
char buf[FILE_READ_CHUNK_SIZE] = {0}; // FILE_READ_CHUNK_SIZE is the maximal read amount
int numOfChunks=0, bytesRead = 0, bytesLeft = filesArr[i]->fileSize ;
bool eof = false;
bytesSent = 1;

while (bytesLeft > 0){
    bytesRead = fileInfoRead(filesArr[i], buf, &eof);
    int _bytesLeft = bytesRead;
    while (bytesSent < _bytesLeft){
        bytesSent = sendBuf(sockFd, buf, _bytesLeft);
        _bytesLeft -= bytesSent;
    }
    //printf("    chunk #%d: sent %d bytes\n", numOfChunks+1, bytesRead);
    bytesLeft -= bytesRead;
    numOfChunks++;
}
printf("Success.\n");

/* get ack from server */
int ack=0;
bytesRecv = getInt(sockFd, &ack);
if (bytesRecv!=4 || ack!=1){
    printf("Server ack is invalid.\n");
}
printf("Finished sending file #%d.\n",i+1);
4

1 回答 1

0

你的发送代码是错误的,正常情况下它不会发送超过一个块(即一次写入一个完整的块)。这是因为您在内循环之后
没有重置。bytesSent很可能它会== FILE_READ_CHUNK_SIZE在第一个之后sendBuf,而 while 条件在那之后将永远为假。

所以你只发送一个缓冲区。

(您的代码中可能还有更多错误。例如,如果发生短写入(您将重新发送缓冲区的头部),您将发送缓冲区的错误部分。)

于 2012-04-22T10:14:13.647 回答