域套接字.

使用套接字实现本机进程间通信

为本机进程间通信而生——域套接字

实现方法

  1. 第一种:使用网络套接字
    • 比如使用TCP/IP协议族来实现,不管是使用TCP还是使用UDP都可以,只要目标ip指定为本机IP或者127.0.0.1网环地址,即可实现本机进程间通信。
    • 事实上使用实现网络通信的“网络套接字”来实现本机进程间通信,有点大才小用,如果想要使用套接字来实现本机进程间通信的话,我们应该是使用“域套接字”,而不是“网络套接字”。
    • 实现本机进程间通信,“域套接字”的效率高于“网络套接字”。
  2. 第二种:使用“域套接字”
    • 创建“域套接字文件时”,如果指定的是“域套通信协议族”的话,这个套接字文件就被称为“域套接字文件”简称域套接字。
    • 域套接字不能实现跨进程间通信,只能用来实现“本机进程间通信”,而且还是专业的“本机间通信”。
    • 总结以下,到目前为止,进程间通信的方式有那些?
      • 信号
      • 管道(无名、有名)
      • system V IPC(消息队列、共享内存、无名信号量)
      • 域套接字

域套接字的实现

有两种,分别是

  • 字节流的“域套接字”
  • 数据报的“域套接字”

字节流“域套接字”

  1. 编程模型
    1. 编程模型与TCP的编程模型完全一样。
    2. 虽然编程模型与TCP一样,但是还是有所不同的,其中最大的不同就是,绑定时设置的结构体是struct sockaddr_un,而不是struct sockaddr_in
    3. struct sockaddr_un是域套接字使用的,struct sockaddr_in是TCP套接字使用的
    4. #define UNIX_PATH_MAX 108
      
      struct sockaddr_un
      {
        	sa_family_t sun_family;              /*PF_UNIX或AF_UNIX */
        	char        sun_path[UNIX_PATH_MAX]; /* 路径名 */
      };
      

设置这个结构体时,

sun_family = AF_UNIX或者AF_LOCAL

sun_path = 一个文件路径名//注意不是IP和端口,域套接字实现本机进程间通信时,用不到ip和端口。

bind绑定结构体的内容时,会创建“所设置路径名的”文件。

疑问:这个文件起什么作用?

简单理解就是,起到与ip和端口类似的作用

TCP代码

程序演示:

server.c

int cfd;

void signal_fun(int signo)
{
	if (SIGINT == signo)
	{
		/* Delete file! */
		remove("./UNIX_SOCK");
		exit(0);
	}
}

void *pth_func(void *arg)
{
	int ret = -1;
	char buf[100] = {0};
	while(1)
	{
		bzero(buf, sizeof(buf));
		ret = read(cfd, buf, sizeof(buf));
		if (ret == -1) print_err("read failed", __LINE__, errno);
		else if (ret > 0)
		{
			printf("%s\n", buf);
		}
	}
}

int main(int argc, char *argv[])
{
	int sockfd = -1; 
	int ret = -1;
	char buf[100] = {0};
	pthread_t tid;
	struct sockaddr_un addr = {0};

	signal(SIGINT, signal_fun);

	//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
	if (sockfd == -1) print_err("socket failed", __LINE__, errno); 

	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, "UNIX_SOCK");

	ret = bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
	if (ret == -1) print_err("bind failed", __LINE__, errno);
 
	ret = listen(sockfd, 3);
	if (ret == -1) print_err("listen failed", __LINE__, errno);

	cfd = accept(sockfd, NULL, 0);
	if (cfd == -1) print_err("accept failed", __LINE__, errno);

	ret = pthread_create(&tid, NULL, pth_func, NULL);
	if (ret != 0) print_err("pthread_create failed", __LINE__, ret);

	while(1)
	{
		bzero(buf, sizeof(buf));
		scanf("%s", buf);
		ret = write(cfd, buf, sizeof(buf));
		if (ret == -1) print_err("write failed", __LINE__, errno);
	}

	return 0;
}

client.c

int sockfd;

void *pth_func(void *arg)
{
	int ret = -1;
	char buf[100] = {0};
	while(1)
	{
		bzero(buf, sizeof(buf));
		ret = read(sockfd, buf, sizeof(buf));
		if (ret == -1) print_err("read failed", __LINE__, errno);
		else if (ret > 0)
		{
			printf("%s\n", buf);
		}
	}
}

int main(int argc, char *argv[])
{ 
	int ret = -1;
	char buf[100] = {0};
	pthread_t tid;
	struct sockaddr_un addr = {0};

	//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
	if (sockfd == -1) print_err("socket failed", __LINE__, errno); 

	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, "./UNIX_SOCK");
	ret = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
	if (ret == -1) print_err("connect failed", __LINE__, errno);

	ret = pthread_create(&tid, NULL, pth_func, NULL);
	if (ret != 0) print_err("pthread_create failed", __LINE__, ret);

	while(1)
	{
		bzero(buf, sizeof(buf));
		scanf("%s", buf);

		ret = write(sockfd, buf, sizeof(buf));
		if (ret == -1) print_err("write failed", __LINE__, errno);
	}

	return 0;
}

UDP

代码演示:

A

int sockfd;
struct sockaddr_un peer_addr;

void signal_fun(int signo)
{
	if (SIGINT == signo)
	{
		/* Delete file! */
		remove("./unix_sock_A");
		exit(0);
	}
}

void *pth_func(void *arg)
{
		int ret = -1;
		char buf[100] = {0};
		int peer_addr_size = sizeof(peer_addr);

		while(1)
		{
			bzero(buf, sizeof(buf));
			ret = recvfrom(sockfd, buf, sizeof(buf), 0, \
							(struct sockaddr *)&peer_addr, &peer_addr_size);
			if (ret == -1) print_err("recvfrom failed", __LINE__, errno);
			else if (ret > 0)
			{
				printf("%s\n", buf);
			}
		}
}

int main(int argc, char *argv[])
{
	int ret;
	char buf[100] = {0};
	pthread_t tid;

	if (argc != 2)
	{
		printf("./a.out filename\n");
	}

	signal(SIGINT, signal_fun);

	sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
	if (sockfd == -1) print_err("socket failed", __LINE__, errno);

	struct sockaddr_un addr = {0};
	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, "./unix_sock_A");
	ret = bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
	if (ret == -1) print_err("bind failed", __LINE__, errno);

	ret = pthread_create(&tid, NULL, pth_func, NULL);
	if (ret != 0) print_err("pthread_create failed", __LINE__, ret);

	while(1)
	{
		peer_addr.sun_family = AF_UNIX;
		strcpy(peer_addr.sun_path, argv[1]);
	
		bzero(buf, sizeof(buf));
		scanf("%s", buf);
	
		ret = sendto(sockfd, buf, sizeof(buf), 0, \
						(struct sockaddr *)&peer_addr, sizeof(peer_addr));
		if (ret == -1) print_err("sendto failed", __LINE__, errno);
	
	}

	return 0;
}

B

int sockfd;
struct sockaddr_un peer_addr;

void signal_fun(int signo)
{
	if (SIGINT == signo)
	{
		/* Delete file! */
		remove("./unix_sock_B");
		exit(0);
	}
}

void *pth_func(void *arg)
{
		int ret = -1;
		char buf[100] = {0};
		int peer_addr_size = sizeof(peer_addr);

		while(1)
		{
			bzero(buf, sizeof(buf));
			ret = recvfrom(sockfd, buf, sizeof(buf), 0, \
							(struct sockaddr *)&peer_addr, &peer_addr_size);
			if (ret == -1) print_err("recvfrom failed", __LINE__, errno);
			else if (ret > 0)
			{
				printf("%s\n", buf);
			}
		}
}

int main(int argc, char *argv[])
{
	int ret;
	char buf[100] = {0};
	pthread_t tid;

	if (argc != 2)
	{
		printf("./a.out filename\n");
	}

	signal(SIGINT, signal_fun);

	sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
	if (sockfd == -1) print_err("socket failed", __LINE__, errno);

	struct sockaddr_un addr = {0};
	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, "./unix_sock_B");
	ret = bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
	if (ret == -1) print_err("bind failed", __LINE__, errno);

	ret = pthread_create(&tid, NULL, pth_func, NULL);
	if (ret != 0) print_err("pthread_create failed", __LINE__, ret);

	while(1)
	{
		peer_addr.sun_family = AF_UNIX;
		strcpy(peer_addr.sun_path, argv[1]);
	
		bzero(buf, sizeof(buf));
		scanf("%s", buf);
	
		ret = sendto(sockfd, buf, sizeof(buf), 0, \
						(struct sockaddr *)&peer_addr, sizeof(peer_addr));
		if (ret == -1) print_err("sendto failed", __LINE__, errno);
	
	}

	return 0;
}
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值