#include
#include
#include
#include
#define PORT 69
#define IP "192.168.122.137"
#define NAME "5.png"
int main(int argc, const char *argv[])
{
//创建报式套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("sfd = %d\n",cfd);
//编辑下载请求
char buf[517] = "";
char *ptr = buf;
unsigned short* p1 = (unsigned short*)buf;
*p1 = htons(1);
char *p2 = (char*)p1+2;
strcpy(p2,NAME);
char *p3 = p2 + strlen(p2)+1;
strcpy(p3,"octet");
size_t size = 2+strlen(p2)+1+strlen("octet")+1;
ssize_t res = 0;
//存储服务器地址和69端口
struct sockaddr_in sin;
socklen_t addrlen = sizeof(sin);
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//发送下载请求
if(sendto(cfd,buf,size,0,(struct sockaddr*)&sin,addrlen)< 0 )
{
ERR_MSG("sendto");
return -1;
}
int fd = open("./2.png",O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd < 0)
{
ERR_MSG("open");
return -1;
}
char* p4 =(char*)p1+4;
int i = 1;
while(i)
{
//接收数据包
res = recvfrom(cfd,buf,517,0,\
(struct sockaddr*)&sin,&addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
write(fd,p4,512);
//发送ACK
*p1 = htons(4);
size_t size_ACK = 4;
if(sendto(cfd,buf,size_ACK,0,(struct sockaddr*)&sin,addrlen)< 0 )
{
ERR_MSG("sendto");
return -1;
}
if(res < 516)
{
i = 0;
}
}
//关闭文件描述符
close(cfd);
return 0;
}
#include
#include
#include
#include
#define PORT 69
#define IP "192.168.122.137"
#define NAME "1.png"
int main(int argc, const char *argv[])
{
//创建报式套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("sfd = %d\n",cfd);
char buf[516] = "";
char *ptr = buf;
unsigned short* p1 = (unsigned short*)buf;
*p1 = htons(2);
char *p2 = (char*)p1+2;
strcpy(p2,NAME);
char *p3 = p2 + strlen(p2)+1;
strcpy(p3,"octet");
size_t size = 2+strlen(p2)+1+strlen("octet")+1;
ssize_t res = 0;
//存储服务器地址和69端口
struct sockaddr_in sin;
socklen_t addrlen = sizeof(sin);
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//发送上传请求
if(sendto(cfd,buf,size,0,(struct sockaddr*)&sin,addrlen)< 0 )
{
ERR_MSG("sendto");
return -1;
}
int fd = open("./1.png",O_RDONLY);
if(fd < 0)
{
ERR_MSG("open");
return -1;
}
char* p4 =(char*)p1+4;
//接收ACK
recvfrom(cfd,buf,4,0,\
(struct sockaddr*)&sin,&addrlen);
int i = 1;
int a = 1;
while(i)
{
//发送数据包
*p1 = htons(3);
*((short*)p2) = htons(a);
a++;
res = read(fd,p4,512);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,addrlen)< 0 )
{
ERR_MSG("sendto");
return -1;
}
//接收ACK
recvfrom(cfd,buf,4,0,\
(struct sockaddr*)&sin,&addrlen);
if(res < 512)
{
i = 0;
}
}
//关闭文件描述符
close(cfd);
return 0;
}