UDP的广播模型,组播模型。TCP的多进程并发服务器

广播模型---发送方

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


#define PORT 6666
#define IP "192.168.124.255"
#define ERR_MSG(msg) {fprintf(stderr,"%d__",__LINE__);perror(msg);}

int main(int argc, const char *argv[])
{
	//创建报式套接字
	int sfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sfd<0)
	{
		ERR_MSG("sockte");
		return -1;
	}
	printf("报式套接字创建成功,sfd=%d\n",sfd);

	//允许广播
	int reuse = 1;
	if(setsockopt(sfd, SOL_SOCKET, SO_BROADCAST, &reuse, sizeof(reuse)) < 0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("允许广播成功 \n");

	//填充服务器的地址信息结构体,给bind使用
	//真实的地址信息结构体根据地址族指定
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);



	char buf[128]="";
	struct sockaddr_in cin;
	socklen_t addlen=sizeof(cin);
	while(1)
	{
		//发送数据
		printf("请输入数据>>>");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]='\0';

		if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0)
		{
			ERR_MSG("sendto");
			return -1;
		}
		printf("发送成功\n");

	}
	return 0;
}

广播模型---接收方

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


#define PORT 6666
#define IP "192.168.124.255"
#define ERR_MSG(msg) {fprintf(stderr,"%d__",__LINE__);perror(msg);}

int main(int argc, const char *argv[])
{
	//创建报式套接字
	int sfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sfd<0)
	{
		ERR_MSG("sockte");
		return -1;
	}
	printf("报式套接字创建成功,sfd=%d\n",sfd);

	//允许端口快速被复用
	int reuse = 1;
	if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("允许端口快速重用\n");

	//填充服务器的地址信息结构体,给bind使用
	//真实的地址信息结构体根据地址族指定
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);
	//绑定服务器的地址信息,必须绑定
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("绑定成功\n");


	char buf[128]="";
	struct sockaddr_in cin;
	socklen_t addlen=sizeof(cin);
	while(1)
	{
		//接收数据
		if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addlen)<0)
		{
			ERR_MSG("recvfrom");
			return -1;
		}
		printf("[%s : %d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
	}
	return 0;
}

组播模型----发送方

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


#define PORT 6666
#define IP "224.1.2.3"
#define ERR_MSG(msg) {fprintf(stderr,"%d__",__LINE__);perror(msg);}

int main(int argc, const char *argv[])
{
	//创建报式套接字
	int sfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sfd<0)
	{
		ERR_MSG("sockte");
		return -1;
	}
	printf("报式套接字创建成功,sfd=%d\n",sfd);

	//允许端口快速被复用
	int reuse = 1;
	if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("允许端口快速重用\n");

	//填充服务器的地址信息结构体,给bind使用
	//真实的地址信息结构体根据地址族指定
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);



	char buf[128]="";
	struct sockaddr_in cin;
	socklen_t addlen=sizeof(cin);
	while(1)
	{
		//发送数据
		printf("请输入数据>>>");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]='\0';

		if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0)
		{
			ERR_MSG("sendto");
			return -1;
		}
		printf("发送成功\n");

	}
	return 0;
}

组播模型----接收方

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


#define PORT 6666
#define LOL_IP "192.168.124.128"
#define GRP_IP "224.1.2.3"
#define ERR_MSG(msg) {fprintf(stderr,"%d__",__LINE__);perror(msg);}

int main(int argc, const char *argv[])
{
	//创建报式套接字
	int sfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sfd<0)
	{
		ERR_MSG("sockte");
		return -1;
	}
	printf("报式套接字创建成功,sfd=%d\n",sfd);

	//允许端口快速被复用
	int reuse = 1;
	if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("允许端口快速重用\n");

	//填充服务器的地址信息结构体,给bind使用
	//真实的地址信息结构体根据地址族指定
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(GRP_IP);
	//绑定服务器的地址信息,必须绑定
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("绑定成功\n");


	//加入多播组
	struct ip_mreqn mq;
	mq.imr_multiaddr.s_addr=inet_addr(GRP_IP);
	mq.imr_address.s_addr=inet_addr(LOL_IP);
	mq.imr_ifindex=0;
	if(setsockopt(sfd, IPPROTO_IP,IP_ADD_MEMBERSHIP, &mq,sizeof(mq)) < 0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("加入小组[%s:%d]成功\n",GRP_IP,PORT);


	char buf[128]="";
	struct sockaddr_in cin;
	socklen_t addlen=sizeof(cin);
	while(1)
	{
		//接收数据
		if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addlen)<0)
		{
			ERR_MSG("recvfrom");
			return -1;
		}
		printf("[%s : %d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
	}
	return 0;
}

TCP的多进程并发服务器

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

#define ERR_F(msg) {fprintf(stderr,"__%d__\n",__LINE__);perror(msg);}
#define IP "192.168.124.128"
#define PORT 8888

void headnel()
{
	while(waitpid(-1,NULL,WNOHANG)>0);
}

int main(int argc, const char *argv[])
{
	//回收僵尸进程
	if(signal(17,headnel)==SIG_ERR)
	{
		ERR_F("signal");
		return -1;
	}
	int sfd;
	sfd=socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_F("socket");
		return -1;
	}
	printf("流式套接字创建完成\n");

	//允许端口快速被复用
	int reuse = 1;
	if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
	{
		ERR_F("setsockopt");
		return -1;
	}
	printf("允许端口快速重用\n");



	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);

	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		ERR_F("bind");
		return -1;
	}
	printf("绑定成功\n");

	if(listen(sfd,128)<0)
	{
		ERR_F("listen");
		return -1;
	}
	printf("监听成功\n");

	while(1)
	{
		struct sockaddr_in cin;
		socklen_t addelen=sizeof(cin);
		int newfd=accept(sfd,(struct sockaddr*)&cin,&addelen);
		if(newfd<0)
		{
			ERR_F("accept");
			return -1;
		}
		printf("[%s:%d]客户端链接成功\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));

		pid_t pid;
		pid=fork();
		if(pid==0)
		{
			close(sfd);
			ssize_t res;
			char buf[128]="";
			while(1)
			{
				//接收数据
				res=recv(newfd,buf,sizeof(buf),0);
				if(res<0)
				{
					ERR_F("recv");
					return -1;
				}
				else if(0==res)
				{
					printf("客户端[%s:%d]关闭\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
					return -1;
				}
				printf("[%s:%d] newfd=%d :%s\n",\
							inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);

				//发送数据
				strcat(buf,"***");
				if(send(newfd,buf,sizeof(buf),0)<0)
				{
					ERR_F("send");
					return -1;
				}
				printf("%s\n",buf);

			}
			close(newfd);
			exit(0);
		}
		close(newfd);

	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值