#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d ", __LINE__);\
perror(msg);\
}while(0)
#define SER_IP "192.168.8.103" //本机IP ifconfig
#define SER_PORT 69
int do_download(int cfd, struct sockaddr_in sin)
{
//组下载请求协议
char filename[20] = "";
char buf[516] = {0};
printf("请输入要下载的文件名>>> ");
scanf("%s", filename);
while(getchar()!=10);
short* p1 = (short*)buf;
*p1 = htons(1);
char* p2 = buf+2;
strcpy(p2, filename);
char* p4 = p2+strlen(p2)+1;
strcpy(p4, "octet");
int size = 4+strlen(p2)+strlen(p4);
//发送下载请求协议
if(sendto(cfd, buf, size, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("发送成功\n");
//变量最好初始化,且初始化为无意义的数据
int fd = -1;
socklen_t addrlen = sizeof(sin);
ssize_t res = 0;
int ret = 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");
ret = -1;
break;
}
//下列测试可以得到结果,有效操作码存储在buf[1]的位置,buf[0]的位置是0
//printf("操作码:%#x | %d %d\n", ntohs(*(short*)buf), buf[0], buf[1]);
//printf(" 快编号:%d | %d %d\n", ntohs(*(short*)(buf+2)), buf[2], buf[3]);
if(3 == buf[1]) //数据包
{
//判断数据包中的块编号是否是正确的块编号
//需要与本地记录的块编号进行对比
if(*(unsigned short*)(buf+2) == htons((num+1)))
{
num++;
if(1 == ntohs(*(unsigned short*)(buf+2)))
{
fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0664);
if(fd < 0)
{
ERR_MSG("open");
return -1;
}
}
//将接收到的数据另存
if(write(fd, buf+4, res-4) < 0)
{
ERR_MSG("write");
ret = -1;
break;
}
//回复ACK,由于ACK包和数据包前四个字节只有操作码不一致
//所以可以直接修改数据包的操作码,然后将前4个字节回复给服务器即可
buf[1] = 4;
if(sendto(cfd, buf, 4, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
ret = -1;
break;
}
//直到数据小于512个字节,实际获取到的数据包大小为res
//数据大小= res - 操作码 - 块编号
if(res-4 < 512)
{
printf("%s 文件拷贝完毕\n", filename);
break;
}
}
}
else if(5 == buf[1]) //错误包
{
fprintf(stderr, "DOWNLOAD_ERROR:%d : %s\n", ntohs(*(short*)(buf+2)), buf+4);
break;
}
}
close(fd);
return ret;
}
int uploal_func(int cfd, struct sockaddr_in sin)
{
printf("请输入上传文件名\n");
char pathname_upload[32] = "";
scanf("%s", pathname_upload);
//创建文件描述符
int upload = open(pathname_upload, O_RDONLY);
if (upload < 0)
{
ERR_MSG("open");
return -1;
}
//上传请求buf
//p1: 1读 2写
char buf[516] = "";
*(short*)buf = htons((short)2);
strcpy(buf+2, pathname_upload);
strcpy(buf+2+strlen(pathname_upload)+1, "octet");
int size = 4+strlen(pathname_upload)+strlen("octet");
if (sendto(cfd, buf, size, 0, (struct sockaddr*)&sin, (socklen_t)sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
struct sockaddr_in cin;
socklen_t cinlen = sizeof(cin);
int res = 0;
int flag = 0;
while (1)
{
//接收ACK
bzero(buf, sizeof(buf));
res = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &cinlen);
if (res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
//拆分ACK
short num1 = ntohs(*(short*)buf);
short num2 = ntohs(*(short*)(buf+2));
printf("[%s : %d] cfd = %d : 操作码:%d 块编号:%d\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), cfd, num1, num2);
if (4 == num1)
{
//数据包拼接
bzero(buf, sizeof(buf));
*(short*)buf = htons(3);
*(short*)(buf+2) = htons(num2+1);
//读取文件
res = read(upload, buf+4, 512);
if (res < 0)
{
ERR_MSG("write");
return -1;
}
size = 4+res;
printf("res = %d\n", res);
printf("size = %d\n", size);
//printf("%d %d %ld\n", num2-1, num2, strlen(num3));
//printf("%d %d %ld\n", *(short*)buf, *(short*)(buf+2), strlen(buf+4));
if (sendto(cfd, buf, size, 0, (struct sockaddr*)&cin, (socklen_t)sizeof(cin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
bzero(buf, sizeof(buf));
if (res < 512)
{
printf("上传完毕\n");
break;
}
}
if (5 == num1)
{
fprintf(stderr, "DOWNLOAD_ERROR:%d : %s\n", ntohs(*(short*)(buf+2)), buf+4);
break;
}
}
close(upload);
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;
}
//bind 非必须绑定
//如果不绑定则由操作系统自动绑定IP和端口
//填充服务器的地址信息结构体,给sendto函数使用
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET;
sin.sin_port = htons(SER_PORT); //端口号的网络字节序
sin.sin_addr.s_addr = inet_addr(SER_IP); //服务器IP地址
char c = 0;
while(1)
{
system("clear");
printf("---------------------------\n");
printf("----------1. 下载----------\n");
printf("----------2. 上传----------\n");
printf("----------3. 退出----------\n");
printf("---------------------------\n");
c = getchar();
while(getchar() != 10);
switch(c)
{
case '1':
do_download(cfd, sin);
break;
case '2':
uploal_func(cfd, sin);
break;
case '3':
goto END;
break;
default:
printf("输入错误,请重新输入\n");
}
printf("输入任意字符清屏>>>");
while(getchar()!=10);
}
END:
//关闭文件描述符
close(cfd);
return 0;
}