标题:这个地方不太懂,有没有人可以帮忙解释一下?
取消只看楼主
专业课的小白
Rank: 1
等 级:新手上路
威 望:1
帖 子:56
专家分:0
注 册:2019-12-17
结帖率:40%
已结贴  问题点数:20 回复次数:0 
这个地方不太懂,有没有人可以帮忙解释一下?
背景:用户输入命令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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-509218-1-1.html




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

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