windows下IPv6 UDP组播(C++、MFC)

windows下IPv6 UDP组播(C++、MFC)📞

更多精彩内容
👉个人内容分类汇总 👈

🌈 linux下 IPv6组播(C++)
🍓windows下IPv4 UDP通信(C++、MFC)
📞windows下IPv6 UDP通信(C++、MFC)
🍎 windows下IPv6 UDP组播(C++、MFC)

Server

#include <stdio.h>
#include <Ws2tcpip.h>
#include <winsock2.h>  
#pragma comment(lib,"ws2_32.lib")

#define PORT  6060   
#define IP "ff02::2"    
#define BUF_LEN 256  

int main(int argc, char* argv[])
{
    WSADATA     wsaData;
    WORD wVersionRequested;                   // 版本
    wVersionRequested = MAKEWORD(1, 1);       //版本信息
    WSAStartup(wVersionRequested, &wsaData);  //初始化Windows套接字库

    //使用此结构来指定将套接字连接到的本地或远程端点地址
    struct sockaddr_in6 addr = { AF_INET6, htons(PORT) };
    
    //创建一个UDP套接字
    int l_nServer;
    if ((l_nServer = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
    {
        perror("创建失败");
        return -1;
    }
    bind(l_nServer, (struct sockaddr*)&addr, sizeof(addr));
    //ipv6_mreq结构提供了用于IPv6地址的多播组的信息。
    struct ipv6_mreq group;
    //将接口索引指定为0,则使用默认的多播接口。
    group.ipv6mr_interface = 0;
    //IPv6组播组的地址。
    inet_pton(AF_INET6, IP, &group.ipv6mr_multiaddr);
    //将套接字加入到指定接口上提供的多播组。此选项仅对数据报和原始套接字有效(套接字类>型必须为SOCK_DGRAM或SOCK_RAW)。
    setsockopt(l_nServer, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char*)&group, sizeof(group));

    int l_naddLen = sizeof(addr);
    int l_nReadLen = 0;
    char msgbuf[BUF_LEN];
    printf("等待接收\n");
    while (1)
    {
        l_nReadLen = recvfrom(l_nServer, msgbuf, BUF_LEN, 0, (struct sockaddr*)&addr, &l_naddLen);
        if (l_nReadLen < 0)
        {
            perror("接收失败");
            exit(1);
        }
        msgbuf[l_nReadLen] = '\0';
        printf("%s\n", msgbuf);

        strcpy_s(msgbuf, "world");
        int l_nLen = sendto(l_nServer, msgbuf, strlen(msgbuf), 0, (struct sockaddr*)&addr, sizeof(addr));
        if (l_nLen < 0)
        {
            perror("发送失败");
            exit(1);
        }
        printf("Send %s\n", msgbuf);
    }
   
    return 0;
}

Cilect

#include <stdio.h>
#include <Ws2tcpip.h>
#include <winsock2.h>
#define HELLO_PORT  7905    
#define HELLO_GROUP "224.0.0.1"    
#pragma comment(lib,"ws2_32.lib")

int main(int argc, char* argv[])
{
    WSADATA     wsaData;
    WORD wVersionRequested;
    wVersionRequested = MAKEWORD(1, 1);
    // Initialize Windows socket library
    WSAStartup(0x0202, &wsaData);
    int l_nCilect;

    struct sockaddr_in6 addr = { AF_INET6, htons(6060) };

    /* 创建一个UDP套接字 */
    if ((l_nCilect = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
    {
        perror("创建失败");
        exit(1);
    }
    inet_pton(AF_INET6, "ff02::2", &addr.sin6_addr);

    char message[128];
    int l_naddLen = sizeof(addr);
    while (1)
    {
        strcpy_s(message, "hello");
        int l_nLen = sendto(l_nCilect, message, strlen(message), 0, (struct sockaddr*)&addr, sizeof(addr));
        if (l_nLen < 0)
        {
            perror("发送失败");
            exit(1);
        }
        printf("Send %s\n", message);
        Sleep(1000);
        int l_nReadLen = recvfrom(l_nCilect, message, strlen(message), 0, (struct sockaddr*)&addr, &l_naddLen);
        if (l_nReadLen < 0)
        {
            perror("接收失败");
            exit(1);
        }
        message[l_nReadLen] = '\0';
        printf("%s\n", message);

    }

    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

mahuifa

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

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

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

打赏作者

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

抵扣说明:

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

余额充值