网络编程-UDP-从服务器下载图片

主函数:

#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:准备接收数据包
文件下载完毕
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coding Peasant

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值