windows socket ipv6 SOCK_RAW

bind处一直报错WSAEADDRNOTAVAIL10049,不知道为什么?

WSAEADDRNOTAVAIL
10049
Cannot assign requested address.
The requested address is not valid in its context. This normally results from an attempt to bind to an address that is not valid for the local computer. This can also result from connect, sendto, WSAConnect, WSAJoinLeaf, or WSASendTo when the remote address or port is not valid for a remote computer (for example, address or port 0).
#include <stdio.h>
#include <winsock2.h>
#include <ws2ipdef.h>

#pragma comment(lib, "ws2_32.lib")

#define BUF_SIZE 30

int main()
{
    int iResult = 0;
    WSADATA wsaData;
    SOCKET sock;
    char message[BUF_SIZE];
    int strlen;
    int addrsize;

    SOCKADDR_IN6 addrto, addrfrom;

    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);

    sock = socket(AF_INET6, SOCK_RAW, 89);    /*需要管理员权限*/
    if (sock == INVALID_SOCKET)
        printf("sock err = %d\n", WSAGetLastError());

    addrto.sin6_family = AF_INET6;
    addrto.sin6_addr.u.Byte[0] = 0xfc; 
    addrto.sin6_addr.u.Byte[1] = 0x00;
    addrto.sin6_addr.u.Byte[2] = 0x00;
    addrto.sin6_addr.u.Byte[3] = 0x00;
    addrto.sin6_addr.u.Byte[4] = 0x00;
    addrto.sin6_addr.u.Byte[5] = 0x00;
    addrto.sin6_addr.u.Byte[6] = 0x0d;
    addrto.sin6_addr.u.Byte[7] = 0x41;
    addrto.sin6_addr.u.Byte[8] = 0x00;
    addrto.sin6_addr.u.Byte[9] = 0x00;
    addrto.sin6_addr.u.Byte[10] = 0x00;
    addrto.sin6_addr.u.Byte[11] = 0x00;
    addrto.sin6_addr.u.Byte[12] = 0x0d;
    addrto.sin6_addr.u.Byte[13] = 0x41;
    addrto.sin6_addr.u.Byte[14] = 0x00;
    addrto.sin6_addr.u.Byte[15] = 0x02;
    addrto.sin6_port = 89;

    iResult = bind(sock, (SOCKADDR *)&addrto, sizeof(SOCKADDR_IN6));
    if (iResult == SOCKET_ERROR)
        printf("bind err = %d\n", WSAGetLastError());

    while (1)
    {
        addrsize = sizeof(addrfrom);
        strlen = recvfrom(sock, message, BUF_SIZE, 0, (SOCKADDR*)&addrfrom, &addrsize);
    }

}

猜你喜欢

转载自www.cnblogs.com/yanhc/p/10306304.html