netday4广播/组播/多进程/多线程

广播代码

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

#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__",__LINE__);\
	perror("socket");\
}while(0)

#define IP "192.168.2.255" //广播IP
#define PORT 8888

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

	//填充接收方的地址信息结构体
	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;
	}
	puts("bind success");

	//存储数据包是从谁哪里来的
	struct sockaddr_in cin;
	socklen_t addrlen=sizeof(cin);

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

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

	return 0;
}

组播

发送方

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

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

#define GRP_IP "224.1.2.3" //组播IP 224.0.0.0 - 239.255.255.255
#define PORT 8888

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

	//绑定发送方的地址信息 非必须绑定
	//若不绑定则会自动绑定本机IP以及随机端口
	
	//填充接收方的地址信息结构体 给下面的sendto函数使用
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(GRP_IP); //组播IP 224.0.0.0 - 239.255.255.255

	char buf[128]="";
	while(1){
		bzero(buf,sizeof(buf));
		//发送数据 给接收方
		printf("请输入>>>");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]=0;

		if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr *)&sin,sizeof(sin)) < 0){
			ERR_MSG("sendto");
			return -1;
		}
		printf("sendto success \n");
	}

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

	return 0;
}

接收方

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

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

#define LOL_IP "192.168.2.216" //ifconfig的本机IP
#define GRP_IP "224.1.2.3" //组播IP 224.0.0.0 - 239.255.255.255
#define PORT 8888

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

	//加入 多播组
	struct ip_mreqn mq;
	mq.imr_multiaddr.s_addr=inet_addr(GRP_IP);//组播IP
	mq.imr_address.s_addr=inet_addr(LOL_IP);//本机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);

	//填充接收方的地址信息结构体
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(GRP_IP); //组播IP 224.0.0.0 - 239.255.255.255


	//绑定接收方的地址信息
	if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin))< 0){
		ERR_MSG("bind");
		return -1;
	}
	puts("bind success");

	//存储数据包是从谁哪里来的
	struct sockaddr_in cin;
	socklen_t addrlen=sizeof(cin);

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

	}

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

	return 0;
}

多进程

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

#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__",__LINE__);\
	perror("socket");\
}while(0)

#define IP "192.168.2.255" //广播IP
#define PORT 8888

int deal_cli_msg(int newfd, struct sockaddr_in cin);

void handler(int sig){
	while(waitpid(-1, NULL,WNOHANG) > 0);
}

int main(int argc, const char *argv[])
{
	//捕获17号信号
	if(signal(SIGCHLD,handler)==SIG_ERR){
		ERR_MSG("signal");
		return -1;
	}
	puts("捕获17号信号成功");

	//创建流式套接字
	int sfd=socket(AF_INET,SOCK_STREAM,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;
	}
	puts("允许端口快速被重用成功");
	
	//填充服务器的地址信息结构体 AF_INET: man 7 IP
	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的网络字节序

	//绑定服务器的IP和端口 必须绑定
	if(bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0){
		ERR_MSG("bind");
		return -1;
	}
	puts("bind success");

	//将套接字设置为被动监听整体,监听是否有客户端连接
	if(listen(sfd,128) < 0){
		ERR_MSG("listen");
		return -1;
	}
	puts("listen success");

	struct sockaddr_in cin; //存储客户端地址信息
	socklen_t addrlen = sizeof(cin);
	pid_t cpid = 0;
	int newfd=-1;

	while(1){
		//父进程只负责处理客户端的连接
		//阻塞函数,阻塞等待客户端连接成功;从已完成连接的队列头中获取一个客户端信息
		//这个新的文件描述符,才是与客户端通信的文件描述符
		newfd=accept(sfd, (struct sockaddr *)&cin, &addrlen);
		if(newfd < 0){
			ERR_MSG("accept");
			return -1;
		}
		printf("[%s : %d] newfd=%d 客户端连接成功\n",\
				inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);

		//能运行到当前位置,则代表有客户端连接成功了,则需要创建一个子进程用于收发数据
		cpid = fork();
		if(0==cpid){
			close(sfd);
			//子进程只负责与客户端交互
			deal_cli_msg(newfd, cin);

			close(newfd);
			exit(0);//子进程只负责与客户端交互, 所以当客户端退出后要退出子进程
		}
		close(newfd);
		
		waitpid(-1,NULL,WNOHANG);
	}

	//关闭文件描述符
	if(close(sfd) < 0){
		ERR_MSG("close");
		return -1;
	}

	return 0;
}

int deal_cli_msg(int newfd, struct sockaddr_in cin){
	char buf[128]="";
	ssize_t res = 0;
	while(1){
		bzero(buf, sizeof(buf));

		//接收数据
		res=recv(newfd, buf, sizeof(buf), 0);
		if(res < 0){
			ERR_MSG("recv");
			return -1;
		}else if(0==res){
			printf("[%s : %d] newfd=%d 客户端下线\n",\
					inet_n(cin.sin_addr), ntohs(cin.sin_port), newfd);
			break;
		}
		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_MSG("send");
			return -1;
		}
		puts("发送成功");
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值