为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 100
学习任务

别摸我的键盘 的学生作业:

#ifndef __FILE_TRANS_H__ #define __FILE_TRANS_H__ #include #include #include #include #include #include #include #define FILENAME_SZ 64 typedef struct file_protocol{ //文件大小 ssize_t filesize; //文件名字 char filename[FILENAME_SZ]; }file_protocol_t; //服务端接受文件协议头 extern int recv_protocol_head(int cfd, file_protocol_t *p_head); //服务端接收文件数据 extern int recv_filedata(int cfd,const char *filename,size_t targetsize); //服务端接受客户端文件上传接口设计 extern int client_upload_file(int cfd); //客户端发送传输头协议 extern int send_protocol_head(const char *filename,int sockfd); //文件上传发送接口设计 extern int upload_file(const char *filename,int sockfd); #endif #include "file_transmission.h" #include "tcpsocket.h" #include "debug.h" #define BUF_SZ 1024 int recv_protocol_head(int cfd, file_protocol_t *p_head){ ssize_t rbytes; int total_sz; char *buf = (char *)p_head; int length = sizeof(file_protocol_t); while(1){ rbytes = tcp_recv_pack(cfd, buf + total_sz, length - total_sz); if(-1 == rbytes){ DEBUG_INFO("[ERROR] tcp_recv_pack(): %s\n", strerror(errno)); return -1; }else if(rbytes == 0){ DEBUG_INFO("[INFO] client quit!\n"); break; }else if(rbytes > 0){ total_sz += rbytes; if(total_sz == length){ break; } } } if(total_sz != length){ DEBUG_INFO("[ERROR] server recv head data fail!\n"); return -1; } return 0; } int recv_filedata(int cfd,const char *filename,size_t targetsize){ ssize_t rbytes = 0,wbytes = 0; int total_sz = 0; char buf[BUF_SZ]; int fd; DEBUG_INFO("[INFO] : filename %s\n",filename); fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666); if(fd == -1){ DEBUG_INFO("[ERROR] open(): %s\n", strerror(errno)); return -1; } while(1){ memset(buf, 0, sizeof(buf)); rbytes = tcp_recv_pack(cfd, buf, sizeof(buf)); if(rbytes == -1){ DEBUG_INFO("[ERROR] tcp_recv_pack(): %s\n", strerror(errno)); return -1; }else if(rbytes == 0){ DEBUG_INFO("[INFO] client quit!\n"); break; }else if(rbytes > 0){ wbytes = write(fd, buf, strlen(buf)); if(wbytes != rbytes){ DEBUG_INFO("[ERROR] Failed to write data\n"); return -1; } total_sz += rbytes; if(total_sz == targetsize){ break; } } } close(fd); return total_sz; } int client_upload_file(int cfd){ file_protocol_t head; ssize_t sz, filesize; char *filename; sz = recv_protocol_head(cfd, &head); if(sz == -1){ return -1; } filesize = head.filesize; filename = head.filename; sz = recv_filedata(cfd, filename, filesize); if(sz == -1){ return -1; } printf("Recv %%%d\n",(int)(sz * 1.0 / filesize) * 100); return sz; } int send_protocol_head(const char *filename,int sockfd){ file_protocol_t head; int fd; int filesize = 0; ssize_t sz; fd = open(filename, O_RDONLY); if(fd == -1){ DEBUG_INFO("[ERROR] open(): %s\n",strerror(errno)); return -1; } filesize = lseek(fd, 0, SEEK_END); if(-1 == filesize){ DEBUG_INFO("[ERROR] lseek(): %s\n",strerror(errno)); return -1; } close(fd); head.filesize = filesize; strcpy(head.filename, filename); sz = tcp_send_pack(sockfd, &head, sizeof(file_protocol_t)); if(sz != sizeof(file_protocol_t)){ DEBUG_INFO("[ERROR] lseek(): %s\n",strerror(errno)); return -1; } return filesize; } int upload_file(const char *filename,int sockfd){ int fd; char buf[BUF_SZ]; ssize_t rsize = 0, ssize = 0; int total_sz = 0, hsize = 0; hsize = send_protocol_head(filename, sockfd); if(hsize == -1){ DEBUG_INFO("[ERROR] send_protocol_head()\n"); return -1; } fd = open(filename, O_RDONLY); if(fd == -1){ DEBUG_INFO("[ERROR] open(): %s\n",strerror(errno)); return -1; } while(1){ memset(buf, 0, sizeof(buf)); rsize = read(fd, buf, sizeof(buf)); if(-1 == rsize){ DEBUG_INFO("[ERROR] open(): %s\n",strerror(errno)); return -1; } ssize = tcp_send_pack(sockfd, buf, sizeof(buf)); if(rsize != ssize){ DEBUG_INFO("[ERROR] tcp_send_pack()\n"); return -1; } total_sz += ssize; if(total_sz == hsize){ break; } } close(fd); return hsize; }

得分 100
学习任务

别摸我的键盘 的学生作业:

#ifndef __FILE_TRANS_H__ #define __FILE_TRANS_H__ #include #include #include #include #include #include #include #define FILENAME_SZ 64 typedef struct file_protocol{ //文件大小 ssize_t filesize; //文件名字 char filename[FILENAME_SZ]; }file_protocol_t; //接受文件协议头 extern int recv_protocol_head(int cfd, file_protocol_t *p_head); //接收文件数据 extern int recv_filedata(int cfd,const char *filename,size_t targetsize); //文件上传接口设计 extern int client_upload_file(int cfd); //发送传输头协议 extern int send_protocol_head(const char *filename,int sockfd); #endif #include "file_transmission.h" #include "tcpsocket.h" #include "debug.h" #define BUF_SZ 1024 int recv_protocol_head(int cfd, file_protocol_t *p_head){ ssize_t rbytes; int total_sz; char *buf = (char *)p_head; int length = sizeof(file_protocol_t); while(1){ rbytes = tcp_recv_pack(cfd, buf + total_sz, length - total_sz); if(-1 == rbytes){ DEBUG_INFO("[ERROR] tcp_recv_pack(): %s\n", strerror(errno)); return -1; }else if(rbytes == 0){ DEBUG_INFO("[INFO] client quit!\n"); break; }else if(rbytes > 0){ total_sz += rbytes; if(total_sz == length){ break; } } } if(total_sz != length){ DEBUG_INFO("[ERROR] server recv head data fail!\n"); return -1; } return 0; } int recv_filedata(int cfd,const char *filename,size_t targetsize){ ssize_t rbytes = 0,wbytes = 0; int total_sz = 0; char buf[BUF_SZ]; int fd; DEBUG_INFO("[INFO] : filename %s\n",filename); fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666); if(fd == -1){ DEBUG_INFO("[ERROR] open(): %s\n", strerror(errno)); return -1; } while(1){ memset(buf, 0, sizeof(buf)); rbytes = tcp_recv_pack(cfd, buf, sizeof(buf)); if(rbytes == -1){ DEBUG_INFO("[ERROR] tcp_recv_pack(): %s\n", strerror(errno)); return -1; }else if(rbytes == 0){ DEBUG_INFO("[INFO] client quit!\n"); break; }else if(rbytes > 0){ wbytes = write(fd, buf, strlen(buf)); if(wbytes != rbytes){ DEBUG_INFO("[ERROR] Failed to write data\n"); return -1; } total_sz += rbytes; if(total_sz == targetsize){ break; } } } close(fd); return total_sz; } int client_upload_file(int cfd){ file_protocol_t head; ssize_t sz, filesize; char *filename; sz = recv_protocol_head(cfd, &head); if(sz == -1){ return -1; } filesize = head.filesize; filename = head.filename; sz = recv_filedata(cfd, filename, filesize); if(sz == -1){ return -1; } printf("Recv %%%d\n",(int)(sz * 1.0 / filesize) * 100); return sz; } int send_protocol_head(const char *filename,int sockfd){ file_protocol_t head; int fd; int filesize = 0; ssize_t sz; fd = open(filename, O_RDONLY); if(fd == -1){ DEBUG_INFO("[ERROR] open(): %s\n",strerror(errno)); return -1; } filesize = lseek(fd, 0, SEEK_END); if(-1 == filesize){ DEBUG_INFO("[ERROR] lseek(): %s\n",strerror(errno)); return -1; } close(fd); head.filesize = filesize; strcpy(head.filename, filename); sz = tcp_send_pack(sockfd, &head, sizeof(file_protocol_t)); if(sz != sizeof(file_protocol_t)){ DEBUG_INFO("[ERROR] lseek(): %s\n",strerror(errno)); return -1; } return filesize; }

得分 100
学习任务

别摸我的键盘 的学生作业:

#ifndef __FILE_TRANS_H__ #define __FILE_TRANS_H__ #include #include #include #include #include #include #include #define FILENAME_SZ 64 typedef struct file_protocol{ //文件大小 ssize_t filesize; //文件名字 char filename[FILENAME_SZ]; }file_protocol_t; //接受文件协议头 extern int recv_protocol_head(int cfd, file_protocol_t *p_head); //接收文件数据 extern int recv_filedata(int cfd,const char *filename,size_t targetsize); //文件上传接口设计 extern int client_upload_file(int cfd); #endif #include "file_transmission.h" #include "tcpsocket.h" #include "debug.h" #define BUF_SZ 1024 int recv_protocol_head(int cfd, file_protocol_t *p_head){ ssize_t rbytes; int total_sz; char *buf = (char *)p_head; int length = sizeof(file_protocol_t); while(1){ rbytes = tcp_recv_pack(cfd, buf + total_sz, length - total_sz); if(-1 == rbytes){ DEBUG_INFO("[ERROR] tcp_recv_pack(): %s\n", strerror(errno)); return -1; }else if(rbytes == 0){ DEBUG_INFO("[INFO] client quit!\n"); break; }else if(rbytes > 0){ total_sz += rbytes; if(total_sz == length){ break; } } } if(total_sz != length){ DEBUG_INFO("[ERROR] server recv head data fail!\n"); return -1; } return 0; } int recv_filedata(int cfd,const char *filename,size_t targetsize){ ssize_t rbytes = 0,wbytes = 0; int total_sz = 0; char buf[BUF_SZ]; int fd; DEBUG_INFO("[INFO] : filename %s\n",filename); fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666); if(fd == -1){ DEBUG_INFO("[ERROR] open(): %s\n", strerror(errno)); return -1; } while(1){ memset(buf, 0, sizeof(buf)); rbytes = tcp_recv_pack(cfd, buf, sizeof(buf)); if(rbytes == -1){ DEBUG_INFO("[ERROR] tcp_recv_pack(): %s\n", strerror(errno)); return -1; }else if(rbytes == 0){ DEBUG_INFO("[INFO] client quit!\n"); break; }else if(rbytes > 0){ wbytes = write(fd, buf, strlen(buf)); if(wbytes != rbytes){ DEBUG_INFO("[ERROR] Failed to write data\n"); return -1; } total_sz += rbytes; if(total_sz == targetsize){ break; } } } close(fd); return total_sz; } int client_upload_file(int cfd){ file_protocol_t head; ssize_t sz, filesize; char *filename; sz = recv_protocol_head(cfd, &head); if(sz == -1){ return -1; } filesize = head.filesize; filename = head.filename; sz = recv_filedata(cfd, filename, filesize); if(sz == -1){ return -1; } printf("Recv %%%d\n",(int)(sz * 1.0 / filesize) * 100); return sz; }

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号