完成多进程多线程并发服务器搭建

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define IP "192.168.8.112"
#define PORT 8888
#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__:",__LINE__);\
	perror(msg);\
}while(0)
void pth_cli(void* arg){
	int newfd=*((int *)arg)
	ssize_t res=0;
	char buf[128]="";
	size_t len=sizeof(buf);
	while(1){
		bzero(buf,sizeof(buf));
		res=recv(newfd,buf,len,0);
		if(res<0){
			ERR_MSG("recv");
			return -1;
		}else if(0==res){
			printf("newfd=%d 客户端关闭\n",newfd);
			break;
		}
		printf("res:%ld nemfd:%d:%s\n",res,newfd,buf);
		/*printf("请输入\n");
		bzero(cuf,sizeof(cuf));
		scanf("%s",cuf);
		if(send(newfd,cuf,sizeof(cuf),0)<0){
			ERR_MSG("send");
			return -1;
		}*/
	}
	close(newfd);
	pthread_detach(pthread_self());
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	int sfd=socket(AF_INET,SOCK_STREAM,0);

	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;
	}
	
	if(listen(sfd,128)<0){
		ERR_MSG("listen");
		return -1;
	}
	printf("服务器监听成功\n");
	struct sockaddr_in cin;
	socklen_t addrlen=sizeof(cin);
	pthread_t tid;
	while(1) {
		newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
		if(newfd<0){
			ERR_MSG("accept");
			return -1;
		}

		printf("newfd=%d 连接成功\n",newfd);
		if(pthread_create(&tid,NULL,pth_cli,(void*)&newfd)<0){
			ERR_MSG("pthread_create");
			return -1;
		}

	}
		close(sfd);
	return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define IP "192.168.8.112"
#define PORT 8888
#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__:",__LINE__);\
	perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
	//创建流式域套接字
	int sfd=socket(AF_UNIX,SOCK_STREAM,0);
	//判断文件是否存在
	if(access("./unix",F_OK)==0){
		if(unlink("./unix")<0){
			ERR_MSG("unlink");
			return -1;
		}
	}
	//填充地址信息结构体
	struct sockaddr_un sin;
	sin.sun_family=AF_UNIX;
	strcpy(sin.sun_path,"./unix");
	//绑定
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
		ERR_MSG("bind");
		return -1;
	}
	
	if(listen(sfd,128)<0){
		ERR_MSG("listen");
		return -1;
	}
	printf("服务器监听成功\n");
	struct sockaddr_un cin;
	socklen_t addrlen=sizeof(cin);
	int newfd;
	pid_t pid;
	while(1){
		newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
		if(newfd<0){
			ERR_MSG("accept");
			return -1;
		}
		printf("newfd=%d 连接成功\n",newfd);
		//创建一个子进程
		pid=fork();
		if(pid>0){
			//父进程
			close(newfd);
		}else if(0==pid){
			//子进程
			ser_child(newfd);
		}else{
			ERR_MSG("fork");
			return -1;
		}
	}
	close(sfd);
	return 0;
}

int ser_child(int newfd){
	ssize_t res=0;
	char buf[128]="";
	size_t len=sizeof(buf);
	while(1){
	bzero(buf,sizeof(buf));
	res=recv(newfd,buf,len,0);
	if(res<0){
		ERR_MSG("recv");
		return -1;
	}else if(0==res){
		printf("newfd=%d 客户端关闭\n",newfd);
		break;
	}
	printf("newfd:%d:%s\n",newfd,buf);
	}
	close(newfd);
	return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define IP "192.168.8.112"
#define PORT 8888
#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__:",__LINE__);\
	perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
	//创建流式域套接字
	int sfd=socket(AF_UNIX,SOCK_STREAM,0);
	//判断文件是否存在
	if(access("./unix",F_OK)==0){
		if(unlink("./unix")<0){
			ERR_MSG("unlink");
			return -1;
		}
	}
	//填充地址信息结构体
	struct sockaddr_un sin;
	sin.sun_family=AF_UNIX;
	strcpy(sin.sun_path,"./unix");
	//绑定
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
		ERR_MSG("bind");
		return -1;
	}
	
	if(listen(sfd,128)<0){
		ERR_MSG("listen");
		return -1;
	}
	printf("服务器监听成功\n");
	struct sockaddr_un cin;
	socklen_t addrlen=sizeof(cin);
	int newfd;
	pid_t pid;
	while(1){
		newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
		if(newfd<0){
			ERR_MSG("accept");
			return -1;
		}
		printf("newfd=%d 连接成功\n",newfd);
		//创建一个子进程
		pid=fork();
		if(pid>0){
			//父进程
			close(newfd);
		}else if(0==pid){
			//子进程
			ser_child(newfd);
		}else{
			ERR_MSG("fork");
			return -1;
		}
	}
	close(sfd);
	return 0;
}

int ser_child(int newfd){
	ssize_t res=0;
	char buf[128]="";
	size_t len=sizeof(buf);
	while(1){
	bzero(buf,sizeof(buf));
	res=recv(newfd,buf,len,0);
	if(res<0){
		ERR_MSG("recv");
		return -1;
	}else if(0==res){
		printf("newfd=%d 客户端关闭\n",newfd);
		break;
	}
	printf("newfd:%d:%s\n",newfd,buf);
	}
	close(newfd);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值