注册 登录
编程论坛 C语言论坛

这个地方不太懂,有没有人可以帮忙解释一下?

专业课的小白 发布于 2022-05-31 11:48, 1417 次点击
背景:用户输入命令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;
            }
        }
3 回复
#2
kob2022-05-31 14:59
#3
do8do8do82022-06-01 08:52
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");
                }
#4
do8do8do82022-06-01 08:55
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;
            }
        }
1