socket 实现最小化组播(多播)程序

2023-10-20 18:21:27 浏览数 (3)

组播是在局域网中向一个子网内加入了某个组播组的客户端发送批量发送数据,它类似于广播但并非广播,组播的地址有固定的几种,本案例中使用了239.0.0.2这个组播地址,具体实现代码分客户端和服务端,请参考:


服务端代码

代码语言:javascript复制
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <net/if.h>
#define SERVER_PORT 8000
#define CLIENT_PORT 9000
#define MAXLINE1500
#define GROUP”239.0.0.2”
int main(int argc, char* argv[])
{
int sock = socket(AF_INET, SOCK_DGRAM, 0);
// 服务器监听本机所有网卡 8000 端口
struct sockaddr_in srvaddr;
memset(&srvaddr, 0, sizeof(srvaddr));
srvaddr.sin_family= AF_INET;
srvaddr.sin_addr.s_addr= htonl(INADDR_ANY);
srvaddr.sin_port= htons(SERVER_PORT);
bind(sock, (struct sockaddr*)&srvaddr, sizeof(srvaddr));
// 设置组播地址
struct ip_mreqn group;
inet_pton(AF_INET, GROUP, &group.imr_multiaddr);// 组
inet_pton(AF_INET, “0.0.0.0”, &group.imr_address);// ip
group.imr_ifindex = if_nametoindex(“eth0”);// 网卡索引号
// 设置发送数据包是组播方式发送
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &group, sizeof(group));
// 构建发送目标结构体
struct sockaddr_in cntaddr;
memset(&cntaddr, 0, sizeof(cntaddr));
cntaddr.sin_family= AF_INET;
cntaddr.sin_port= htons(CLIENT_PORT);// 目标端口
inet_pton(AF_INET, GROUP, &cntaddr.sin_addr.s_addr);// 目标的组地址
char buf[MAXLINE];
while (1)
{
fgets(buf, sizeof(buf), stdin);
sendto(sock, buf, strlen(buf), 0,
(struct sockaddr*)&cntaddr, sizeof(cntaddr));
sleep(1);
}
close(sock);
return 0;
}
客户端代码
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <net/if.h>
#define SERVER_PORT 8000
#define CLIENT_PORT 9000
#define MAXLINE4096
#define GROUP”239.0.0.2”
int main(int argc, char* argv[])
{
int result;
int sock = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in localaddr;
memset(&localaddr, 0, sizeof(localaddr));
localaddr.sin_family= AF_INET;
localaddr.sin_port= htons(CLIENT_PORT);
inet_pton(AF_INET, “0.0.0.0”, &localaddr.sin_addr.s_addr);
result = bind(sock, (struct sockaddr*)&localaddr, sizeof(localaddr));
if (-1 == result)
{
perror(“bind”);
}
struct ip_mreqn group;
inet_pton(AF_INET, GROUP, &group.imr_multiaddr);
inet_pton(AF_INET, “0.0.0.0”, &group.imr_address);
group.imr_ifindex = if_nametoindex(“eth0”);
setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &group, sizeof(group));
int ret;
char buf[MAXLINE];
while (1)
{
ret = recvfrom(sock, buf, sizeof(buf), 0, NULL, 0);
write(STDOUT_FILENO, buf, ret);
}
close(sock);
return 0;
}

编译

编译客户端:gcc multi_client.c -o multi_client 编译服务端:gcc multi_server.c -o multi_server 通讯截图:

0 人点赞