Unix Network Programming Episode 98

复制代码
#include "unp.h"

int my_open(const char *, int);

int main(int argc, char **argv)
{
    int fd, n;
    char buff[BUFFSIZE];

    if(argc!=2)
        err_quit("usage: mycat <pathname>");
    
    if((fd=my_open(argv[1],O_RDONLY))<0)
        err_sys("cannot open %s", argv[1]);
    
    while((n=Read(fd, buff, BUFFSIZE))>0)
        Write(STDOUT_FILENO, buff, n);
    
    return 0;
}

mycat program: copies a file to standard output

复制代码
#include "unp.h"

int my_open(const char *, int);

int my_open(const char *pathname, int mode)
{
    int fd, sockfd[2], status;
    pid_t childpid;
    char c, argsockfd[10], argmode[10];

    Socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd);

    if((childpid=Fork())==0)
    {
        Close(sockfd[0]);
        snprintf(argsockfd, sizeof(argsockfd),"%d", sockfd[1]);
        snprintf(argmode, sizeof(argmode), "%d", mode);
        execl("./openfile", "openfile", argsockfd, pathname, argmode, (char *)NULL);
        err_sys("execl error");
    }

    Close(sockfd[1]);

    Waitpid(childpid, &status, 0);
    if(WIFEXITED(status)==0)
        err_quit("child did not terminate");
    if((status=WEXITSTATUS(status))==0)
        Read_fd(sockfd[0], &c, 1, &fd);
    else
    {
        errno=status;
        fd=-1;
    }

    Close(sockfd[0]);
    
    return fd;
}

my_open function: opens a file and returns a descriptor

复制代码
在这里插入代码片

read_fd function: receives data and a descriptor

相关推荐
FileLink跨网文件交换19 分钟前
跨网文件交换?内外网文件交换十大方法构建安全合规的数据传输通道
运维·服务器·网络
️️(^~^)23 分钟前
静态路由综合配置实验报告
服务器·网络·计算机网络·智能路由器
执笔诉情殇〆3 小时前
前后端分离(java) 和 Nginx在服务器上的完整部署方案(redis、minio)
java·服务器·redis·nginx·minio
宇钶宇夕4 小时前
S7-1200 系列 PLC 中 SCL 语言的 PEEK 和 POKE 指令使用详解
运维·服务器·数据库·程序人生·自动化
心 一4 小时前
Python 类型注解实战:`Optional` 与安全数据处理的艺术
服务器·python·安全
A小码5 小时前
软件开发那些基础事儿:需求、模型与生命周期
运维·服务器
这儿有一堆花6 小时前
Nginx服务器集群:横向扩展与集群解决方案
运维·服务器·nginx
egoist20237 小时前
【Linux仓库】命令行参数与环境变量【进程·伍】
linux·运维·服务器·环境变量·命令行参数·内建命令
Fireworkitte7 小时前
Linux 中替换sed
linux·运维·服务器
gooxi_hui7 小时前
性能狂飙 Gooxi 8卡5090服务器重新定义高密度算力
运维·服务器