Linux C系统编程中的文件传输问题:只能传送文本文件,不能传送二进制文件。(TCP+文件IO实现)

Linux C系统编程中的文件传输问题:只能传送文本文件,不能传送二进制文件。(TCP+文件IO实现),第1张

二进制模式传输文本文件没有问题,但是ASCII模式传输二进制文件会出问题

因为有些控制字符会被错误处理。比如二进制文件中的0,在传输的时候会被当作字符串结束符,这样是无法传送的。所以传输二进制文件的时候状况与文本文件是不一样的。

我提供的代码如下,自己补充main函数哈,希望能够帮到你:)

//相关头文件:

#include <stdio.h>

#include <signal.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

发送方send:

void fifo_pro()

{

char s[128]

int fd

FILE *fp

fp = fopen("./a.txt", "r")

mkfifo("/tmp/fifo.tst", 0644)

fd = open("/tmp/fifo.tst", O_WRONLY)

while(fgets(s, 127, fp) != NULL) {

write(fd, s, strlen(s))

//printf("%s",s)

}

close(fd)

fclose(fp)

unlink("/tmp/fifo.tst")

}

接收方get:

char s[128]

int fd = open("/tmp/fifo.tst", O_RDONLY)

int fd2 = open("./b.txt", O_WRONLY)

memset(s, 0, 128)

while(read(fd, s, 128) >0) {

printf("%s", s)

write(fd2, s, 128)

}

close(fd2)

close(fd)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/9027503.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-24
下一篇 2023-04-24

发表评论

登录后才能评论

评论列表(0条)

保存