?
#include <func.h>
#define PORT 69
#define IP "192.168.0.103"
int do_download(int sfd, struct sockaddr_in sin);
int do_upload(int sfd,struct sockaddr_in sin);
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("sfd");
return -1;
}
printf("socket create succes sfd=%d __%d__\n", sfd, __LINE__);
//允许端口快速被复用---监测端口号是否真的有进程在占用,如果没有,则快速复用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速复用成功\n");
//定义并填充地址信息结构体,方便发送数据
//真实的地址信息结构体根据地址族指定 AF_INET :man ip(7)
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT); //端口号的网络字节序 : 1024-49151
sin.sin_addr.s_addr = inet_addr(IP); //本机号的网络字节序列
char choose = 0;
while(1)
{
printf("-------------------------\n");
printf("----------1.下载---------\n");
printf("----------2.上传---------\n");
printf("----------3.退出---------\n");
printf("-------------------------\n");
printf("请输入---->");
choose = getchar();
while(getchar()!= '\n');
switch(choose)
{
case '1' :
//下载
do_download(sfd, sin);
break;
case '2':
//上传 do_upload
do_upload(sfd, sin);
break;
case '3':
close(sfd);
return 0;
//退出
default:
printf("输入错误请重新输出\n");
}
}
//关闭套接字
if(close(sfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
int do_download(int sfd, struct sockaddr_in sin)
{
char cuf[512] = "";
char brr[200] = "";
printf("-------->请输入下载的文件\n");
scanf("%s", brr);
short *ptr1 = (short *)cuf;
*ptr1 = htons(1);
char *ptr2 = cuf + 2;
strcpy(ptr2, brr);
char *ptr4 =ptr2+strlen(ptr2)+1;
strcpy(ptr4,"octet");
int len = 2+strlen(brr) + 1 + strlen(ptr4) + 1;
if(sendto(sfd,cuf,len,0,(struct sockaddr*)&sin,sizeof(sin)) < 0 )
{
ERR_MSG("sendto");
return-1;
}
//接受返回的地址信息结构体
struct sockaddr_in cin;
socklen_t clen = sizeof(cin);
char buf[516];//数据包
short * pcr1 = (short *)buf;
*pcr1 = htons(3);
short * pcr2 = pcr1 + 1;
char arr[4];//ack
short * ptrc1 = (short *)arr;
*ptrc1 = htons(4);
short * ptrc2 = ptrc1 + 1;
ssize_t ter = 0;
int fp = open("./1.png", O_WRONLY|O_CREAT|O_APPEND|O_TRUNC,0664);
while(1)
{
//接受数据
ter = recvfrom(sfd,buf,516,0,(struct sockaddr*)&cin,&clen);
printf("ter=%ld __%d__\n", ter, __LINE__);
*ptrc2 = *pcr2;
write(fp,&buf[4], ter-4);
sendto(sfd,arr,sizeof(arr), 0, (struct sockaddr*)&cin,sizeof(cin));
if(ter < 516)
{
printf("下载成功\n");
break;
}
}
return 0;
}
int do_upload(int sfd,struct sockaddr_in sin)
{
int ph = open("./2.png", O_RDONLY);
char cuf[512] = "";
char brr[200] = "";
printf("-------->请输入传输的文件\n");
scanf("%s", brr);
short *ptr1 = (short *)cuf;
*ptr1 = htons(2); //写请求
char *ptr2 = cuf + 2;
strcpy(ptr2, brr);
char *ptr4 =ptr2+strlen(ptr2)+1;
strcpy(ptr4,"octet");
int len = 2+strlen(brr) + 1 + strlen(ptr4) + 1;
if(sendto(sfd,cuf,len,0,(struct sockaddr*)&sin,sizeof(sin)) < 0 )
{
ERR_MSG("sendto");
return-1;
}
//打包
char demo[516] = "";
short * ctr = (short*) demo;
*ctr = htons(3);
short* ctr2 = ctr+1;
char arr[4];//ack
short * ptrc1 = (short *)arr;
*ptrc1 = htons(4);
short * ptrc2 = ptrc1 + 1;
ssize_t tev = 0, ter = 0;;
int i = 1;
//定义接收的临时套接字
struct sockaddr_in cin;
socklen_t clen = sizeof(cin);
while(1)
{
//发送数据
if(recvfrom(sfd,arr,sizeof(arr),0,(struct sockaddr*)&cin,&clen)<0)//收到服务器创建的临时套接字地址
{
ERR_MSG("recvfrom");
return -1;
}
*ctr2 = htons(i++);//编号
tev = read(ph,(char*)(demo+4), 512);//读取文件
if(ter = sendto(sfd, demo,tev+4,0, (struct sockaddr*)&cin, sizeof(cin)) < 0)//发送文件
{
ERR_MSG("sendto");
return -1;
}
if(tev < 512)
{
printf("发送成功\n");
break;
}
printf("ter = %ld __%d__\n", ter, __LINE__);
}
return 0;
}