【问题标题】:c socket sent file contains other messagesc socket发送的文件包含其他消息
【发布时间】:2012-07-08 04:02:36
【问题描述】:

这是 RETR cmd 的简单实现,服务器首先接收文件名,然后发送文件。

/************************* RECEIVE FILE NAME AND SEND FILE *************************/
if(recv(newsockd, buffer, sizeof(buffer), 0) < 0){
  perror("error receiving file name");
  onexit(newsockd, sockd, 0, 2);
}
other = strtok(buffer, " ");
filename = strtok(NULL, "\n");
if(strcmp(other, "RETR") == 0){
  printf("received RETR request\n");
} else onexit(newsockd, sockd, 0, 2);

fd = open(filename, O_RDONLY);
    if (fd < 0) {
    fprintf(stderr, "cannot open '%s': %s\n", filename, strerror(errno));
    onexit(newsockd, sockd, 0, 2);
}

if(fstat(fd, &fileStat) < 0){
    perror("Error fstat");
    onexit(newsockd, sockd, fd, 3);
}
fsize = fileStat.st_size;
if(send(newsockd, &fsize, sizeof(fsize), 0) < 0){
    perror("Error on sending file size\n");
    onexit(newsockd, sockd, fd, 3);
}

rc = sendfile(newsockd, fd, &offset, fileStat.st_size);
if(rc == -1) {
        fprintf(stderr, "error sending file: '%s'\n", strerror(errno));
        onexit(newsockd, sockd, fd, 3);
}
if((uint32_t)rc != fsize) {
    fprintf(stderr, "transfer incomplete: %d di %d bytes sent\n", rc, (int)fileStat.st_size);
    onexit(newsockd, sockd, fd, 3);
}
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, "226 File trasferito con successo\n\0");
if(send(newsockd, buffer, strlen(buffer)+1, 0) < 0){
  perror("Errore durante l'invio 226");
  onexit(newsockd, sockd, 0, 2);
}
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, "221 Goodbye\n\0");
if(send(newsockd, buffer, strlen(buffer)+1, 0) < 0){
  perror("Errore durante l'invio 221");
  onexit(newsockd, sockd, 0, 2);
}
/************************* END PART *************************/

这是客户端程序的sn-p:

/************************* SEND FILE NAME AND RECEIVE FILE *************************/
printf("Inserire il nome del file da scaricare: ");
if(fgets(dirpath, BUFFGETS, stdin) == NULL){
    perror("fgets name file");
    close(sockd);
}
filename = strtok(dirpath, "\n");
sprintf(buffer, "RETR %s", dirpath);
if(send(sockd, buffer, strlen(buffer), 0) < 0){
    perror("error sending file name");
    close(sockd);
    exit(1);
}
if(read(sockd, &fsize, sizeof(fsize)) < 0){
    perror("error on receiving file size\n");
    close(sockd);
    exit(1);
}
fd = open(filename, O_CREAT | O_WRONLY, 0644);
if (fd  < 0) {
    perror("open");
    exit(1);
}

while(((uint32_t)total_bytes_read != fsize) && ((nread = read(sockd, filebuffer, fsize)) > 0)){
    if(write(fd, filebuffer, nread) < 0){
        perror("write");
        close(sockd);
        exit(1);
    }
    total_bytes_read += nread;
}
memset(buffer, 0, sizeof(buffer));
if(recv(sockd, buffer, 34, 0) < 0){
    perror("Error receiving 226");
    close(sockd);
    exit(1);
}
printf("%s", buffer);
memset(buffer, 0, sizeof(buffer));
if(recv(sockd, buffer, 13, 0) < 0){
    perror("Error receiving 221");
    close(sockd);
    exit(1);
}
printf("%s", buffer);
memset(buffer, 0, sizeof(buffer));
close(fd);
/************************* END PART *************************/

有什么问题?
问题是已发送的文件还包含服务器发送的 2 条消息(226 和 221),我不知道为什么会出现这种行为 O.o
示例:
RETR tryfile.txt
File tryfile.txt received
cat tryfile.txt
“这是tryfile.txt,你好客户端
226 文件传输成功
221再见”

【问题讨论】:

  • 1) 您假设 TCP 保留了消息边界。他们不是。 2)您假设 NUL 终止的字符串,但您不发送 NUL。 3)IIRC,基于行的协议应以\r\n终止行。
  • 1) 嗯?我不明白,抱歉:(2) 用strlen(buffer)+1 更正send 3) 我假设使用普通线:) 这是一个非常简单的项目
  • @wildplasser:虽然反直觉,但一定要使用\r\n。尽管这是 Windows 的行尾,但我的经验是,尽管 Windows 在收到 UNIX 行尾时会抱怨和损坏。然而,在标准的 UNIX 方式中,UNIX 系统很乐意为我从 DOS 格式转换它,没有抱怨等。
  • 你能翻译一些意大利语 cmets 吗?
  • 我认为所有 rfc822 (-->> SMTP, HTTP) 之类的协议都希望 \r\n 作为 EOL,独立于平台(但实现应该容忍预期)@ 1) TCP是流协议。八位字节按顺序发送,但每个数据包接收的数量不需要等于发送的“数据包”的大小。 TCP 中没有数据包。 read() 和 write() 的返回值也不必等于第三个参数。它可以更小,或者 0 或 -1。发送/接收类似。

标签: c sockets sendfile


【解决方案1】:

strlen() 确实计算 \0,所以 strlen() 将返回 12:

strcpy(buffer, "221 Goodbye\n\0");
if(send(newsockd, buffer, strlen(buffer), 0) < 0){ ...}

客户端使用硬编码值 13。(发送 33 和预期 34 的其他状态消息相同。)顺便说一句:您真的需要一些缓冲机制,至少在客户端中。

更新: 使用“嵌入的 null”显示字符串的 strlen:

#include <stdio.h>
#include <string.h>

int main(void)
{
fprintf(stderr, "strlen is %u\n", (unsigned) strlen("221 Goodbye\n\0") );

return 0;
}

解释:strlen() 只计算字符,直到遇到 '\0' 字符

【讨论】:

  • 在上面的评论中我写过我已经更正了我的代码:) 但我没有更新问题对不起!但是有什么问题呢?这些是预定义的字符串,所以我的代码有什么问题???谢谢!
  • 你可以在源代码中添加一个额外的调试fprintf(stderr, "strlen is %u\n", (unsigned) strlen("221 Goodbye\n\0") );
  • 是的,strlen 是 13 岁 :) 我不明白你想对我说什么 :(
  • 也许你有不同的strlen();我的指纹 12.
  • WTF!!你说得对 xD 我是个白痴,我把 strlen+1 放了,因为我想 '\0' 但我已经包含了它 xD
【解决方案2】:

我已经找到并(最终)解决了这个问题。
这是解决方案:

uint32_t fsize_tmp = fsize;
while(((uint32_t)total_bytes_read != fsize) && ((nread = read(sockd, filebuffer, fsize_tmp)) > 0)){
     if(write(fd, filebuffer, nread) < 0){
    perror("write");
    close(sockd);
    exit(1);
      }
      total_bytes_read += nread;
      fsize_tmp -= nread;
}

问题是由于我没有检查“动态”文件大小(如果文件以 75% 发送,我的 while 循环再次预期 fsize 并且这是不可能的)所以使用此代码我将减小文件大小每次需要时:)

【讨论】:

  • 您仍然没有检查 write 的返回值。它可能小于nread。另外:读取可能返回零,导致片段永远循环。
  • 但是我已经设置了条件nread&gt;0,所以如果返回0就没有问题:)
猜你喜欢
  • 2023-03-15
  • 2014-07-03
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 2014-01-27
  • 1970-01-01
  • 2016-08-09
相关资源
最近更新 更多