码迷,mamicode.com
首页 > Windows程序 > 详细

windows socket ipv6 SOCK_RAW

时间:2019-01-22 22:52:56      阅读:577      评论:0      收藏:0      [点我收藏+]

标签:its   eof   end   http   family   table   ast   为什么   .lib   

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);
    }

}

 

windows socket ipv6 SOCK_RAW

标签:its   eof   end   http   family   table   ast   为什么   .lib   

原文地址:https://www.cnblogs.com/yanhc/p/10306304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!