当前位置: 代码网 > 手机>品牌>苹果iPhone > UDP的广播模型,组播模型。TCP的多进程并发服务器

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

2024年08月02日 苹果iPhone 我要评论
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;
}

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com