windows下IPv6组播(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;
}

 

posted @ 2020-12-24 22:21  mahuifa  阅读(700)  评论(0编辑  收藏  举报