1

我有一台服务器与另一台服务器联系以获取文件,然后将其发送给客户端。我已经编写了这段代码(C - Linux),但只有前 4 个字节到达客户端。比我更好的人能看出错误吗?非常感谢

int Recv_file (int s, int f, char *ptr, size_t maxlen){
    int const TIMEOUT = 60; /* 60 seconds */
    size_t n;
    ssize_t nread;
    ssize_t nsend;
    char c;
    fd_set cset;
    struct timeval tval;
    int x;

    for (n=1; n<maxlen; n++)
    {
        FD_ZERO(&cset);
        FD_SET(s, &cset);
        tval.tv_sec = TIMEOUT;
        tval.tv_usec = 0;
        x = select(FD_SETSIZE, &cset, NULL, NULL, &tval);
        if (x==-1) {
            perror("select() failed");
            return -1; /* -1 = close connection with the client */
        }
        if (x>0) {
            nread=recv(s, &c, 1, 0);
            if (nread == 1)
            {
                *ptr++ = c;
                nsend = Send(f,&c,1);
                if (nsend != 1) {
                    return -1; /* close connection with the client  */
                }
            }else if (nread == 0){
                *ptr = 0;
                return (-1); /* close connection with the client  */
            }
            else
                return (-1); /* close connection with the client  */
        }else{
            printf("(It's been %d seconds and I have not received any response",TIMEOUT);
            close(s);
            return(-1); /* close connection with the client  */
        }
    }
    *ptr = 0;
    return (n); /* n == maxlen */
}

和:

int Send(int s, char *ptr, size_t nbytes){
    size_t  nleft;
    ssize_t nwritten;

    for (nleft=nbytes; nleft > 0; )
    {
        nwritten = send(s, ptr, nleft, 0);
        if (nwritten <=0)
            return (nwritten);
        else
        {
            nleft -= nwritten;
            ptr += nwritten;
        }
    }
    return (nbytes - nleft); /* number of bytes sent */
}

更新:

在等待我修改的答案时:

nread=recv(s, &c, 1, 0);nread=recv(s, &c, sizeof(c), 0);

Send(f,&c,1);Send(f,&c,sizeof(c));

但是现在我没有收到文件的最后一个字节!

再次感谢你!

4

1 回答 1

1

你没有得到最后一个字节,因为 for 循环从 1 开始

for (n=1; n<maxlen; n++); 
// it should be
for (n=0; n<maxlen; n++); 
// or either
for (n=1; n<=maxlen; n++); 

编辑:此外,您可以读取尽可能多的字节,而不仅仅是一个一个,实际上您正在以正确的方式发送,

例如:

int bLeft = maxlen;
while (bLeft > 0)
{
    FD_ZERO(&cset);
    FD_SET(s, &cset);
    tval.tv_sec = TIMEOUT;
    tval.tv_usec = 0;
    x = select(FD_SETSIZE, &cset, NULL, NULL, &tval);
    if (x > 0) {
         nread=recv(s, ptr, bLeft , 0);
         if (nread > 0)
         {
            nsend = Send(f, ptr, nread, 0);
            if (nsend <= 0) {
               return -1; /* close connection with the client  */
            }
            ptr+ = nread;
            bLeft -= nread;
        }else if (nread == 0){
             *ptr = 0;
             return (-1); /* close connection with the client  */
         }
         else
             return (-1); /* close connection with the client  */
   }else{
         printf("(It's been %d seconds and I have not received any response",TIMEOUT);
         close(s);
         return(-1); /* close connection with the client  */
   }
}
// I would not recommended this, you should adhere to a maxlen length
// assuming there is more space could lead to a memory Exception.
ptr++;
*ptr ='\0';
return (maxlen-bLeft);
于 2013-06-15T13:48:15.433 回答