| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1170 人关注过本帖
标题:这个地方不太懂,有没有人可以帮忙解释一下?
只看楼主 加入收藏
专业课的小白
Rank: 1
等 级:新手上路
威 望:1
帖 子:56
专家分:0
注 册:2019-12-17
结帖率:40%
收藏
已结贴  问题点数:20 回复次数:3 
这个地方不太懂,有没有人可以帮忙解释一下?
背景:用户输入命令get,从服务器中得到文件,对应指令GET
代码:
服务器端:
程序代码:
 else if(strcmp(text,"GET") == 0)
                {
                    int fd;
                    char buf1[N] = "";
                    char text1[N] = "";
                    if(recv(acceptfd,text1,N,0) == -1)
                    {
                        perror("fail to recv");
                        exit(1);
                    }
                    
                    if((fd = open(text1,O_RDONLY)) == -1)//表示以只读的方式打开文件
                                                         //open成功则返回,否则返回-1;
                    {
                        if(errno == ENOENT)
                        {
                            strcpy(buf1,"***NO FILE***");//strcpy函数,将后者复制到前者,返回值为指向前者的一个指针
                            if(send(acceptfd,buf1,N,0) == -1)
                            {
                                perror("fail to send");
                                exit(1);
                            }

                            return;
                        }
                        else
                        {
                            perror("fail to open");
                            exit(1);
                        }
                    }
                    //文件存在
                    strcpy(buf1,"***start***");
                    if(send(acceptfd,buf1,N,0) == -1)
                    {
                        perror("fail to send");
                        exit(1);
                    }
                    ssize_t bytes;
                    while((bytes = read(fd,buf1,N)) != 0)
                    {
                        if(send(acceptfd,buf1,bytes,0) == -1)
                        {
                            perror("fail to send");
                            exit(1);
                        }
                    }
                    sleep(1);
                    strcpy(buf1,"***over***");
                    if(send(acceptfd,buf1,N,0) == -1)
                    {
                        perror("fail to send");
                        exit(1);
                    }
                    printf("--文件发送完毕--\n");
                }


客户端:
程序代码:
strcpy(buf,"GET");
            if(send(sockfd,buf,N,0) == -1)
            {
                perror("fail to send");
                exit(1);
            }
            printf("请输入你想要的文件名:\n");
            char file_name[N] = "";
            fgets(file_name,N,stdin);
            file_name[strlen(file_name) - 1] = '\0';
            if(send(sockfd,file_name,N,0) == -1)
            {
                perror("fail to send");
                exit(1);
            }

            int fd;
            char buf1[N] = "";
            char text1[N] = "";
            if(recv(sockfd,text1,N,0) == -1)
            {
                perror("fail to recv");
                exit(1);
            }
            if(strncmp(text1,"***NO FILE***",13) == 0)//将前者与后者进行比较,最多比较13个
            {
                printf("%s\n",text1);
            }
            else
            {
                printf("%s\n",text1);
                if((fd = open("copy.txt",O_CREAT | O_TRUNC | O_WRONLY,0664)) == -1)
                {
                    perror("fail to open");
                    exit(1);
                }
                ssize_t bytes;
                while((bytes = recv(sockfd,text1,N,0)) != -1)
                {
                    if(strncmp(text1,"***over***",10) == 0)
                    {
                        printf("%s\n",text1);
                        break;
                    }

                    write(fd,text1,bytes);
                }
                printf("--文件接收完毕--\n");
                //return;
            }
        }
搜索更多相关主题的帖子: exit text1 strcpy send printf 
2022-05-31 11:48
kob
Rank: 2
等 级:论坛游民
帖 子:3
专家分:10
注 册:2022-5-31
收藏
得分:10 
2022-05-31 14:59
do8do8do8
Rank: 10Rank: 10Rank: 10
来 自:沙滩
等 级:贵宾
威 望:17
帖 子:366
专家分:1845
注 册:2010-7-2
收藏
得分:10 
else if(strcmp(text,"GET") == 0)  // text 里的内容与  GET 比较,相同则往下执行
                {
                    int fd;        声明整型变量 fd
                    char buf1[N] = ""; 声明字符数组
                    char text1[N] = ""; 声明字符数组
                    if(recv(acceptfd,text1,N,0) == -1) 从acceptfd接收数据到text1
                    {
                        perror("fail to recv");
                        exit(1);
                    }
                    
                    if((fd = open(text1,O_RDONLY)) == -1)//表示以只读的方式打开文件
                                                         //open成功则返回,否则返回-1;
                    {
                        if(errno == ENOENT)
                        {
                            strcpy(buf1,"***NO FILE***");//strcpy函数,将后者复制到前者,返回值为指向前者的一个指针
                            if(send(acceptfd,buf1,N,0) == -1)
                            {
                                perror("fail to send");
                                exit(1);
                            }

                            return;
                        }
                        else
                        {
                            perror("fail to open");
                            exit(1);
                        }
                    }
                    //文件存在
                    strcpy(buf1,"***start***");  buf1装这个字符串并在字符串后尾添加'\0'
                    if(send(acceptfd,buf1,N,0) == -1)  发送buf1里的字符串到acceptfd
                    {
                        perror("fail to send");
                        exit(1);
                    }
                    ssize_t bytes;  声明变量
                    while((bytes = read(fd,buf1,N)) != 0) 读取
                    {
                        if(send(acceptfd,buf1,bytes,0) == -1) 发送
                        {
                            perror("fail to send");
                            exit(1);
                        }
                    }
                    sleep(1);    延时
                    strcpy(buf1,"***over***");  复制
                    if(send(acceptfd,buf1,N,0) == -1) 发送
                    {
                        perror("fail to send");
                        exit(1);
                    }
                    printf("--文件发送完毕--\n");
                }

学C语言从底层开始,学编程从问题开始,一日学会C!!!
2022-06-01 08:52
do8do8do8
Rank: 10Rank: 10Rank: 10
来 自:沙滩
等 级:贵宾
威 望:17
帖 子:366
专家分:1845
注 册:2010-7-2
收藏
得分:0 
strcpy(buf,"GET");  复制
            if(send(sockfd,buf,N,0) == -1) 往sockfd发送buf里的内容
            {
                perror("fail to send");
                exit(1);
            }
            printf("请输入你想要的文件名:\n");
            char file_name[N] = "";  声明字符数组
            fgets(file_name,N,stdin);  从stdin 获取内容到 file_name
            file_name[strlen(file_name) - 1] = '\0'; 在后尾添加'\0'
            if(send(sockfd,file_name,N,0) == -1) 发送
            {
                perror("fail to send");
                exit(1);
            }

            int fd;
            char buf1[N] = "";
            char text1[N] = "";
            if(recv(sockfd,text1,N,0) == -1)  从sockfd接收数据到text1
            {
                perror("fail to recv");
                exit(1);
            }
            if(strncmp(text1,"***NO FILE***",13) == 0)//将前者与后者进行比较,最多比较13个
            {
                printf("%s\n",text1);
            }
            else
            {
                printf("%s\n",text1);
                if((fd = open("copy.txt",O_CREAT | O_TRUNC | O_WRONLY,0664)) == -1) 打开copy.txt 文件
                {
                    perror("fail to open");
                    exit(1);
                }
                ssize_t bytes;
                while((bytes = recv(sockfd,text1,N,0)) != -1) 接收
                {
                    if(strncmp(text1,"***over***",10) == 0)
                    {
                        printf("%s\n",text1);
                        break;
                    }

                    write(fd,text1,bytes);  把数据写到文件里copy.txt
                }
                printf("--文件接收完毕--\n");
                //return;
            }
        }

学C语言从底层开始,学编程从问题开始,一日学会C!!!
2022-06-01 08:55
快速回复:这个地方不太懂,有没有人可以帮忙解释一下?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.079841 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved