#include
#define PORT 69
#define N 516
int do_download(int , struct sockaddr_in );
int do_upload(int sfd, struct sockaddr_in sin);
int main(int argc, const char *argv[])
{
if(argc < 2)
{
printf("请输入IP\n");
return -1;
}
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
perror("socket");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(argv[1]);
char choose;
while(1)
{
printf("******************\n");
printf("******1.下载******\n");
printf("******2.上传******\n");
printf("******3.退出******\n");
printf("******************\n");
printf("请输入>>>");
scanf("%c", &choose);
while(getchar()!=10);
switch(choose)
{
case '1':
do_download(sfd, sin);
break;
case '2':
do_upload(sfd, sin);
break;
case '3':
goto END;
break;
default:
printf("输入错误\n");
}
}
END:
close(sfd);
return 0;
}
int do_upload(int sfd, struct sockaddr_in sin)
{
char filename[20] = "";
printf("请输入要上传的文件名>>>");
fgets(filename, 20, stdin);
filename[strlen(filename)-1] = 0;
int fd = open(filename, O_RDONLY);
if(fd < 0)
{
if(errno == ENOENT)
{
printf(">>>文件不存在,请重新输入<<<\n");
return -2;
}
else
{
ERR_MSG("open");
return -1;
}
}
char buf[N] = "";
int size = sprintf(buf, "%c%c%s%c%s%c", 0, 2, filename, 0, "octet", 0);
if(sendto(sfd, buf, size, 0, (struct sockaddr*)&sin, sizeof(sin))<0)
{
ERR_MSG("sendto");
return -1;
}
int recv_len;
unsigned short num = 0;
socklen_t addrlen = sizeof(sin);
while(1)
{
bzero(buf, N);
recv_len = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, &addrlen);
if(recv_len < 0)
{
ERR_MSG("recvfrom");
return -1;
}
if(4 == buf[1])
{
if(num == ntohs(*(unsigned short*)(buf+2)))
{
buf[1] = 3;
num++;
*(unsigned short*)(buf+2) = htons(num);
int res = read(fd, buf+4, N-4);
if(res < 0)
{
ERR_MSG("read");
return -1;
}
else if(0 == res)
{
printf("-----文件上传完毕-----\n");
break;
}
if(sendto(sfd, buf, res+4, 0, (struct sockaddr*)&sin, sizeof(sin)) <0)
{
ERR_MSG("sendto");
return -1;
}
}
else
{
printf("-----文件上传失败,请检查网络环境-----\n");
break;
}
}
else if(5 == buf[1])
{
printf("-----ERROR:%s-----\n", buf+4);
break;
}
}
return 0;
}
int do_download(int sfd, struct sockaddr_in sin)
{
char filename[20] = "";
printf("请输入要下载的文件名>>>");
fgets(filename, 20, stdin);
filename[strlen(filename)-1] = 0;
char buf[N] = "";
int size = sprintf(buf, "%c%c%s%c%s%c", 0, 1, filename, 0, "octet", 0);
if(sendto(sfd, buf, size, 0, (struct sockaddr*)&sin, sizeof(sin))<0)
{
ERR_MSG("sendto");
return -1;
}
int flag = 0;
int fd=-1 ;
ssize_t recv_len;
unsigned short num = 1;
socklen_t addrlen = sizeof(sin);
while(1)
{
bzero(buf, N);
recv_len = recvfrom(sfd, buf, N, 0, (struct sockaddr*)&sin, &addrlen);
if(recv_len < 0)
{
ERR_MSG("recvfrom");
return -1;
}
if(3 == buf[1])
{
if(0 == flag)
{
fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0664);
if(fd < 0)
{
ERR_MSG("open");
return -1;
}
flag = 1;
}
if(htons(num) == *(unsigned short*)(buf+2))
{
if(write(fd, buf+4, recv_len-4) < 0)
{
printf("fd:%d recv_len=%ld\n", fd, recv_len);
ERR_MSG("write");
break;
}
buf[1] = 4;
if(sendto(sfd, buf, 4, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
}
if(recv_len < 512+2+2)
{
printf("-----文件下载完毕!!-----\n");
break;
}
num++;
}
}
else if(5 == buf[1])
{
printf("-----ERROR:%s-----\n", buf+4);
break;
}
}
close(fd);
return 0;
}