//multicast_chat.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pthread.h>
#include <ifaddrs.h>
#define MULTICAST_ADDR "224.10.10.10" // 组播地址
#define BUFFER_SIZE 1024
int sock;
struct sockaddr_in multicast_addr;
struct ip_mreq mreq;
char my_ip[INET_ADDRSTRLEN];
void* receive_thread(void* arg) {
char buffer[BUFFER_SIZE];
struct sockaddr_in local_addr;
socklen_t addr_len = sizeof(local_addr);
int nbytes;
// 接收组播消息
while (1) {
nbytes = recvfrom(sock, buffer, BUFFER_SIZE, 0, (struct sockaddr*)&local_addr, &addr_len);
if (nbytes < 0) {
perror("recvfrom");
pthread_exit(NULL);
}
buffer[nbytes] = '\0'; // Null-terminate the received data
printf("收到来自 %s 的消息: %s\n", inet_ntoa(local_addr.sin_addr), buffer);
printf("请输入消息(exit退出): \n");
}
}
void* send_thread(void* arg) {
char buffer[BUFFER_SIZE];
char join_msg[BUFFER_SIZE];
char leave_msg[BUFFER_SIZE];
// 创建加入和离开消息
snprintf(join_msg, sizeof(join_msg), "用户: %s , 加入聊天室.", my_ip);
snprintf(leave_msg, sizeof(leave_msg), "用户: %s , 离开聊天室.", my_ip);
// 发送组播消息
printf("请输入消息(exit退出): ");
while (1) {
fgets(buffer, BUFFER_SIZE, stdin);
buffer[strcspn(buffer, "\n")] = 0; // 去掉换行符
if (strcmp(buffer, "exit") == 0) {
// 发送离开消息
if (sendto(sock, leave_msg, strlen(leave_msg), 0, (struct sockaddr*)&multicast_addr, sizeof(multicast_addr)) < 0) {
perror("sendto leave_msg");
pthread_exit(NULL);
}
// 退出组播组
if (setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
perror("setsockopt IP_DROP_MEMBERSHIP");
close(sock);
exit(EXIT_FAILURE);
}
pthread_exit(NULL);
}
// 发送消息到组播组
if (sendto(sock, buffer, strlen(buffer), 0, (struct sockaddr*)&multicast_addr, sizeof(multicast_addr)) < 0) {
perror("sendto");
pthread_exit(NULL);
}
}
}
int main(int argc, char* argv[]) {
struct sockaddr_in local_addr;
pthread_t recv_thread_id, snd_thread_id;
struct ifaddrs* ifaddr, * ifa;
socklen_t len = sizeof(struct sockaddr_in);
if (argc != 2) {
fprintf(stderr, "Usage: %s <port>\n", argv[0]);
exit(EXIT_FAILURE);
}
int port = atoi(argv[1]);
if (port <= 0 || port > 65535) {
fprintf(stderr, "Invalid port number.\n");
exit(EXIT_FAILURE);
}
// 创建UDP套接字
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
// 允许地址重用
int reuse = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
perror("setsockopt SO_REUSEADDR");
close(sock);
exit(EXIT_FAILURE);
}
// 配置本地地址和端口
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = INADDR_ANY;
local_addr.sin_port = htons(port);
// 绑定套接字到本地地址
if (bind(sock, (struct sockaddr*)&local_addr, sizeof(local_addr)) < 0) {
perror("bind");
close(sock);
exit(EXIT_FAILURE);
}
// 获取本地IP地址
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
close(sock);
exit(EXIT_FAILURE);
}
// 寻找非回环接口的IPv4地址
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET && strcmp(ifa->ifa_name, "lo") != 0) {
inet_ntop(AF_INET, &((struct sockaddr_in*)ifa->ifa_addr)->sin_addr, my_ip, sizeof(my_ip));
break;
}
}
freeifaddrs(ifaddr);
if (my_ip[0] == '\0') {
fprintf(stderr, "No valid network interface found.\n");
close(sock);
exit(EXIT_FAILURE);
}
// 加入组播组
mreq.imr_multiaddr.s_addr = inet_addr(MULTICAST_ADDR);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
perror("setsockopt IP_ADD_MEMBERSHIP");
close(sock);
exit(EXIT_FAILURE);
}
// 配置组播地址
memset(&multicast_addr, 0, sizeof(multicast_addr));
multicast_addr.sin_family = AF_INET;
multicast_addr.sin_addr.s_addr = inet_addr(MULTICAST_ADDR);
multicast_addr.sin_port = htons(port);
// 创建接收线程
if (pthread_create(&recv_thread_id, NULL, receive_thread, NULL) != 0) {
perror("pthread_create recv_thread");
close(sock);
exit(EXIT_FAILURE);
}
// 创建发送线程
if (pthread_create(&snd_thread_id, NULL, send_thread, NULL) != 0) {
perror("pthread_create snd_thread");
close(sock);
exit(EXIT_FAILURE);
}
// 发送加入消息
char join_msg[BUFFER_SIZE];
snprintf(join_msg, sizeof(join_msg), "用户: %s , 加入聊天室.", my_ip);
if (sendto(sock, join_msg, strlen(join_msg), 0, (struct sockaddr*)&multicast_addr, sizeof(multicast_addr)) < 0) {
perror("sendto join_msg");
close(sock);
exit(EXIT_FAILURE);
}
// 等待线程结束
pthread_join(recv_thread_id, NULL);
pthread_join(snd_thread_id, NULL);
close(sock);
return 0;
}
编译命令
gcc -o multicast_chat multicast_chat.c -lpthread