2023/4/21


1. 将UDP服务器客户端脱离笔记重新搭建(上交)
2. 完成下载上传(上交,必须完成下载,上传尽力完成)

下载客户端

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<sys/stat.h>

#define ERR_MSG(msg) do{\
	fprintf(stderr,"Line:%d",__LINE__);\
	perror(msg);\
}while(0)

#define IP "192.168.0.196"  //本机IP
#define PORT 69



int main(int argc, const char *argv[])
{
	//创建套接字
	int client_fd = socket(AF_INET,SOCK_DGRAM,0);
	if(client_fd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success server_fd=%d__%d__\n",client_fd,__LINE__);

	//填充客户端的地址信息结构体
	struct sockaddr_in cin;
	cin.sin_family = AF_INET;
	cin.sin_port = htons(PORT);
	cin.sin_addr.s_addr = inet_addr(IP);
	//	sockaddr_in addrlen = sizeof(cin);

	char buf[516] = {0};
	bzero(buf,sizeof(buf));

	unsigned short *p1 = (unsigned short*)buf;  		//操作码
	*p1 = htons(1);

	char *p2 = buf+2;
	char filename[128] = "" ;
	printf("请输入要下载的文件名>>>>>");
	scanf("%s",filename);
	getchar();
	strcpy(p2,filename);  	//要下载的文件名

	char *p3 = p2+strlen(p2);
	*p3 = 0;

	char *p4 = p3+1;
	strcpy(p4,"octet"); 		//模式

	char *p5 = p4+strlen(p4);
	*p5 = 0;

	int size = 2+1+1+strlen(p2)+strlen(p4);
	//请求下载
	ssize_t ret = sendto(client_fd,buf,size,0,(struct sockaddr*)&cin,sizeof(cin)); 
	if(ret < 0)
	{
		ERR_MSG("sendto");
		return -1;
	}
	printf("sendto success\n");

	socklen_t addrlen = sizeof(cin);

	//打开文件
	int fd;
	ssize_t recv_len;
	ssize_t res = 0;
	unsigned short num = 0;

	while(1)
	{
		bzero(buf,sizeof(buf));
		recv_len = recvfrom(client_fd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
		if(recv_len < 0)
		{
			ERR_MSG("recvfrom");
			return -1;
		}
		printf("recvfrom success\n");

		if(buf[1] == 0x05){
			fprintf(stderr,"download erro____%d____%s\n",ntohs(*(short*)(buf+2)),buf+4);
			break;
		}

		else if(buf[1] == 0x03)					//此时正常获取到数据包
		{
			printf("获取\n");

			fd = open(filename, O_WRONLY|O_CREAT|O_APPEND, 0666);
			if(fd < 0)
			{
				ERR_MSG("open");
				return -1; 
			}
			//判断块编号
		/*	if(*(unsigned short*)(buf+2) == htons(num))
			{*/
				printf("kuaibianhao\n");

				if(write(fd,buf+4,recv_len-4) < 0)
				{
					ERR_MSG("write");
					ret = -1;
					break;
				}
				printf("write success\n");

				buf[1] = 0x04;
				if(sendto(client_fd,buf,4,0,(struct sockaddr*)&cin,sizeof(cin)) < 0)
				{
					ERR_MSG("sendto");
					return -1;
				}

				printf("sendto success\n");
				if(recv_len < 516)
				{
					break;
				}
				
			
			close (fd);	
		}		
	}


	printf("下载完成\n");

	//关闭套接字
	close(client_fd);

	return 0;


}

UDP服务器


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>

#define ERR_MSG(msg) do{\
	fprintf(stderr,"Line:%d",__LINE__);\
	perror(msg);\
}while(0)

#define IP "192.168.0.141"  //本机IP
#define PORT 2222

int main(int argc, const char *argv[])
{
	//创建套接字
	int server_fd = socket(AF_INET,SOCK_DGRAM,0);
	if(server_fd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success server_fd=%d__%d__\n",server_fd,__LINE__);

	//填充服务器的地址信息结构体
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);

	//绑定---->>>>>必须绑定
	if(bind(server_fd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success __%d__\n",__LINE__);

	//存储客户端地址信息
	struct sockaddr_in cin;
	socklen_t cin_addrlen = sizeof(cin);

	char ch[128] = "" ;
	ssize_t res = 0;
	while(1)
	{
		bzero(ch,sizeof(ch));

		//接受数据
		res = recvfrom(server_fd,ch,sizeof(ch),0,(struct sockaddr*)&cin,&cin_addrlen);
		if(res < 0)
		{
			ERR_MSG("recvfrom");
			return -1;
		}
		printf(":%s\n",ch);

		//发送数据
		strcat(ch,"*-*");
		sendto(server_fd,ch,sizeof(ch),0,(struct sockaddr*)&cin,sizeof(cin));
	}

	//关闭套接字
	close(server_fd);

	return 0;
}

UDP客户端

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
 
#define ERR_MSG(msg){\
	fprintf(stdout,"cline----->%d",__LINE__);\
	perror(msg);\
}
 
 
int main(int argc,const char * argv[])
{
 
	//1.socket
	int cfd = socket(AF_INET,SOCK_DGRAM,0);
 
	
	
	ssize_t sendto_return;
	ssize_t recvfrom_return;
	char buf[128];
 
	struct sockaddr_in s;
	s.sin_family = AF_INET;
	s.sin_port = htons(6666);
	s.sin_addr.s_addr = inet_addr(argv[1]);
 
	int s_len = sizeof(s);
	while(1)
	{
	bzero(buf,sizeof(buf));
 
	fgets(buf,sizeof(buf),stdin);
	//3.sendto
	sendto_return = sendto(cfd,buf,sizeof(buf),0,(struct sockaddr *)&s,s_len);
	if(sendto_return < 0)
	{
		ERR_MSG("sendto");
		return -1;
	}
 
 
	bzero(buf,sizeof(buf));
	//4.recvfrom
	recvfrom_return = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr *)&s,&s_len);
 
	if(recvfrom_return < 0)
	{
		ERR_MSG("recvfrom");
		return -1;
	}
 
 
		printf("客户端收到消息是:%s\n",buf);
 
 
	}
 
	close(cfd);
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值