网络编程9.4

1、多进程多线程并发服务器,再实现一遍(重点模型)。

多进程并发服务器:

#include 
#define SERPORT 9999
#define SERIP "192.168.0.162"
#define BACKLOG 10
void hande(int sss)
{
	if(sss==SIGCHLD)
	{
		while(waitpid(-1,NULL,WNOHANG)!=-1);
	}
}
int main(int argc, const char *argv[])
{
	if(signal(SIGCHLD,hande)==SIG_ERR)
	{
		perror("signal");
		return -1;
	}
	int oldfd = socket(AF_INET,SOCK_STREAM,0);
	if(oldfd==-1)
	{
		perror("socket");
		return -1;
	}
	
	int kkk = 666;
	if(setsockopt(oldfd,SOL_SOCKET,SO_REUSEADDR,&kkk,sizeof(kkk))==-1)
	{
		perror("setsockopt");
		return -1;
	}

	struct sockaddr_in sin = {
	.sin_family = AF_INET,
	.sin_port = htons(SERPORT),
	.sin_addr.s_addr = inet_addr(SERIP)};
	if(bind(oldfd,(struct sockaddr *)&sin,sizeof(sin))==-1)
	{
		perror("bind");
		return -1;
	}
	if(listen(oldfd,BACKLOG)==-1)
	{
		perror("listen");
		return -1;
	}
	
	struct sockaddr_in cin;
	int cinlen = sizeof(cin);

	while(1)
	{
	int newfd = accept(oldfd,(struct sockaddr *)&cin,&cinlen);
	if(newfd==-1)
	{
		perror("accept");
		return -1;
	}
	printf("%s:%d:连接成功\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
	pid_t pid = fork();
	if(pid>0)
	{
		close(newfd);
	}
	else if(pid==0)
	{
		close(oldfd);
		char buf[1024];
		while(1)
		{
			int len = recv(newfd,buf,sizeof(buf),0);
			if(len==0)
			{
				printf("客户端退出\n");
				break;
			}

			printf("服务器收到消息:%s\n",buf);
			strcat(buf,"^_^");
			putchar(10);
			send(newfd,buf,sizeof(buf),0);
			printf("发送成功\n");
		}
		close(newfd);
		exit(0);//退出子进程
	}
	else
	{
		perror("fork");
		return -1;
	}
	}
	close(oldfd);
	return 0;
}

多线程并发服务器:

#include 
#define SERPORT 8888
#define SERIP "192.168.0.162"
#define BACKLOG 20

typedef struct
{
	int newfd;
	struct sockaddr_in cin;
}LA;

void *fun1(void *ddd)
{
	int newfd = ((LA *)ddd)->newfd;
	struct sockaddr_in cin = ((LA *)ddd)->cin;
	char buf[1024];

	while(1)
	{
		bzero(buf,sizeof(buf));
		int len = recv(newfd,buf,sizeof(buf),0);
		printf("收到%s:%d的信息:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
		if(len==0)
		{
			printf("客户端退出\n");
			break;
		}
		strcat(buf,"JTT1314");
		send(newfd,buf,sizeof(buf),0);
		printf("发送成功\n");
	}
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	int oldfd = socket(AF_INET,SOCK_STREAM,0);
	if(oldfd==-1)
	{
		perror("socket");
		return -1;
	}
	int BMWm = 4;
	if(setsockopt(oldfd,SOL_SOCKET,SO_REUSEADDR,&BMWm,sizeof(BMWm))==-1)
	{
		perror("setsockopt");
		return -1;
	}
	printf("端口号快速复用成功\n");

	struct sockaddr_in sin = {
	.sin_family = AF_INET,
	.sin_port = htons(SERPORT),
	.sin_addr.s_addr = inet_addr(SERIP)};
	
	if(bind(oldfd,(struct sockaddr *)&sin,sizeof(sin))==-1)
	{
		perror("bind");
		return -1;
	}
	if(listen(oldfd,BACKLOG)==-1)
	{
		perror("listen");
		return -1;
	}
	printf("监听成功\n");
	struct sockaddr_in cin;
	int cinlen = sizeof(cin);
	LA MSG;

	while(1)
	{
		int newfd = accept(oldfd,(struct sockaddr *)&cin,&cinlen);
		if(newfd==-1)
		{
			perror("accept");
			return -1;
		}
		printf("%s:%d发来请求\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
		MSG.newfd = newfd;
		MSG.cin = cin;
		pthread_t tid;
		if(pthread_create(&tid,NULL,fun1,&MSG)!=0)
		{
			perror("pthread_create");
			return -1;
		}
		pthread_detach(tid);
	}
	close(oldfd);
	return 0;
}

2、广播发送接收端,自己实现一遍(重点模型)

发送端:

#include 
#define SENDPORT 6666
#define SENDIP "192.168.0.255"
int main(int argc, const char *argv[])
{
	int oldfd = socket(AF_INET,SOCK_DGRAM,0);
	if(oldfd==-1)
	{
		perror("socket");
		return -1;
	}
	
	int kkk = 2;
	if(setsockopt(oldfd,SOL_SOCKET,SO_BROADCAST,&kkk,sizeof(kkk))==-1)
	{
		perror("setsockopt");
		return -1;
	}
	printf("设置允许广播成功\n");

	struct sockaddr_in send = {
	.sin_family = AF_INET,
	.sin_port = htons(SENDPORT),
	.sin_addr.s_addr = inet_addr(SENDIP)};
	
	struct sockaddr_in ree;
	int reelen = sizeof(ree);
	char buf[1024];
	while(1)
	{
		bzero(buf,sizeof(buf));
		sleep(2);
		strcat(buf,"5201314");
		sendto(oldfd,buf,sizeof(buf),0,(struct sockaddr *)&send,sizeof(send));
		printf("发送成功\n");
	}
	close(oldfd);
	return 0;
}

接收端:

#include 
#define SENDPORT 6666
#define SENDIP "192.168.0.255"
int main(int argc, const char *argv[])
{
	int oldfd = socket(AF_INET,SOCK_DGRAM,0);
	if(oldfd==-1)
	{
		perror("socket");
		return -1;
	}

	struct sockaddr_in ree = {
	.sin_family = AF_INET,
	.sin_port = htons(SENDPORT),
	.sin_addr.s_addr = inet_addr(SENDIP)};

	if(bind(oldfd,(struct sockaddr *)&ree,sizeof(ree))==-1)
	{
		perror("bind");
		return -1;
	}
	char buf[1024];

	while(1)
	{
		bzero(buf,sizeof(buf));
		recvfrom(oldfd,buf,sizeof(buf),0,NULL,NULL);
		printf("%s\n",buf);
		strcat(buf,"^-^");
		sendto(oldfd,buf,sizeof(buf),0,(struct sockaddr *)&ree,sizeof(ree));
		printf("发送成功\n");
	}
	return 0;
}

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