【问题标题】:c socket send file size and then the filec socket 发送文件大小,然后发送文件
【发布时间】:2012-06-26 21:56:09
【问题描述】:

这是client.c的sn-p:

size_t fsize;
int total_bytes_read = 0, ret_val;

hp = gethostbyname(argv[1]);
DescrittoreClient = socket(AF_INET, SOCK_STREAM, 0);
    connect(DescrittoreClient, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
strcpy(Buffer, filename);
send(DescrittoreClient, Buffer, strlen(Buffer), 0);
ret_val = read(DescrittoreClient, &fsize, sizeof(fsize));
if(ret_val == -1){
    printf("Errore durante ricezione grandezza file\n");
    close(DescrittoreClient);
    exit(1);
}

fd = open(filename, O_CREAT | O_WRONLY,0644);
if (fd  < 0) {
    perror("open");
    exit(1);
}

while(total_bytes_read < fsize){
    while ((nread = read(DescrittoreClient, Buffer, sizeof(Buffer))) > 0){
        write(fd, Buffer, nread);
        total_bytes_read += nread;
    }
}
printf("File ricevuto\n");

这是server.c的sn-p:

int DescrittoreServer, DescrittoreClient, LunghezzaClient;
int NumPorta = atoi(argv[1]);
char Buffer[1024] = {};
int rc, fd;
off_t offset = 0;
struct stat stat_buf;
char filename[1024] = {};
int fsize[10240] = {};

DescrittoreServer = socket(AF_INET, SOCK_STREAM, 0);

if(bind(DescrittoreServer, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
    perror("Errore di bind\n");
    close(DescrittoreServer);
    exit(1);
}
listen(DescrittoreServer, 5);
LunghezzaClient = sizeof(cli_addr);
while(1){
    DescrittoreClient = accept(DescrittoreServer, (struct sockaddr *) &cli_addr, &LunghezzaClient);

rc = recv(DescrittoreClient, filename, sizeof(filename), 0);

fprintf(stderr, "Ricevuta richiesta di inviare il file: '%s'\n", filename);

/* open the file to be sent */
fd = open(filename, O_RDONLY);

/* get the size of the file to be sent */
fstat(fd, &stat_buf);
*fsize = stat_buf.st_size;
send(DescrittoreClient, fsize, sizeof(fsize), 0);

/* copy file using sendfile */
offset = 0;
rc = sendfile(DescrittoreClient, fd, &offset, stat_buf.st_size);
if (rc != stat_buf.st_size) {
    fprintf(stderr, "Trasferimento incompleto: %d di %d bytes\n", rc, (int)stat_buf.st_size);
    exit(1);
}   

    close(DescrittoreClient);
    close(fd);
}

这段代码有什么作用:
1) 客户端发送文件名
2) 服务器发送文件的长度(以字节为单位)
3) 客户端接收文件的长度
4) 服务器发送文件
5) 客户端接收文件
问题是文件没有完全接收到,只有一部分被客户端写入(例如,服务器发送一个 2143 字节的文件,客户端只接收到 95 个字节) 我不明白为什么!
PS:部分错误处理已被删除,代码更具可读性!

【问题讨论】:

  • @nos 我不明白!我已经在客户端声明了 fsize[10240]!
  • 他说sizeof(fsize) 是 10240。这就是您在客户端使用第二个send() 发送的字节数。
  • @polslinux 但是recv(DescrittoreClient, fsize, sizeof(fsize), 0); 可能不会读取所有这些字节,它可能会读取不到sizeof(fsize)(请记住,TCP 为您提供字节流,它不保留任何消息/数据包边界)
  • @nos 我该如何解决这个问题??
  • @polslinux:您可能想在这里阅读:beej.us/guide/bgnet

标签: c sockets sendfile


【解决方案1】:

你必须在循环中调用 recv() 并总结它的返回值:

while (read_bytes < filesize) {
    read_bytes += recv(socket, buffer + read_bytes, filesize-read_bytes, flags)
}

当然,您还应该添加错误处理,即检查您是否读取了 0 个字节并且连接已关闭。

【讨论】:

  • 感谢您的回答,但使用您的代码我得到“错误:函数‘读取’的参数太多”
  • 对不起,我的意思是使用 recv 而不是 read。 read() 不接受标志参数。否则它是相同的技术。
  • 是的,我在评论后看到了 xD
【解决方案2】:

问题是我在客户端中声明了char nread=0 而不是int nread=0,所以我可以用char 读取的最大值是127...将char 更改为int 解决了我的问题!

【讨论】:

  • size_t 会是一个更好的选择 :) 这正是 read 所期望的。
猜你喜欢
  • 2018-03-24
  • 2021-07-09
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多