网络编程--day4

网络编程--day4_第1张图片

1.广播--

接收端

#include
#define ERR_(err) {perror(err);\
			fprintf(stderr,"%d",__LINE__);} 
#define DK 8888  
#define IP "192.168.123.255"
int main(int argc, const char *argv[])
{
	//udp广播接收端
	//创建套接字
	int sfd=0;
	sfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sfd<0)
	{
		ERR_("socket");
		return -1;
	}
	printf("socket success\n");

	//存储网络地址端口
	struct sockaddr_in sin;
	sin.sin_family  = AF_INET;
	sin.sin_port    = htons(DK);
	sin.sin_addr.s_addr = inet_addr(IP);
	//绑定套接字
	if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)))
	{
		ERR_("bind");
		return -1;
	}
	puts("bind success");
	//接受数据

	char buf[128]="";
	while(1)
	{
		bzero(buf,sizeof(buf));
		if(recv(sfd,buf,sizeof(buf),0)<0)
		{
			ERR_("recv");
			return -1;
		}
		puts("recv success");
		printf("消息为:%s\n",buf);

	
	}
	//关闭套接字
	if(close(sfd)<0)
	{
		ERR_("close");
		return -1;
	}
	puts("close success");

	return 0;
}

发送端

#include
#define ERR_(err) {perror(err);\
	fprintf(stderr,"%d",__LINE__);}
#define DK 8888
#define IP "192.168.123.255"
int main(int argc, const char *argv[])
{
	//广播UDP发送端
	//创建套接字
	int sfd=0;
	sfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sfd<0)
	{
		ERR_("socket");
		return -1;
	}
	printf("socket success\n");
	//设置网络属性为广播
	int optval=1;
	if(setsockopt(sfd,SOL_SOCKET,SO_BROADCAST,&optval,sizeof(optval))<0)
	{
		ERR_("set");
		return -1;
	}
	printf("setwb success\n");
	//存储地址
	struct sockaddr_in sin;
	sin.sin_family   = AF_INET;
	sin.sin_port   = htons(DK);
	sin.sin_addr.s_addr  = inet_addr(IP);

	char buf[128]="";
	//发送
	while(1)
	{
		bzero(buf,sizeof(buf));
		printf("请输入>>\n");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]=0;
		//发送数据
		if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr *)&sin,sizeof(sin))<0)
		{
			ERR_("sendto");
			return -1;
		}
		printf("sendto success\n");

	}

	if(close(sfd)<0)
	{
		ERR_("close");
		return -1;
	}
	puts("close success");

	return 0;
}

2.组播--

数据发送

#include
#define ERR_(err) {perror(err);\
	fprintf(stderr,"%d",__LINE__);}
#define DK 8888
#define IP "224.1.2.3"
int main(int argc, const char *argv[])
{
	//组播发送端
	//创建套接字
	int sfd=0;
	sfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sfd<0)
	{
		ERR_("err");
		return -1;
	}
	puts("socket success");

	//存储地址
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port   = htons(DK);
	sin.sin_addr.s_addr = inet_addr(IP);

	//发送数据
	char buf[128]="";
	while(1)
	{   //清空字符数组
		bzero(buf,sizeof(buf));
		//输入数据
		printf("请输入>>\n");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]=0;
		//发送数据
		if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr *)&sin,sizeof(sin))<0)
		{
			ERR_("send");
			return -1;
		}
		puts("send success");
	}

	//关闭套接字
	if(close(sfd))
	{
		ERR_("close");
		return -1;
	}

	return 0;
}

数据接收

#include
#define ERR_(err) {perror(err);\
	fprintf(stderr,"%d",__LINE__);}
#define DK 8888
#define IP "224.1.2.3"
#define LIP "192.168.122.50"
int main(int argc, const char *argv[])
{
	//组播接受端
	//创建套接字
	int sfd=0;
	sfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sfd<0)
	{
		ERR_("socket");
		return -1;
	}
	puts("socket success");
	//存储地址
	struct sockaddr_in sin;
	sin.sin_family  = AF_INET;
	sin.sin_port    = htons(DK);
	sin.sin_addr.s_addr = inet_addr(IP);
	//绑定
	if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin))<0)
	{
		ERR_("bind");
		return -1;
	}
	puts("bind success");
	//定义网络属性
	struct ip_mreqn mre;
	mre.imr_multiaddr.s_addr = inet_addr(IP);
	mre.imr_address.s_addr = inet_addr(LIP);
	mre.imr_ifindex  =  0;
	if(setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mre,sizeof(mre))<0)
	{
		ERR_("set");
		return -1;
	}
	puts("set success");

	//接受数据
	char buf[128]="";
	while(1)
	{
		bzero(buf,sizeof(buf));
		if(recv(sfd,buf,sizeof(buf),0)<0)
		{
			ERR_("recv");
			return -1;
		}
		puts("recv success");
		printf("%s\n",buf);
	}

	//关闭套接字
	if(close(sfd)<0)
	{
		ERR_("close");
		return -1;
	}
	return 0;
}

3.TCP并发多进程

#include
#define ERR_(err) {perror(err);\
	fprintf(stderr,"%d",__LINE__);}
#define DK 6666
#define IP "192.168.122.50"
int r_and_s(int nuwfd,struct sockaddr_in cin);

void huishou()
{
	while(waitpid(-1,NULL,WNOHANG)>0);
	printf("回收子程序成功\n");
}
int main(int argc, const char *argv[])
{
	//tcp多进程并发
	//回收子进程
	if(signal(SIGCHLD,huishou)==SIG_ERR)
	{
		ERR_("signal");
		return -1;
	}

	//创建套接字
	int sfd=0;
	sfd=socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_("socket");
		return -1;
	}
	puts("socket success");

	//设置网络属性
	int optval=1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval))<0)
	{
		ERR_("set");
		return -1;
	}
	puts("set success");
	//定义存储地址的结构体
	struct sockaddr_in sin;
	sin.sin_family  = AF_INET;
	sin.sin_port    = htons(DK);
	sin.sin_addr.s_addr = inet_addr(IP);

	//绑定套接字
	if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin))<0)
	{
		ERR_("bind");
		return -1;
	}
	puts("bind success");

	//监听
	if(listen(sfd,128)<0)
	{
		ERR_("listen");
		return -1;
	}
	puts("listen success");

	while(1)
	{
		struct sockaddr_in cin;
		socklen_t addrlen=sizeof(cin);

		//阻塞
		int nuwfd=accept(sfd,(struct sockaddr *)&cin,&addrlen);
		if(nuwfd<0)
		{
			ERR_("accept");
			return -1;
		}
		printf("accept success\n");

		//创建子进程
		pid_t pid=0;
		pid=fork();
		if(pid<0)
		{
			ERR_("pid create error");
			return -1;
		}

		//进入子进程
		else if(pid==0)
		{
			close(sfd);
			r_and_s(nuwfd,cin);
			exit(0);//关闭子进程
		}
	}
	//关闭套接字
	if(close(sfd)<0)
	{
		ERR_("close");
		return -1;
	}
	puts("close success");

	return 0;
}

int r_and_s(int nuwfd,struct sockaddr_in cin)
{
	char buf[128]="";
	while(1)
	{
		bzero(buf,sizeof(buf));
		//接收数据
		ssize_t res=recv(nuwfd,buf,sizeof(buf),0);
		if(res<0)			
		{
			ERR_("recvfrom");
			return -1;
		}
		if(res==0)
		{
			printf("客户端已下线\n");
			break;
		}
		printf("recvfrom success\n");
		printf("[%s:%d]发来了: %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);


		strcat(buf,"*^_^*");
		//发送数据
		if(send(nuwfd,buf,sizeof(buf),0)<0)
		{
			ERR_("sendto");
			return -1;
		}
		puts("sendto success");
	}
	return 0;
}

3.多线程并发

#include
#define ERR_(err) {perror(err);\
			fprintf(stderr,"%d",__LINE__);}
#define IP "192.168.122.50"
#define PORT 8888
void* r_and_s(void *arg);

typedef struct aa
{
	int nuwfd;
	struct sockaddr_in cin; 
}fifo;
int main(int argc, const char *argv[])
{
	//tcp多线程并发服务器
	//创建套接字
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_("socket");
		return -1;
	}
	puts("socket success");
	//设置网络属性允许端口快速重定义
	const int optval = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval))<0)
	{
		ERR_("set");
		return -1;
	}
	puts("set success");

	//绑定
	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_("bind");
		return -1;
	}
	puts("bind success");

	//监听
	if(listen(sfd,128)<0)
	{
		ERR_("listen");
		return -1;
	}
	puts("listen success");
	//while
	while(1)
	{
		struct sockaddr_in cin;
		socklen_t len = sizeof(len);
	//阻塞
	int nuwfd=accept(sfd,(struct sockaddr *)&cin,&len);
	if(nuwfd<0)
	{
		ERR_("accept");
		return -1;
	}
	puts("accept success");

	//创建线程
	fifo bag;
	bag.nuwfd=nuwfd;
	bag.cin=cin;

	pthread_t tid;
	if(pthread_create(&tid,NULL,r_and_s,&bag))
	{
		ERR_("create");
		return -1;
	}
	
	//回收线程
	pthread_detach(tid);
	}
	//关闭套接字
	close(sfd);
	return 0;
}
void* r_and_s(void *arg)
{
	int nuwfd=((fifo*)arg)->nuwfd;
	struct sockaddr_in cin=((fifo*)arg)->cin;

	char buf[128]="";
	while(1)
	{
		bzero(buf,sizeof(buf));
		ssize_t res=recv(nuwfd,buf,sizeof(buf),0);
		if(res<0)
		{
			ERR_("recv");
			break;
		}
		if(res==0)
		{
			puts("客户端下线");
			break;
		}
		printf("[%s:%d] %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);

		strcat(buf,"*^_^*");
		if(send(nuwfd,buf,sizeof(buf),0)<0)
		{
			ERR_("send");
			break;
		}

	}
	//关闭套接字
	close(nuwfd);
	//退出线程
	pthread_exit(NULL);

}

你可能感兴趣的:(网络,单片机,linux)