网络编程——并发服务器、域套接字

 多进程并发服务器

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

#define ERR_MSG(msg) do{\
	fprintf(stderr,"_%d_:",__LINE__);\
	perror(msg);\
}while(0)
#define PORT 6666
#define IP "192.168.31.57"
typedef void (*sighandler_t)(int);

int rcv_cli_mag(int newfd,struct sockaddr_in cin);
void handler(int sig)
{
	while(waitpid(-1,NULL,WNOHANG)>0);
}
int main(int argc, const char *argv[])
{
	//捕获17号信号,用信号的方式回收僵尸进程
	sighandler_t s = signal(SIGCHLD,handler);
	if(SIG_ERR==s)
	{
		ERR_MSG("signal");
		return -1;
	}

	//创建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}
	//允许端口快速重用
	int reuse = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	//绑定服务器的IP和端口
	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;
	}
	struct sockaddr_in cin;
	//获取新的文件描述符
	socklen_t addrlen=sizeof(cin);
	while(1)
	{
	int newfd = accept (sfd,(struct sockaddr*)&cin,&addrlen);
	if(newfd <0)
	{
		ERR_MSG("accept");
		return -1;
	}
	printf("newfd = %d\n",newfd);

	pid_t pid = fork();
	if(0==pid)
	{
		close(sfd);
		rcv_cli_mag(newfd,cin);
		close(newfd);
		exit(0);
	}
	else if(pid>0)
	{
		close(newfd);
	}
	else
	{
		ERR_MSG("fork");
		return -1;
	}
	}
	close(sfd);
	return 0;
}

int rcv_cli_mag(int newfd,struct sockaddr_in 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");
			return -1;
		}
		else if(0 == res)
		{
			fprintf(stderr,"%d客户端关闭\n",newfd);
			break;
		}
			printf("%s",buf);
		//发送
		strcat(buf,"*****");
		if(send(newfd,buf,sizeof(buf),0)<0)
		{
			ERR_MSG("send");
			return -1;
		}

	}
	return 0;
}

 多线程并发服务器

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

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

#define PORT 6666
#define IP "192.168.31.57"
struct msg
{
	int newfd;
	struct sockaddr_in cin;

};
void *callBack_front(void* arg);

int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}
	//允许端口快速重用
	int reuse = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	//绑定服务器的IP和端口
	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;
	}
	struct sockaddr_in cin;
	//获取新的文件描述符
	socklen_t addrlen=sizeof(cin);
	struct msg info;
	//	info.newfd = newfd;
		info.cin=cin;

	while(1)
	{
		int newfd = accept (sfd,(struct sockaddr*)&cin,&addrlen);
		if(newfd <0)
		{
			ERR_MSG("accept");
			return -1;
		}
		printf("newfd = %d\n",newfd);
		pthread_t tid1;

		info.newfd = newfd;
	//	info.cin=cin;

		if(pthread_create(&tid1,NULL,callBack_front,(void*)&info)!=0)
		{
			ERR_MSG("pthread_creat");
			return -1;

		}
		pthread_detach(tid1);
	}

	close(sfd);
	return 0;
}
void *callBack_front(void* arg)
{
	struct msg info = *(struct msg*)arg;
	int newfd = info.newfd;
	struct sockaddr_in cin =info.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)
		{
			fprintf(stderr,"%d客户端关闭\n",newfd);
			break;
		}
		printf("%s\n",buf);
		//发送
		strcat(buf,"*****");
		if(send(newfd,buf,sizeof(buf),0)<0)
		{
			ERR_MSG("send");
			break;
		}

	}
	close(newfd);
	pthread_exit(NULL);
}

 域式套接字客户端——UDP

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/socket.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/un.h>
#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_DGRAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}
	//判断要绑定的套接字文件路径是否存在
	if(access("./unix2",F_OK)==0)
	{
		if(unlink("./unix2")<0)
		{
			ERR_MSG("unlink");
			return -1;
		}
	}
	struct sockaddr_un cun;
	cun.sun_family = AF_UNIX;
	strcpy(cun.sun_path,"./unix2");

	if(bind(sfd,(struct sockaddr*)&cun,sizeof(cun))<0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");
	struct sockaddr_un sun;
	sun.sun_family =AF_UNIX;
	strcpy(sun.sun_path,"unix1");

	char buf[128]="";
	ssize_t res = 0;
	struct sockaddr_un rcv_addr;
	socklen_t addrlen = sizeof(rcv_addr);

	while(1)
	{
		bzero(buf,sizeof(buf));
		printf("请输入");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;
		if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sun,sizeof(sun))<0)
		{
			ERR_MSG("sendto");
			return -1;
		}
		bzero(buf,sizeof(buf));
		res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&rcv_addr,&addrlen);
		if(res <0)
		{
			ERR_MSG("recvfrom");
			return -1;
		}

	}

close(sfd);
		return 0;
}

 域式套接字服务端——UDP

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/socket.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/un.h>
#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_DGRAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}
	//判断要绑定的套接字文件路径是否存在
	if(access("./unix1",F_OK)==0)
	{
		if(unlink("./unix1")<0)
		{
			ERR_MSG("unlink");
			return -1;
		}
	}
	struct sockaddr_un cun;
	cun.sun_family = AF_UNIX;
	strcpy(cun.sun_path,"./unix1");

	if(bind(sfd,(struct sockaddr*)&cun,sizeof(cun))<0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");

	char buf[128]="";
	ssize_t res = 0;
	struct sockaddr_un rcv_addr;
	socklen_t addrlen = sizeof(rcv_addr);

	while(1)
	{
		bzero(buf,sizeof(buf));
		res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&rcv_addr,&addrlen);
		if(res <0)
		{
			ERR_MSG("recvfrom");
			return -1;
		}
		bzero(buf,sizeof(buf));
		printf("请输入");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;
		if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&rcv_addr,sizeof(rcv_addr))<0)
		{
			ERR_MSG("sendto");
			return -1;
		}
	
	}

close(sfd);
		return 0;
}

 域式套接字服务器——TCP

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

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

#define PORT 8888
#define IP "192.168.31.57"
int main(int argc, const char *argv[])
{ 
	//创建流式套接字
	int sfd = socket(AF_UNIX,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}

	if(access("./unix",F_OK)==0)
	{
		if(nulink("./unix")<0)
		{
			ERR_MSG("unlink");
			return -1;
		}
	}
	//绑定服务器的IP地址和端口号
	struct sockaddr_un sun;
	sun.sun_family = AF_INET;
	strcpy(sun.sun_path,"./unix");


	if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))<0)
	{
		ERR_MSG("bind");
		return -1;
	}

	//将套接字设置为被动监听设备
	if(listen(sfd,10)<0)
	{
		ERR_MSG("listen");
		return -1;
	}
	//获取连接成功后的套接字
	struct sockaddr_un cun;
	socklen_t addrlen = sizeof(cun);

	int newfd=accept(sfd,(struct sockaddr*)&cun,&addrlen);

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

	//接收客户端的信息
	char buf[128]="";
	while(1)
	{
		bzero(buf,sizeof(buf));

		ssize_t res = recv(newfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			ERR_MSG("recv");
			return -1;
		}
		else if(res==0)
		{
			printf("客户端关闭\n");
			return -1;
		}
		printf("发送成功\n");
		//发送信息给客户端
		strcat(buf,"**");
		if(send(newfd,buf,sizeof(buf),0)<0)
		{
			ERR_MSG("send");
			return -1;
		}
		printf("发送成功\n");
	}
	close(newfd);
	close(sfd);



	return 0;
}

  域式套接字客户端——TCP

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

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

#define PORT 8888
#define IP "192.168.31.57"
int main(int argc, const char *argv[])
{ 
	//创建流式套接字
	int sfd = socket(AF_UNIX,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}
	//绑的IP地址和端口号
	struct sockaddr_un sun;
	sun.sun_family = AF_INET;
	strcpy(sun.sun_path,"./unix");


	if(connect(sfd,(struct sockaddr*)&sun,sizeof(sun))<0)
	{
		ERR_MSG("connect");
		return -1;
	}

	
	//接收客户端的信息
	char buf[128]="";
	while(1)
	{
		bzero(buf,sizeof(buf));
		printf("请输入");
		fget(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;

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

		ssize_t res = recv(newfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			ERR_MSG("recv");
			return -1;
		}
		else if(res==0)
		{
			printf("服务器关闭\n");
			return -1;
		}
		printf("发送成功\n");
	}
	close(newfd);
	close(sfd);



	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值