简体   繁体   English

使用C将文件从服务器发送到Linux上的客户端

[英]Send file from Server to Client on Linux in C

I have a problem with this code. 我对此代码有疑问。 The problem is that I have to develop a client-server model with this futures: 问题是我必须用这种期货开发一个客户端-服务器模型:

  1. Client type: "GETfile_name.txt"; 客户端类型:“ GETfile_name.txt”;
  2. Server receives the request and send file's size; 服务器接收请求并发送文件的大小;
  3. Server send the file; 服务器发送文件;

The problem is that Server doesn't send the file to the client. 问题在于服务器不将文件发送到客户端。 Thanks to all for your help. 感谢大家的帮助。

The code is this: 代码是这样的:

Server: 服务器:

f = fopen(file_name,"r");

/* file's size */
fseek(f,0L,SEEK_END);
size = ftell(f);
fseek(f,0L,SEEK_SET);

/* convert int to string */
sprintf(file_size,"%d",size); 

/* send file's size to the client */
Send(s,file_size,sizeof(file_size)); 

/* send file to the client */
buffer = (char *) malloc(sizeof(char)*size);  
bzero(buffer,sizeof(char)*size);
x = fread(binoltro,sizeof(char),sizeof(char)*dimensione,f);
Send(s,binoltro,x);

Client: 客户:

/* wait file's size */
recv(s,buffer,MAX,0); 
printf("Remote server will send: %s Bytes\n",buffer);
size = atoi(buffer);

/* execute malloc */
buffer = (char *) malloc(sizeof(char)*size);
bzero(buffer,size*sizeof(char));

Recv(s,buffer,size*sizeof(char));
fwrite(buffer,sizeof(char),size*sizeof(char),f);
printf("File: %s",buffer);

/* clean the buffer */
bzero(buffer,MAX);

/* do free */
free(buffer);
fclose(f);

With: 带有:

int Recv (int s, char *ptr, size_t maxlen){
size_t n;
ssize_t nread;
char c;

for (n=1; n<maxlen; n++)
{
    nread=recv(s, &c, 1, 0);
    if (nread == 1)
    {
        *ptr++ = c;
        if (c == '\n')
            break;
    }
    else if (nread == 0)    
    {
        *ptr = 0;
        return (n-1); 
    }
    else
        return (-1); 
}
*ptr = 0;
return (n);
}

And: 和:

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

The problem I see is that your Recv() function breaks when either it receives a '\\n' or there is no more data or maxlen is reached. 我看到的问题是,当您的Recv()函数接收到'\\ n'或没有更多数据或达到maxlen时,它就会中断。 None of these conditions match after your client received the last byte of the file length so it reads on and the file data is lost. 在您的客户端收到文件长度的最后一个字节后,这些条件均不匹配,因此它将继续读取并且文件数据丢失。

I would suggest you fill the file length to a fixed number of digits( eg 10) by using 我建议您使用来将文件长度填充为固定位数(例如10)

sprintf( file_size, "%010s", size );

and use a modified Recv() function like this: 并使用经过修改的Recv()函数,如下所示:

int Recv (int s, char *ptr, size_t nbytes ){
    size_t n;
    ssize_t nread;


    while( nbytes > 0 ) {
        if( (nread = recv( s, ptr, nbytes, 0 )) <= 0 )
            return -1; /* Error */

        ptr += nread;
        nbytes -= nread;
    }
    +ptr = '\0';
    return 0; /* OK */
}

Then your client can first read the file size using 然后,您的客户端可以首先使用读取文件大小

Recv( s, buffer, 10 )

and then the data using 然后使用

Recv( s, ..., atoi( buffer ) )

Make sure to alloc file size + 1 because of the '\\0' that is added by Recv() 由于Recv()添加了'\\ 0',因此请确保分配文件大小+ 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM