Linux网络编程-6.本地套接字

6.1 广播

  • 服务器
    • 创建套接字 - socket
    • fd绑定服务器IP和端口
    • 初始化客户端IP和端口信息:
      • struct sockaddr_in cli;
      • cli.sin_family = AF_INET;
      • cli.port = htons(port);
      • inet_pton(AF_INET, "xxx.xxx.xxx.255", &cli.sin_addr.s_addr);
    • 发送数据:
      • sendto(fd, buf, len, 0);
    • 设置广播权限
      • setsockopt();
  • 客户端
    • 创建套接字
    • 显式绑定IP和端口
      • bind();
    • 接收数据
      • recvfrom();
  • 适用范围
    • 只适用于局域网
  • 无法拒绝接收

示例:

// server.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <arpa/inet.h>

int main(int argc, const char *argv[])
{
	// 创建套接字
	int fd = socket(AF_INET, SOCK_DGRAM, 0);
	if(fd == -1)
	{
		perror("socket error");
		exit(-1);
	}
	// 绑定IP和端口
	struct sockaddr_in serv;
	memset(&serv, 0x00, sizeof(serv));
	serv.sin_family = AF_INET;
	serv.sin_port = htons(8888);
	serv.sin_addr.s_addr = htonl(INADDR_ANY);
	int ret = bind(fd, (struct sockaddr*)&serv, sizeof(serv));
	if(ret == -1)
	{
		perror("bind error:");
		exit(-2);
	}

	// 初始化客户端地址信息
	struct sockaddr_in client;
	memset(&client, 0x00, sizeof(client));
	client.sin_family = AF_INET;
	client.sin_port = htons(9999);
	// 使用广播地址给客户端发送数据
	inet_pton(AF_INET, "172.16.221.255", &client.sin_addr.s_addr);
	// 给服务器开放广播权限
	int flag = 1;
	setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &flag, sizeof(flag));
	// 通信
	while(1)
	{
		//一直给客户端发送数据
		static int num = 0;
		char buf[1024] = { 0 };
		sprintf(buf, "hello, num = %d\n", num++);
		if(sendto(fd, buf, strlen(buf) + 1, 0, (struct sockaddr*)&client, sizeof(client)) == -1)
		{
			perror("sendto error:");
			break;
		}
		printf("send buf:%s\n", buf);
		sleep(1);
	}
	close(fd);
	return 0;
}
// client.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <arpa/inet.h>

int main(int argc, const char *argv[])
{
	// 创建套接字
	int fd = socket(AF_INET, SOCK_DGRAM, 0);
	if(fd == -1)
	{
		perror("socket error");
		exit(-1);
	}
	// 绑定IP和端口
	struct sockaddr_in client;
	memset(&client, 0x00, sizeof(client));
	client.sin_family = AF_INET;
	client.sin_port = htons(9999);
	inet_pton(AF_INET, "0.0.0.0", &client.sin_addr.s_addr);
	int ret = bind(fd, (struct sockaddr*)&client, sizeof(client));
	if(ret == -1)
	{
		perror("bind error:");
		exit(-2);
	}
	while(1)
	{
		char buf[1024] = { 0 };
		int len = recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);
		if(len == -1)
		{
			perror("recvfrom error:");
			exit(-3);
		}
		printf("client - recv buf:%s\n", buf);
	}
	close(fd);
	return 0;
}

6.2 组播

使用范围

  • 局域网
  • Internet
  • 可以拒绝接收

服务器端:

  • 需要使用组播地址
  • 发送到客户端对应的端口上
  • 添加组播权限

客户端:

  • 绑定固定端口
  • 加入到组播地址

组播地址:

  • 224.0.0.0~224.0.0.255
    • 预留的组播地址(永久组地址),地址224.0.0.0保留不做分配,其他地址供路由协议使用
  • 224.0.1.0~224.0.1.255
    • 公用组播地址,可以用于Internet;欲使用需申请
  • 224.0.2.0~238.255.255.255
    • 用户可用的组播地址(临时组地址),全网范围内有效
  • 239.0.0.0~239.255.255.255
    • 本地管理组播地址,仅在特定的本地范围内有效

相关结构体:

struct ip_mreqn
{
	struct in_addr	imr_multiaddr;	//组播组的IP地址
	struct in_addr 	imr_interface;	// 本地某一网络设备接口的IP地址
	int				imr_ifindex;	// 网卡编号
};
struct in_addr
{
	in_addr_t s_addr;
};

示例:

// server.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <arpa/inet.h>
#include <net/if.h>

int main(int argc, const char *argv[])
{
	// 创建套接字
	int fd = socket(AF_INET, SOCK_DGRAM, 0);
	if(fd == -1)
	{
		perror("socket error");
		exit(-1);
	}
	// 绑定IP和端口
	struct sockaddr_in serv;
	memset(&serv, 0x00, sizeof(serv));
	serv.sin_family = AF_INET;
	serv.sin_port = htons(8888);
	serv.sin_addr.s_addr = htonl(INADDR_ANY);
	int ret = bind(fd, (struct sockaddr*)&serv, sizeof(serv));
	if(ret == -1)
	{
		perror("bind error:");
		exit(-2);
	}

	// 初始化客户端地址信息
	struct sockaddr_in client;
	memset(&client, 0x00, sizeof(client));
	client.sin_family = AF_INET;
	client.sin_port = htons(9999);
	// 使用组播地址给客户端发送数据
	inet_pton(AF_INET, "239.0.0.10", &client.sin_addr.s_addr);
	// 给服务器开放组播权限
	struct ip_mreqn flag;
	inet_pton(AF_INET, "239.0.0.10", &flag.imr_multiaddr.s_addr); // 组播地址
	inet_pton(AF_INET, "0.0.0.0", &flag.imr_address.s_addr);	// 本地IP
	flag.imr_ifindex = if_nametoindex("ens33"); // ens33:网卡名称
	setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &flag, sizeof(flag));
	// 通信
	while(1)
	{
		//一直给客户端发送数据
		static int num = 0;
		char buf[1024] = { 0 };
		sprintf(buf, "hello, num = %d\n", num++);
		if(sendto(fd, buf, strlen(buf) + 1, 0, (struct sockaddr*)&client, sizeof(client)) == -1)
		{
			perror("sendto error:");
			break;
		}
		printf("send buf:%s\n", buf);
		sleep(1);
	}
	close(fd);
	return 0;
}
// client.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <arpa/inet.h>
#include <net/if.h>

int main(int argc, const char *argv[])
{
	// 创建套接字
	int fd = socket(AF_INET, SOCK_DGRAM, 0);
	if(fd == -1)
	{
		perror("socket error");
		exit(-1);
	}
	// 绑定IP和端口
	struct sockaddr_in client;
	memset(&client, 0x00, sizeof(client));
	client.sin_family = AF_INET;
	client.sin_port = htons(9999);
	inet_pton(AF_INET, "0.0.0.0", &client.sin_addr.s_addr);
	int ret = bind(fd, (struct sockaddr*)&client, sizeof(client));
	// 加入到组播地址
	struct ip_mreqn flag;
	inet_pton(AF_INET, "239.0.0.10", &flag.imr_multiaddr.s_addr);
	inet_pton(AF_INET, "0.0.0.0", &flag.imr_address.s_addr);
	flag.imr_ifindex = if_nametoindex("ens33"); // ens33:网卡名称
	setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &flag, sizeof(flag));
	if(ret == -1)
	{
		perror("bind error:");
		exit(-2);
	}
	while(1)
	{
		char buf[1024] = { 0 };
		int len = recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);
		if(len == -1)
		{
			perror("recvfrom error:");
			exit(-3);
		}
		printf("client - recv buf:%s\n", buf);
	}
	close(fd);
	return 0;
}

6.3 本地套接字

文件格式:

  • 套接字:s
    • 和管道文件§一样都是伪文件,大小为0,不存储在磁盘中,存储在内核缓冲区中

服务器端:

  • 创建套接字 AF_UNIX | AF_LOCAL
    • int fd =socket(AF_UNIX, SOCK_STREAM, 0);
    • int fd = socket(FA_LOCAL, SOCK_STREAM, 0);
  • 绑定 - struct sockaddr_un
    • struct sockaddr_un serv;
    • serv.sun_family = AF_UNIX
    • strcpy(serv.sun_path, "server.socket"); // 此时server.socket文件不存在
    • bind(fd, (struct sockaddr*)&serv, sizeof(serv)); // 绑定成功后server.socket文件被创建
  • 设置监听
    • listen();
  • 等待接受请求
    • struct sockaddr_un client;
    • int len = sizeof(client);
    • int cfd = accept(fd, &client, &len);
  • 通信
    • send()
    • recv()
  • 断开连接
    • close(fd);

客户端:

  • 创建套接字 AF_UNIX | AF_LOCAL
    • int fd =socket(AF_UNIX, SOCK_STREAM, 0);
    • int fd = socket(FA_LOCAL, SOCK_STREAM, 0);
  • 绑定一个套接字文件
    • struct sockaddr_un client;
    • client.sun_family = AF_UNIX
    • strcpy(client.sun_path, "client.socket");
    • bind(fd, (struct sockaddr*)&client, sizeof(client));
  • 连接服务器
    • struct sockaddr_un serv;
    • serv.sun_family = AF_UNIX
    • strcpy(serv.sun_path, "server.socket");
    • connect(fd, (struct sockaddr*)&serv, sizeof(serv));
  • 通信
    • send()
    • recv()
  • 断开连接
    • close(fd);

相关结构体:

#include <sys/un.h>

#define UNIX_PATH_MAX 108

struct sockaddr_un
{
	__kernel_sa_family_t	sun_family;
	char 					sun_path[UNIX_PATH_MAX];
};

6.3.1 服务器端代码实现

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

int main(int argc, const char *argv[])
{
	// 创建套接字
	int fd = socket(AF_UNIX, SOCK_STREAM, 0);
	// 绑定
	// 如果套接字文件存在,删除套接字文件
	unlink("server.sock");
	struct sockaddr_un serv;
	serv.sun_family = AF_UNIX;
	strcpy(serv.sun_path, "server.sock");
	bind(fd, (struct sockaddr*)&serv, sizeof(serv));
	// 监听
	listen(fd, 36);
	// 等待接收连接请求
	struct sockaddr_un client;
	socklen_t len = sizeof(client);
	int cfd = accept(fd, (struct sockaddr*)&client, &len);
	// 通信
	while(1)
	{
		char buf[1024] = { 0 };
		int reclen = recv(fd, buf, sizeof(buf), 0);
		if(reclen == -1)	break;
		if(reclen == 0)
		{
			printf("client disconnect...\n");
			close(cfd);
			break;
		}
		printf("recv buf:%s\n", buf);
		send(cfd, buf, reclen, 0);
	}
	close(cfd);
	close(fd);
	return 0;
}

6.3.2 客户端实现代码

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

int main(int argc, const char *argv[])
{
	// 创建套接字
	int fd = socket(AF_UNIX, SOCK_STREAM, 0);
	// 给客户端绑定套接字文件
	unlink("client.sock");
	struct sockaddr_un client;
	client.sun_family = AF_UNIX;
	strcpy(client.sun_path, "client.sock");
	bind(fd, (struct sockaddr*)&client, sizeof(client));
	// 连接服务器
	struct sockaddr_un serv;
	serv.sun_family = AF_UNIX;
	strpcy(serv.sun_path, "server.sock");
	connect(fd, (struct sockaddr*)&serv, sizeof(serv));
	// 通信
	while(1)
	{
		char buf[1024] = { 0 };
		fgets(buf, sizeof(buf), stdin);
		send(fd, buf, strlen(buf)+1,0);
		recv(fd, buf, sizeof(buf), 0);
		printf("recv:%s\n", buf);
	}
	return 0;
}

6.4 心跳包

  • 判断客户端和服务器是否处于连接状态
    - 心跳机制
    - 不会携带大量的数据
    - 每个一定时间服务器->客户端/客户端->服务
    器发送一个数据包
  • 心跳包看成一个协议
    - 应用层协议
  • 判断网络是否断开
    - 有多个连续的心跳包没收到/没有回复
    - 关闭通信的套接字
  • 重连
    - 重新初始套接字
    - 继续发送心跳包
  • 乒乓包
    - 比心跳包携带的数据多一些
    - 除了知道连接是否存在, 还能获取一些信息
  • 23
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT灰猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值