zoukankan      html  css  js  c++  java
  • 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).

     win10, vs2015

     1 #include <stdio.h>
     2 #include <winsock2.h>
     3 #include <ws2ipdef.h>
     4 
     5 #pragma comment(lib, "ws2_32.lib")
     6 
     7 #define BUF_SIZE 30
     8 
     9 int main()
    10 {
    11     int iResult = 0;
    12     WSADATA wsaData;
    13     SOCKET sock;
    14     char message[BUF_SIZE];
    15     int strlen;
    16     int addrsize;
    17 
    18     SOCKADDR_IN6 addrto, addrfrom;
    19 
    20     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    21 
    22     sock = socket(AF_INET6, SOCK_RAW, 89);    /*需要管理员权限*/
    23     if (sock == INVALID_SOCKET)
    24         printf("sock err = %d
    ", WSAGetLastError());
    25 
    26     addrto.sin6_family = AF_INET6;
    27     addrto.sin6_addr.u.Byte[0] = 0xfc; 
    28     addrto.sin6_addr.u.Byte[1] = 0x00;
    29     addrto.sin6_addr.u.Byte[2] = 0x00;
    30     addrto.sin6_addr.u.Byte[3] = 0x00;
    31     addrto.sin6_addr.u.Byte[4] = 0x00;
    32     addrto.sin6_addr.u.Byte[5] = 0x00;
    33     addrto.sin6_addr.u.Byte[6] = 0x0d;
    34     addrto.sin6_addr.u.Byte[7] = 0x41;
    35     addrto.sin6_addr.u.Byte[8] = 0x00;
    36     addrto.sin6_addr.u.Byte[9] = 0x00;
    37     addrto.sin6_addr.u.Byte[10] = 0x00;
    38     addrto.sin6_addr.u.Byte[11] = 0x00;
    39     addrto.sin6_addr.u.Byte[12] = 0x0d;
    40     addrto.sin6_addr.u.Byte[13] = 0x41;
    41     addrto.sin6_addr.u.Byte[14] = 0x00;
    42     addrto.sin6_addr.u.Byte[15] = 0x02;
    43     addrto.sin6_port = 89;
    44 
    45     iResult = bind(sock, (SOCKADDR *)&addrto, sizeof(SOCKADDR_IN6));
    46     if (iResult == SOCKET_ERROR)
    47         printf("bind err = %d
    ", WSAGetLastError());
    48 
    49     while (1)
    50     {
    51         addrsize = sizeof(addrfrom);
    52         strlen = recvfrom(sock, message, BUF_SIZE, 0, (SOCKADDR*)&addrfrom, &addrsize);
    53     }
    54 
    55 }
    View Code

      

    IPv4版本,需要保证本地连接已接通

     1 #define _WINSOCK_DEPRECATED_NO_WARNINGS
     2 #include <stdio.h>
     3 #include <winsock2.h>
     4 #include <ws2ipdef.h>
     5 #include <ws2tcpip.h>
     6 
     7 #pragma comment(lib, "ws2_32.lib")
     8 
     9 #define BUF_SIZE 30
    10 #define _WINSOCK_DEPRECATED_NO_WARNINGS
    11 
    12 int main()
    13 {
    14     int iResult = 0;
    15     WSADATA wsaData;
    16     SOCKET sock;
    17     char message[BUF_SIZE] = "send...";
    18     int strlen;
    19     int addrsize;
    20 
    21     SOCKADDR_IN addrto, addrfrom;
    22 
    23     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    24 
    25     sock = socket(AF_INET, SOCK_RAW, 89);    /*需要管理员权限*/
    26     if (sock == INVALID_SOCKET)
    27         printf("sock err = %d
    ", WSAGetLastError());
    28 
    29     addrfrom.sin_family = AF_INET;
    30     addrfrom.sin_addr.S_un.S_addr = inet_addr("192.168.0.4");
    31     addrfrom.sin_port = 89;
    32 
    33     addrto.sin_family = AF_INET;
    34     addrto.sin_addr.S_un.S_addr = inet_addr("192.168.0.5");
    35     addrto.sin_port = 89;
    36 
    37     iResult = bind(sock, (SOCKADDR *)&addrfrom, sizeof(addrfrom));
    38     if (iResult == SOCKET_ERROR)
    39         printf("bind err = %d
    ", WSAGetLastError());
    40 
    41     while (1)
    42     {
    43         addrsize = sizeof(addrto);
    44         strlen = sendto(sock, message, BUF_SIZE, 0, (SOCKADDR*)&addrto, addrsize);
    45     }
    46 
    47 }
    View Code

      

     IPv6版本,2000::2可以通,addrfrom.sin6_scope_id = 0;地址范围设置是必须的,若为unicast地址可以设置为0。需要赋值,不然内存默认可能为cc。

     1 #define _WINSOCK_DEPRECATED_NO_WARNINGS
     2 #include <stdio.h>
     3 #include <winsock2.h>
     4 #include <ws2ipdef.h>
     5 #include <ws2tcpip.h>
     6 #include <wsipv6ok.h>
     7 
     8 #pragma comment(lib, "ws2_32.lib")
     9 
    10 #define BUF_SIZE 30
    11 #define _WINSOCK_DEPRECATED_NO_WARNINGS
    12 
    13 int main()
    14 {
    15     int iResult = 0;
    16     WSADATA wsaData;
    17     SOCKET sock;
    18     char message[BUF_SIZE] = "send...";
    19     int strlen;
    20     int addrsize;
    21     SOCKADDR_IN6 addrto, addrfrom;
    22 
    23     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    24 
    25     sock = socket(AF_INET6, SOCK_RAW, 89);    /*需要管理员权限*/
    26     if (sock == INVALID_SOCKET)
    27         printf("sock err = %d
    ", WSAGetLastError());
    28 
    29     addrfrom.sin6_family = AF_INET6;
    30     iResult = inet_pton(AF_INET6, "2000::2", addrfrom.sin6_addr.u.Byte);
    31     addrfrom.sin6_port = htons(89);
    32     addrfrom.sin6_scope_id = 0;
    33 
    34     addrto.sin6_family = AF_INET6;
    35     iResult = inet_pton(AF_INET6, "2000::1", addrto.sin6_addr.u.Byte);
    36     addrto.sin6_port = htons(89);
    37     addrto.sin6_scope_id = 0;
    38 
    39     iResult = bind(sock, (SOCKADDR *)&addrfrom, sizeof(addrfrom));
    40     if (iResult == SOCKET_ERROR)
    41         printf("bind err = %d
    ", WSAGetLastError());
    42 
    43     while (1)
    44     {
    45         addrsize = sizeof(addrto);
    46         strlen = sendto(sock, message, BUF_SIZE, 0, (SOCKADDR*)&addrto, addrsize);
    47         printf("sendto err = %d
    ", WSAGetLastError());
    48     }
    49 
    50 }
    View Code

      

     IPv6通,主要是scope_id

     1 #define _WINSOCK_DEPRECATED_NO_WARNINGS
     2 #include <stdio.h>
     3 #include <winsock2.h>
     4 #include <ws2ipdef.h>
     5 #include <ws2tcpip.h>
     6 #include <wsipv6ok.h>
     7 
     8 #pragma comment(lib, "ws2_32.lib")
     9 
    10 #define BUF_SIZE 30
    11 #define _WINSOCK_DEPRECATED_NO_WARNINGS
    12 
    13 int main()
    14 {
    15     int iResult = 0;
    16     WSADATA wsaData;
    17     SOCKET sock;
    18     char message[BUF_SIZE] = "send...";
    19     int strlen;
    20     int addrsize;
    21     SOCKADDR_IN6 addrto, addrfrom;
    22 
    23     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    24 
    25     sock = socket(AF_INET6, SOCK_RAW, 89);    /*需要管理员权限*/
    26     if (sock == INVALID_SOCKET)
    27         printf("sock err = %d
    ", WSAGetLastError());
    28 
    29     addrfrom.sin6_family = AF_INET6;
    30     iResult = inet_pton(AF_INET6, "fc00::d41:0:0:d41:2", addrfrom.sin6_addr.u.Byte);
    31     addrfrom.sin6_port = htons(89);
    32     addrfrom.sin6_scope_id = 0;
    33 
    34     addrto.sin6_family = AF_INET6;
    35     iResult = inet_pton(AF_INET6, "fc00::d41:0:0:d41:1", addrto.sin6_addr.u.Byte);
    36     addrto.sin6_port = htons(89);
    37     addrto.sin6_scope_id = 0;
    38 
    39     iResult = bind(sock, (SOCKADDR *)&addrfrom, sizeof(addrfrom));
    40     if (iResult == SOCKET_ERROR)
    41         printf("bind err = %d
    ", WSAGetLastError());
    42 
    43     while (1)
    44     {
    45         addrsize = sizeof(addrto);
    46         strlen = sendto(sock, message, BUF_SIZE, 0, (SOCKADDR*)&addrto, addrsize);
    47         printf("sendto err = %d
    ", WSAGetLastError());
    48     }
    49 
    50 }
    View Code

      

     windows10,vs2015,控制台C程序,使用socket TCP作为服务器,接收,接收数据。

    一开始wireshark上都能看到SYN包了,但就是一直阻塞在accept上,原因是防火墙屏蔽了2020端口,把防火墙关闭之后就可以了。

     1 #define _WINSOCK_DEPRECATED_NO_WARNINGS
     2 #include <stdio.h>
     3 #include <winsock2.h>
     4 #include <ws2ipdef.h>
     5 #include <ws2tcpip.h>
     6 #include <wsipv6ok.h>
     7 
     8 #pragma comment(lib, "ws2_32.lib")
     9 
    10 #define BUF_SIZE 4096
    11 #define _WINSOCK_DEPRECATED_NO_WARNINGS
    12 
    13 int main()
    14 {
    15     int iResult = 0;
    16     WSADATA wsaData;
    17     SOCKET sock;
    18     char message[BUF_SIZE] = "send...";
    19     int strlen;
    20     int addrsize;
    21     SOCKADDR_IN6 addrBOARD, addrPC;
    22 
    23     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    24 
    25     sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);    /*需要管理员权限*/
    26     if (sock == INVALID_SOCKET)
    27         printf("sock err = %d
    ", WSAGetLastError());
    28 
    29     addrPC.sin6_family = AF_INET6;
    30     iResult = inet_pton(AF_INET6, "fc00:0:0:d401::1", addrPC.sin6_addr.u.Byte);
    31     addrPC.sin6_port = htons(2020);
    32     addrPC.sin6_scope_id = 0;
    33 
    34     iResult = bind(sock, (SOCKADDR *)&addrPC, sizeof(addrPC));
    35     if (iResult == SOCKET_ERROR)
    36         printf("bind err = %d
    ", WSAGetLastError());
    37 
    38     iResult = listen(sock, 5);
    39     if (iResult == SOCKET_ERROR)
    40         printf("listen err = %d
    ", WSAGetLastError());
    41 
    42     while (1)
    43     {
    44         SOCKET sockConnection;
    45         
    46         addrsize = sizeof(addrBOARD);
    47         sockConnection = accept(sock, (SOCKADDR *)&addrBOARD, &addrsize);
    48         if (sockConnection == INVALID_SOCKET)
    49             printf("sock accept err = %d
    ", WSAGetLastError());
    50 
    51         recv(sockConnection, message, BUF_SIZE, 0);
    52         
    53     }
    54 
    55 }
    View Code
  • 相关阅读:
    TCP 登录实现代码
    网络编程步骤 乔老师整理
    网络编程步骤 乔老师整理
    UDP 编程 客服咨询回复
    UDP 编程 客服咨询回复
    利用java在服务器和客服端建立连接,进行通讯(代码实例)
    利用java在服务器和客服端建立连接,进行通讯(代码实例)
    InetAddress 类简介
    InetAddress 类简介
    Mysql并发时经典常见的死锁原因及解决方法
  • 原文地址:https://www.cnblogs.com/yanhc/p/10306304.html
Copyright © 2011-2022 走看看