23062网络编程day4

作业:

多进程并发服务器根据流程图重新编写。

#include 

#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__",__LINE__);\
	perror(msg);}while(0)
#define PORT 8888
#define IP "192.168.114.104"
void handler(int sig)
{
	while(waitpid(-1,NULL,WNOHANG)>0);
}

int main(int argc, const char *argv[])
{
	if(signal(17,handler)==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\n",sfd);
	int reuse=1;
	if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("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_MSG("bind");
		return -1;
	}
	printf("bind success\n");
	//设置套接字被动监听状态
	if(listen(sfd,128)<0)
	{
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success\n");

	int newfd=-1;
	struct sockaddr_in cin;
	socklen_t addrlen=sizeof(cin);
	pid_t pid;
	char buf[128]="";
	ssize_t res=0;
	while(1)
	{
		newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
		if(newfd<0)
		{
			ERR_MSG("newfd");
			return -1;
		}
		printf("客户端连接成功 newfd=%d\n",newfd);
		pid = fork();
		if(0==pid)
		{
			close(sfd);
			while(1)
			{
				res=recv(newfd,buf,sizeof(buf),0);
				if(res<0)
				{
					ERR_MSG("recv");
					return -1;
				}
				else if(res==0)
				{
					printf("客户端退出成功 newfd=%d\n",newfd);
					return 0;
				}
				printf("newfd=%d : %s\n",newfd,buf);
				if(send(newfd,buf,sizeof(buf),0)<0)
				{
					ERR_MSG("send");
					return -1;
				}
				printf("send success\n");
			}
			close(newfd);
			exit(0);
		}
		
		close(newfd);
	}
		if(close(sfd)<0)
		{
			ERR_MSG("close");
			return -1;
		}

	return 0;
}


多线程并发服务器重新写一遍。

#include 

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

#define PORT 8888

#define IP "192.168.114.104"

//需要传入到分支线程的参数
struct Climsg{
	int newfd;
	struct sockaddr_in cin;
};
void *task1(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=recv(newfd,buf,sizeof(buf),0);
	if(res<0)
	{
		ERR_MSG("recv");
		break;
	}
	else if(0==res)
	{
		printf("[%s:%d]客户端下线 newfd=%d\n",\
			inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
		break;
	}

		printf("[%s:%d] @_@:%s\n",\
			inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
	//发送
	strcat(buf,"*_*");
	if(send(newfd,buf,sizeof(buf),0)<0)
	{
		ERR_MSG("send");
		break;
	}
	printf("send success\n");
	}

	close(newfd);
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	//创建流式套接字 socket
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success! 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;
	}
	//将套接字设置为被动监听状态 listen
	if(listen(sfd,128)<0)
	{
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success!\n");
	//获取一个已经完成的客户端信息,生成一个新的文件描述符 accept
	int newfd=-1;
	struct sockaddr_in cin;
	socklen_t addrlen = sizeof(cin);
	//创建子线程
	pthread_t tid1;
	struct Climsg info;
	while(1)
	{
	newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
	if(newfd<0)
	{
		ERR_MSG("newfd");
		return -1;
	}
	printf("[%s:%d]客户端连接成功 newfd=%d\n",\
			inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
	info.newfd=newfd;
	info.cin=cin;
	if(pthread_create(&tid1,NULL,task1,(void*)&info))
	{
		fprintf(stderr,"pthread_create__%d__\n",__LINE__);
		return -1;
	}
	pthread_detach(tid1);
	}
	if(close(sfd)<0)
	{
		ERR_MSG("close");
		return -1;
	}
	return 0;
}



23062网络编程day4_第1张图片23062网络编程day4_第2张图片

你可能感兴趣的:(c语言,tcp/ip,网络协议,udp)