windows socket ipv6 SOCK_RAW

Posted yanhc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
", 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
", WSAGetLastError());

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

}

 

以上是关于windows socket ipv6 SOCK_RAW的主要内容,如果未能解决你的问题,请参考以下文章

尝试将python套接字绑定到IPv6地址时参数无效

windows socket 编程:bind 错误码-1 如何解决?

socket

[python网络编程]socket的简单使用

Socket套接字

socket模块__python