主函数:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__:", __LINE__);\
perror(msg);\
}while(0)
#define PORT 69
#define IP "192.168.31.87"
int main(int argc, const char *argv[])
{
if (argc < 2)
{
fprintf(stdout,"please enter filename !!\n");
return -1;
}
//-----------------------------创建报式套接字---------------------------------
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//允许端口快速重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用\n");
//填充地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET
sin.sin_port = htons(PORT); //网络字节序的端口号1024~49151
sin.sin_addr.s_addr = inet_addr(IP); //本机IP,ifconfig查找
socklen_t addrlen = sizeof(sin);
char buf[1024] = "";
int flag = 0;
unsigned short num = 0;
ssize_t res = 0;
//-------------------------------打开对应文件-----------------------------------
char newname[20] = "./";
strcat(newname,argv[1]);
size_t fd = -1;
//----------------------------发送的文件请求格式----------------------------------
char *ptr = buf;
//操作码
short int *pa = (short int*)ptr;
*pa = htons(1);
//文件名
char *pb = ptr+2;
strcpy(pb,argv[1]);
//格式 0
char *pc = pb+strlen(argv[1]);
*pc = 0;
//模式
char *pd = pc+1;
strcpy(pd,"octet");
//格式 0
char *pe = pd+strlen("octet");
*pe = 0;
//计算发送请求大小
int size = 2+strlen(pb)+1+strlen("octet")+1;
//-------------------------------发送下载请求----------------------------------
if(sendto(sfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
/*接收数据
res = recvfrom(sfd, buf, sizeof(buf), 0,(struct sockaddr*)&cin,&addrlen);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
//printf("[%s:%d] : %s\n", inet_ntoa(cin.sin_addr),ntohs(cin.sin_port), buf);
//printf(" : %s\n",buf);
printf("new port : %d\n",ntohs(cin.sin_port));
printf("new IP : %s\n",inet_ntoa(cin.sin_addr));
*/
char data[512] = "";
//----------------------------------接收数据------------------------------------
while(1)
{
bzero(buf,sizeof(buf));
printf("%d:准备接收数据包\n",__LINE__);
//接收数据包
res = recvfrom(sfd, buf, sizeof(buf), 0,(struct sockaddr*)&sin,&addrlen);
if (res < 0)
{
ERR_MSG("recvfrom");
close(fd);
return -1;
}
if (3 == buf[1])
{
if (0 == flag)
{
fd = open(newname,O_WRONLY|O_CREAT|O_TRUNC);
if (fd < 0)
{
ERR_MSG("open");
return -1;
}
flag = 1;
}
}
if (htons(num+1) == *(unsigned short*)(buf+2))
{
num++;
if (write(fd,buf+4,res-2-2) < 0)
{
ERR_MSG("write");
close(fd);
return -1;
}
}
buf[1] = 4;
if (sendto(sfd,buf,4,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("sendto");
close(fd);
return -1;
}
if (res-2-2<512)
{
printf("文件下载完毕\n");
break;
}
else if(5 == buf[1]) //错误包
{
printf("ERROR [%d] : %s\n", ntohs(*(unsigned short*)(buf+2)), buf+4);
close(fd);
return -1;
}
else
{
printf("未知类型的数据 [%d]\n", buf[1]);
}
}
//关闭文件描述符
close(sfd);
return 0;
}
测试:
ubuntu@ubuntu:网络编程$ ./a.out 5.png
允许端口快速重用
118:准备接收数据包
未知类型的数据 [4]
118:准备接收数据包
未知类型的数据 [4]
118:准备接收数据包
未知类型的数据 [4]
118:准备接收数据包
未知类型的数据 [4]
118:准备接收数据包
未知类型的数据 [4]
118:准备接收数据包
未知类型的数据 [4]
118:准备接收数据包
文件下载完毕