广播服务器和客户端 组播服务器和客户端 基于ttfp协议的上传和下载代码

组播服务器

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#define ERR_MGS(m)do{\
	fprintf(stderr,"%d\n",__LINE__);\
	perror(m);\
}while(0)
int main(int argc, const char *argv[])
{
	//基于UDP的服务器模型
	//创建套接字
	int scoketd=socket(AF_INET,SOCK_DGRAM,0);
	if(-1==scoketd){
		ERR_MGS("scoket");
		return 0;
	}
	//允许端口快速重用
	int reuse = 1;
	if(setsockopt(scoketd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))<0)
	{
		ERR_MGS("setsockopt");
		return -1;
	}
	//设置加入多播组
	struct ip_mreqn eq;
	eq.imr_multiaddr.s_addr=inet_addr("224.1.2.3");
	eq.imr_address.s_addr=inet_addr("192.168.8.214");
	eq.imr_ifindex = if_nametoindex("ens33");
	if(setsockopt(scoketd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&eq,sizeof(eq))<0){
		ERR_MGS("setsockopt1");
		return 0;
	}
	printf("加入多播组成功\n");
	printf("创建套接字成功\n");
	//绑定服务器的ip和端口
	//创建结构体变量并填充结构体
	struct sockaddr_in cin;
	cin.sin_family=AF_INET;
	cin.sin_port=htons(2222);
	cin.sin_addr.s_addr=inet_addr("224.1.2.3");
	socklen_t addrlen=sizeof(cin);
	int fd=bind(scoketd,(struct sockaddr*)&cin,addrlen);
	if(-1==fd){
		ERR_MGS("bind");
		return 0;
	}
	printf("绑定成功\n");
	//接收客户端的信息
	ssize_t res;
	char buf[128];
	struct sockaddr_in sind;
	socklen_t addrlen1=sizeof(sind);
	while(1){
		bzero(buf,sizeof(buf));
		res=recvfrom(scoketd,buf,sizeof(buf),0,(struct sockaddr*)&sind,&addrlen1);

		if(-1==res){
			ERR_MGS("recvfrom");
			break;
		}
		printf("[%s | %d] %s\n",inet_ntoa(sind.sin_addr),ntohs(sind.sin_port),buf);
	}
	//关闭文件描述符
	close(scoketd);

	return 0;
}

//组播客户端代码

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

#define ERR_MGS(m)do{\
	fprintf(stderr,"%d\n",__LINE__);\
	perror(m);\
}while(0)
int main(int argc, const char *argv[])
{
	//基于UDP的客户端模型
	//创建套接字
	int scoketd=socket(AF_INET,SOCK_DGRAM,0);
	if(-1==scoketd){
		ERR_MGS("scoket");
		return 0;
	}
	//允许端口快速重用
	int reuse = 1;
	if(setsockopt(scoketd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))<0)
	{
		ERR_MGS("setsockopt");
		return -1;
	}
	printf("创建套接字成功\n");
	//绑定客户端的ip和端口
	//创建结构体变量并填充结构体
	struct sockaddr_in cin;
	cin.sin_family=AF_INET;
	cin.sin_port=htons(8888);
	cin.sin_addr.s_addr=inet_addr("192.168.8.214");
	socklen_t addrlen=sizeof(cin);
	int fd=bind(scoketd,(struct sockaddr*)&cin,addrlen);
	if(-1==fd){
		ERR_MGS("bind");
		return 0;
	}
	printf("绑定成功\n");
	//向服务器的发送信息
	ssize_t res;
	char buf[128];
	struct sockaddr_in sind;
	sind.sin_family=AF_INET;
	sind.sin_port=htons(2222);
	sind.sin_addr.s_addr=inet_addr("224.1.2.3");

	socklen_t addrlen1=sizeof(sind);
	while(1){
		printf("请输入>>>\n");
		bzero(buf,sizeof(buf));
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]=0;
		res=sendto(scoketd,buf,sizeof(buf),0,(struct sockaddr*)&sind,addrlen1);
		if(-1==res){
			ERR_MGS("sendto");
			break;
		}
		printf("发送信息成功\n");
	}
	//关闭文件描述符
	close(scoketd);

	return 0;
}

//广播服务器

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

#define ERR_MGS(m)do{\
	fprintf(stderr,"%d\n",__LINE__);\
	perror(m);\
}while(0)
int main(int argc, const char *argv[])
{
	//基于UDP的服务器模型
	//创建套接字
	int scoketd=socket(AF_INET,SOCK_DGRAM,0);
	if(-1==scoketd){
		ERR_MGS("scoket");
		return 0;
	}
	//允许端口快速重用
	int reuse = 1;
	if(setsockopt(scoketd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))<0)
	{
		ERR_MGS("setsockopt");
		return -1;
	}
	printf("创建套接字成功\n");
	//绑定服务器的ip和端口
	//创建结构体变量并填充结构体
	struct sockaddr_in cin;
	cin.sin_family=AF_INET;
	cin.sin_port=htons(2222);
	cin.sin_addr.s_addr=inet_addr("192.168.8.255");
	socklen_t addrlen=sizeof(cin);
	int fd=bind(scoketd,(struct sockaddr*)&cin,addrlen);
	if(-1==fd){
		ERR_MGS("bind");
		return 0;
	}
	printf("绑定成功\n");
		//接收客户端的信息
	ssize_t res;
	char buf[128];
	struct sockaddr_in sind;
	socklen_t addrlen1=sizeof(sind);
	while(1){
		bzero(buf,sizeof(buf));
		res=recvfrom(scoketd,buf,sizeof(buf),0,(struct sockaddr*)&sind,&addrlen1);
	
		if(-1==res){
			ERR_MGS("recvfrom");
			break;
		}
		printf("[%s | %d] %s\n",inet_ntoa(sind.sin_addr),ntohs(sind.sin_port),buf);
		//发送信息给客户端
		strcat(buf,"*_*");
		res=sendto(scoketd,buf,sizeof(buf),0,(struct sockaddr*)&sind,addrlen1);
		if(-1==res){
			ERR_MGS("sendto");
			break;
		}
		printf("发送信息成功\n");
	}
		//关闭文件描述符
		close(scoketd);
	
	return 0;
}

//广播服务器

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

#define ERR_MGS(m)do{\
	fprintf(stderr,"%d\n",__LINE__);\
	perror(m);\
}while(0)
int main(int argc, const char *argv[])
{
	//基于UDP的客户端模型
	//创建套接字
	int scoketd=socket(AF_INET,SOCK_DGRAM,0);
	if(-1==scoketd){
		ERR_MGS("scoket");
		return 0;
	}
	//允许端口快速重用
	int reuse = 1;
	if(setsockopt(scoketd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))<0)
	{
		ERR_MGS("setsockopt");
		return -1;
	}
	//设置允许广播
	int rep=1;
	socklen_t len1=sizeof(rep);
	int sock=setsockopt(scoketd,SOL_SOCKET,SO_BROADCAST,&rep,len1);
	if(-1==sock){
		perror("setsockopt1");
		return 0;
	}
	printf("允许广播成功\n");
	printf("创建套接字成功\n");
	//绑定客户端的ip和端口
	//创建结构体变量并填充结构体
	struct sockaddr_in cin;
	cin.sin_family=AF_INET;
	cin.sin_port=htons(6666);
	cin.sin_addr.s_addr=inet_addr("192.168.8.214");
	socklen_t addrlen=sizeof(cin);
	int fd=bind(scoketd,(struct sockaddr*)&cin,addrlen);
	if(-1==fd){
		ERR_MGS("bind");
		return 0;
	}
	printf("绑定成功\n");
	//向服务器的发送信息
	ssize_t res;
	char buf[128];
	struct sockaddr_in sind;
	sind.sin_family=AF_INET;
	sind.sin_port=htons(2222);
	sind.sin_addr.s_addr=inet_addr("192.168.8.255");

	socklen_t addrlen1=sizeof(sind);
	while(1){
		printf("请输入>>>\n");
		bzero(buf,sizeof(buf));
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]=0;
		res=sendto(scoketd,buf,sizeof(buf),0,(struct sockaddr*)&sind,addrlen1);
		if(-1==res){
			ERR_MGS("sendto");
			break;
		}
		printf("发送信息成功\n");

	}
	//关闭文件描述符
	close(scoketd);

	return 0;
}

//基于ttfp协议的上传和下载代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define ERR_MGS(m)do{\
	fprintf(stderr,"%d\n",__LINE__);\
	perror(m);\
}while(0)
int download(int scoketd,struct sockaddr_in sind,socklen_t addrlen1);
int upload(int scoketd,struct sockaddr_in sind,socklen_t addrlen1);
int main(int argc, const char *argv[])
{
	//基于UDP的客户端模型
	//创建套接字
	int scoketd=socket(AF_INET,SOCK_DGRAM,0);
	if(-1==scoketd){
		ERR_MGS("scoket");
		return 0;
	}
	//允许端口快速重用
	int reuse = 1;
	if(setsockopt(scoketd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))<0)
	{
		ERR_MGS("setsockopt");
		return -1;
	}
	printf("创建套接字成功\n");
	//绑定客户端的ip和端口
	//创建结构体变量并填充结构体
	struct sockaddr_in cin;
	cin.sin_family=AF_INET;
	cin.sin_port=htons(6666);
	cin.sin_addr.s_addr=inet_addr("192.168.8.214");
	socklen_t addrlen=sizeof(cin);
	int fd=bind(scoketd,(struct sockaddr*)&cin,addrlen);
	if(-1==fd){
		ERR_MGS("bind");
		return 0;
	}
	printf("绑定成功\n");
	int option;

	struct sockaddr_in sind;
	sind.sin_family=AF_INET;
	sind.sin_port=htons(69);
	sind.sin_addr.s_addr=inet_addr("192.168.8.238");

	socklen_t addrlen1=sizeof(sind);

	while(1){
		printf("*****1.下载*****\n");
		printf("*****2.上传*****\n");
		printf("*****3.退出*****\n");
		printf("请输入选项>>>\n");
		scanf("%d",&option);
		while(getchar()!='\n');
		switch(option){
		case 1:
			download(scoketd,sind,addrlen1);
			break;
		case 2:
			upload(scoketd,sind,addrlen1);
			break;
		case 3:
			goto END;
		default :
			printf("输入格式错误请重新输入\n");
			break;

		}
	}
END:
	//关闭文件描述符
	close(scoketd);
	return 0;
}
int download(int scoketd,struct sockaddr_in sind,socklen_t addrlen1){
	//向服务器的发送信息
	ssize_t res;
	//组建发送协议包
	char buf[516]="";
	short* p=(short*)buf;
	*p=htons(1);
	char* p1 = buf+2;
	printf("请输入要下载的文件名\n");
	char name[20]="";
	scanf("%s",name);
	strcpy(p1,name);
	char* p2=p1+strlen(p1);
	*p2=0;
	char* p3=p2+1;
	strcpy(p3,"octet");
	char* p4=p3+strlen(p3);
	*p4=0;

	//定义记录数组长度大小的变量记录数组长度
	int size=2+strlen(p1)+1+strlen(p3)+1;
	printf("p1=%s p3=%s size=%d\n",p1,p3,size);
	res=sendto(scoketd,buf,size,0,(struct sockaddr*)&sind,addrlen1);
	if(-1==res){
		ERR_MGS("sendto");
		return 0;
	}
	printf("发送信息成功\n");
	//打开一个文件用来接收传输过来的信息
	umask(0);
	int falg=0;
	int sdp;
	int num=0;
	while(1){
		
		bzero(buf,sizeof(buf));
		res=recvfrom(scoketd,buf,sizeof(buf),0,(struct sockaddr*)&sind,&addrlen1);

		if(-1==res){
			ERR_MGS("recvfrom");
			break;
		}
	
		if(buf[1]==3){
			if(htons(num+1) == *(unsigned short*)(buf+2)){
				num++;
				if(0==falg){
					sdp=open("./7.png",O_RDWR|O_CREAT|O_TRUNC, 0777);
					if(sdp<0){
						ERR_MGS("open");
						return 0;
					}
				}
				falg=1;
				if(write(sdp,buf+4,res-4)<0){
					ERR_MGS("write");
					return 0;
				}
				buf[1]=4;
			

				if(sendto(scoketd,buf,4,0,(struct sockaddr*)&sind,addrlen1)<0){
					ERR_MGS("sendto");
					return 0;
				}
				if(res-4<512){
					printf("文件下载完毕\n");
					break;
				}
			
			}

		}
	
		if(buf[1]==5){
			printf("发生未知错误 差错码=%d,错误信息为:%s\n",ntohs(*(unsigned short*)(buf+2)),buf+4);
			close(sdp);

			break;
		}


	}
	close(sdp);
	return 0;


}
int upload(int scoketd,struct sockaddr_in sind,socklen_t addrlen1){
	//向服务器的发送信息
	ssize_t res;
	//组建发送协议包
	char buf[516]="";
	short* p=(short*)buf;
	*p=htons(2);
	char* p1 = buf+2;
	printf("请输入要上传的文件名\n");
	char name[20]="";
	scanf("%s",name);
	strcpy(p1,name);
	while(getchar()!='\n');
	char* p2=p1+strlen(p1);
	*p2=0;
	char* p3=p2+1;
	strcpy(p3,"octet");
	char* p4=p3+strlen(p3);
	*p4=0;

	//定义记录数组长度大小的变量记录数组长度
	int size=2+strlen(p1)+1+strlen(p3)+1;
	printf("p1=%s p3=%s size=%d\n",p1,p3,size);
	res=sendto(scoketd,buf,size,0,(struct sockaddr*)&sind,addrlen1);
	if(-1==res){
		ERR_MGS("sendto");
		return 0;
	}
	printf("发送信息成功\n");
	//打开一个文件用来接收传输过来的信息
	umask(0);
	int falg=0;
	int sdp;
	int num=0;
	int i=1;
	ssize_t std;
	while(1){
		
		bzero(buf,sizeof(buf));
		res=recvfrom(scoketd,buf,sizeof(buf),0,(struct sockaddr*)&sind,&addrlen1);

		if(-1==res){
			ERR_MGS("recvfrom");
			break;
		}
	
		if(buf[1]==4){
			if(htons(num) == *(unsigned short*)(buf+2)){
				num++;
				if(0==falg){
					sdp=open(name,O_RDONLY);
					if(sdp<0){
						ERR_MGS("open");
						return 0;
					}
				}
				falg=1;
				if((std=read(sdp,buf+4,512))<0){
					ERR_MGS("write");
					return 0;
				}
				buf[1]=3;
				buf[3]=i;
			
				if(sendto(scoketd,buf,std+4,0,(struct sockaddr*)&sind,addrlen1)<0){
					ERR_MGS("sendto");
					return 0;
				}
				i++;
				if(std<512){
					printf("文件上传完毕\n");
					break;
				}
			
			}

		}
	
		if(buf[1]==5){
			printf("发生未知错误 差错码=%d,错误信息为:%s\n",ntohs(*(unsigned short*)(buf+2)),buf+4);
			close(sdp);

			break;
		}


	}
	close(sdp);
	return 0;



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值