网络编程-day3

多进程并发服务器

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

#define PORT 1110
#define ERR_MSG(msg) do{\
	fprintf(stderr, "line:%d\n", __LINE__);\
	perror(msg);\
}while(0)

#define IP "192.168.10.133"     //ifconfig出来的本机IP

int deal_cil_msg(int newfd,struct sockaddr_in cin)
{
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		//对于字符串的操作,操作之前或者操作之后要清空,
		//防止之前的数据对这次操作有干扰
		bzero(buf, sizeof(buf));
		//接收数据
		//res=recvfrom(newfd,buf,sizeof(buf),0,NULL,NULL);
		res = recv(newfd, buf, sizeof(buf), 0);
		//res=read(newfd,buf,sizeof(buf));
		if(res < 0)
		{
			ERR_MSG("recv");
			return -1;
		}
		else if(0 == res)
		{
			printf("[%s : %d] 客户端下线 newfd = %d __%d__\n", \
					inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);			
			break;
		}
		printf("[%s : %d] 服务端接收: %s__%d__\n", \
				inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf, __LINE__);


		//发送数据
		strcat(buf,"^-^");
		if(send(newfd, buf, sizeof(buf), 0) < 0)
		{
			ERR_MSG("send");
			return -1;
		}
		printf("发送成功\n");

	}	
	if(close(newfd)<0)
	{
		ERR_MSG("close");
		return -1;
	}

}

//回收僵尸
void zombie(int sig)
{
	while(waitpid(-1,NULL,WNOHANG)>0);
}

int main(int argc, const char *argv[])
{
	//捕捉17号信号
	if(signal(17,zombie)==SIG_ERR)
	{
		ERR_MSG("signal");
		return -1;
	}


	//创建流式套接字
	int sfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success sfd=%d __%d__\n",sfd,__LINE__);

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


	//填充服务器的地址信息结构体
	//该真实结构体根据地址族制定: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 ifconfig

	//绑定
	if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success __%d__\n", __LINE__);

	if(listen(sfd, 128) < 0)
	{
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success __%d__\n", __LINE__);

	struct sockaddr_in cin;    
	socklen_t len = sizeof(cin);
	int newfd  = -1;

	while(1)
	{
		pid_t cpid=fork();
		newfd = accept(sfd, (struct sockaddr*)&cin, &len);
		if(newfd < 0)
		{
			ERR_MSG("accept");
			return -1;
		}
		printf("[%s : %d] 客户端连接成功 newfd = %d __%d__\n", \
				inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);    

		if(cpid==0)
		{
			close(sfd);
			deal_cil_msg(newfd,cin);
			exit(0);              //退出子进程
		}
		close(newfd);

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

多线程并发服务器

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>
#define PORT 1110
#define ERR_MSG(msg) do{\
	fprintf(stderr, "line:%d\n", __LINE__);\
	perror(msg);\
}while(0)

#define IP "192.168.10.133"     //ifconfig出来的本机IP
struct climsg
{
	int newfd;
	struct sockaddr_in cin;
};
void* deal_cil_msg(void *arg) 
{
	int newfd=((struct climsg*)arg)->newfd;
	struct sockaddr_in cin=((struct climsg*)arg)->cin;
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		bzero(buf, sizeof(buf));
		res=recvfrom(newfd,buf,sizeof(buf),0,NULL,NULL);
		if(res < 0)
		{
			ERR_MSG("recv");
			break;
		}
		else if(0 == res)
		{
			printf("[%s : %d] 客户端下线 newfd = %d __%d__\n", \
					inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);			
			break;
		}
		printf("[%s : %d] 服务端接收: %s__%d__\n", \
				inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf, __LINE__);

		//发送数据
		strcat(buf,"^-^");
		if(send(newfd, buf, sizeof(buf), 0) < 0)
		{
			ERR_MSG("send");
			break;
		}
		printf("发送成功\n");

	}	
	if(close(newfd)<0)
	{
		ERR_MSG("close");
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{

	//创建流式套接字
	int sfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success sfd=%d __%d__\n",sfd,__LINE__);

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


	//填充服务器的地址信息结构体
	//该真实结构体根据地址族制定: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 ifconfig

	//绑定
	if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success __%d__\n", __LINE__);

	if(listen(sfd, 128) < 0)
	{
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success __%d__\n", __LINE__);

	struct sockaddr_in cin;    
	socklen_t len = sizeof(cin);
	int newfd  = -1;
	pthread_t tid;
	struct climsg info;
	while(1)
	{
		newfd = accept(sfd, (struct sockaddr*)&cin, &len);
		if(newfd < 0)
		{
			ERR_MSG("accept");
			return -1;
		}
		printf("[%s : %d] 客户端连接成功 newfd = %d __%d__\n", \
				inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);    
		
		info.newfd=newfd;
		info.cin=cin;

		if(pthread_create(&tid,NULL,deal_cil_msg,(void*)&info)<0)
		{
			ERR_MSG("pthread_create");
			return -1;
		}
		pthread_detach(tid);	
	}
	//关闭文件描述符
	if(close(sfd)<0);
	{
		ERR_MSG("close");
		return -1;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值