#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <head.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>#define PORT 69 //端口号:填服务器绑定端口号
#define IP "192.168.1.101" //IP地址:windows的IP地址int upload(int cfd,struct sockaddr_in sin)
{char filename[20] = "";printf("请输入要上传的文件名>>> ");scanf("%s", filename);while(getchar()!=10);//判断文件存不存在int fd = open(filename,O_RDONLY);if(fd < 0){ERR_MSG("open");return -1;}//发送数据char buf[516] = "";int size = sprintf(buf, "%c%c%s%c%s%c", 0, 2, filename, 0, "octet", 0);if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin)) < 0) {ERR_MSG("sendto");return -1;}printf("sendto success\n");socklen_t addrlen = sizeof(sin);ssize_t res = 0;unsigned short num = 0; //本地记录的块编号while(1){bzero(buf, sizeof(buf));//接收服务器的应答res = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, &addrlen);if(res < 0){ERR_MSG("recvfrom");break;}if(4 == buf[1]){if(htons(num) == *(unsigned short*)(buf+2)){//组数据包给服务器num++;*(unsigned short*)buf = htons(3);*(unsigned short*)(buf+2) = htons(num);res = read(fd, buf+4, 512);if(res < 0){break;}else if(0 == res){printf("文件:%s 上传完毕\n", filename);break;}//发送数据包if(sendto(cfd, buf, res+4, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0){ERR_MSG("sendto");return -1;}}}else if(5 == buf[1])//错误包{printf("MSG_ERR: code[%d] msg[%s] __%d__\n", \ntohs(*(unsigned short*)(buf+2)), buf+4, __LINE__);break;}}close(fd);return 0;
}int download(int cfd,struct sockaddr_in sin)
{char buf[516] = "";char filename[20] = "";printf("请输入要下载的文件名>>> ");scanf("%s", filename);while(getchar()!=10);//发送下载请求//组协议包int size = sprintf(buf, "%c%c%s%c%s%c", 0, 1, filename, 0, "octet", 0);//sendto if(sendto(cfd, buf, size, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0){ERR_MSG("sendto");return -1;}//本地创建并打开要下载的文件int fd = -1; //必须初始化一个无意义的文件描述符,否则下面的closesocklen_t addrlen = sizeof(sin);ssize_t res = 0;unsigned short num = 0; //本地记录的块编号//循环接收数据包,回复ACkwhile(1){bzero(buf, sizeof(buf));//接收数据包res = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, &addrlen);if(res < 0){ERR_MSG("recvfrom");break;}if(3 == buf[1]) //数据包{if(htons(num+1) == *(unsigned short*)(buf+2)){num++;if(-1 == fd){fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0664);if(fd < 0){ERR_MSG("open");break;}}//将获取到的数据,写入到文件中if(write(fd, buf+4, res-4) < 0){ERR_MSG("write");break;}buf[1] = 4;if(sendto(cfd, buf, 4, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0){ERR_MSG("sendto");break;}if(res-4 < 512){printf("======= 文件下载完毕 =======\n");break;}}}else if(5 == buf[1])//错误包{printf("MSG_ERR: code[%d] msg[%s] __%d__\n", \ntohs(*(unsigned short*)(buf+2)), buf+4, __LINE__);break;}}close(fd);return 0;
}
int main(int argc, const char *argv[])
{//创建报式套接字int cfd = socket(AF_INET,SOCK_DGRAM,0);if(cfd < 0){ERR_MSG("socket");return -1;}printf("cfd = %d\n",cfd);//绑定客户端的地址信息结构体到套接字上--->非必须绑定//若不绑定,则操作系统会给客户端绑定上客户端所在的主机IP以及随机端口(49151~65535)//填充地址信息结构体,真实的地址信息结构体根据地址族制定//AF_INET:man 7 ip//要连接哪个服务器,就填对应服务器的IP和端口struct sockaddr_in sin;sin.sin_family = AF_INET; //必须填AF_INETsin.sin_port = htons(PORT); //端口号:填服务器绑定端口号sin.sin_addr.s_addr = inet_addr(IP); //IP地址:服务器绑定的IP地址char c = 0;while(1){ printf("-------------------------\n");printf("---------1.下载----------\n");printf("---------2.上传----------\n");printf("-------------------------\n");printf("请输入>>> ");c = getchar();while(getchar()!=10);switch(c){case '1' :download(cfd,sin);break;case '2' :upload(cfd,sin);break;default :printf("输入错误,请重新输入\n");break;}while(getchar()!=10);}//关闭所有文件描述符close(cfd);return 0;
}