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

 #include <stdio.h>
#include <winsock2.h>
#include <ws2ipdef.h> #pragma comment(lib, "ws2_32.lib") #define BUF_SIZE 30 int main()
{
int iResult = ;
WSADATA wsaData;
SOCKET sock;
char message[BUF_SIZE];
int strlen;
int addrsize; SOCKADDR_IN6 addrto, addrfrom; iResult = WSAStartup(MAKEWORD(, ), &wsaData); sock = socket(AF_INET6, SOCK_RAW, ); /*需要管理员权限*/
if (sock == INVALID_SOCKET)
printf("sock err = %d\n", WSAGetLastError()); addrto.sin6_family = AF_INET6;
addrto.sin6_addr.u.Byte[] = 0xfc;
addrto.sin6_addr.u.Byte[] = 0x00;
addrto.sin6_addr.u.Byte[] = 0x00;
addrto.sin6_addr.u.Byte[] = 0x00;
addrto.sin6_addr.u.Byte[] = 0x00;
addrto.sin6_addr.u.Byte[] = 0x00;
addrto.sin6_addr.u.Byte[] = 0x0d;
addrto.sin6_addr.u.Byte[] = 0x41;
addrto.sin6_addr.u.Byte[] = 0x00;
addrto.sin6_addr.u.Byte[] = 0x00;
addrto.sin6_addr.u.Byte[] = 0x00;
addrto.sin6_addr.u.Byte[] = 0x00;
addrto.sin6_addr.u.Byte[] = 0x0d;
addrto.sin6_addr.u.Byte[] = 0x41;
addrto.sin6_addr.u.Byte[] = 0x00;
addrto.sin6_addr.u.Byte[] = 0x02;
addrto.sin6_port = ; iResult = bind(sock, (SOCKADDR *)&addrto, sizeof(SOCKADDR_IN6));
if (iResult == SOCKET_ERROR)
printf("bind err = %d\n", WSAGetLastError()); while ()
{
addrsize = sizeof(addrfrom);
strlen = recvfrom(sock, message, BUF_SIZE, , (SOCKADDR*)&addrfrom, &addrsize);
} }

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

 #define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <stdio.h>
#include <winsock2.h>
#include <ws2ipdef.h>
#include <ws2tcpip.h> #pragma comment(lib, "ws2_32.lib") #define BUF_SIZE 30
#define _WINSOCK_DEPRECATED_NO_WARNINGS int main()
{
int iResult = ;
WSADATA wsaData;
SOCKET sock;
char message[BUF_SIZE] = "send...";
int strlen;
int addrsize; SOCKADDR_IN addrto, addrfrom; iResult = WSAStartup(MAKEWORD(, ), &wsaData); sock = socket(AF_INET, SOCK_RAW, ); /*需要管理员权限*/
if (sock == INVALID_SOCKET)
printf("sock err = %d\n", WSAGetLastError()); addrfrom.sin_family = AF_INET;
addrfrom.sin_addr.S_un.S_addr = inet_addr("192.168.0.4");
addrfrom.sin_port = ; addrto.sin_family = AF_INET;
addrto.sin_addr.S_un.S_addr = inet_addr("192.168.0.5");
addrto.sin_port = ; iResult = bind(sock, (SOCKADDR *)&addrfrom, sizeof(addrfrom));
if (iResult == SOCKET_ERROR)
printf("bind err = %d\n", WSAGetLastError()); while ()
{
addrsize = sizeof(addrto);
strlen = sendto(sock, message, BUF_SIZE, , (SOCKADDR*)&addrto, addrsize);
} }

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

 #define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <stdio.h>
#include <winsock2.h>
#include <ws2ipdef.h>
#include <ws2tcpip.h>
#include <wsipv6ok.h> #pragma comment(lib, "ws2_32.lib") #define BUF_SIZE 30
#define _WINSOCK_DEPRECATED_NO_WARNINGS int main()
{
int iResult = ;
WSADATA wsaData;
SOCKET sock;
char message[BUF_SIZE] = "send...";
int strlen;
int addrsize;
SOCKADDR_IN6 addrto, addrfrom; iResult = WSAStartup(MAKEWORD(, ), &wsaData); sock = socket(AF_INET6, SOCK_RAW, ); /*需要管理员权限*/
if (sock == INVALID_SOCKET)
printf("sock err = %d\n", WSAGetLastError()); addrfrom.sin6_family = AF_INET6;
iResult = inet_pton(AF_INET6, "2000::2", addrfrom.sin6_addr.u.Byte);
addrfrom.sin6_port = htons();
addrfrom.sin6_scope_id = ; addrto.sin6_family = AF_INET6;
iResult = inet_pton(AF_INET6, "2000::1", addrto.sin6_addr.u.Byte);
addrto.sin6_port = htons();
addrto.sin6_scope_id = ; iResult = bind(sock, (SOCKADDR *)&addrfrom, sizeof(addrfrom));
if (iResult == SOCKET_ERROR)
printf("bind err = %d\n", WSAGetLastError()); while ()
{
addrsize = sizeof(addrto);
strlen = sendto(sock, message, BUF_SIZE, , (SOCKADDR*)&addrto, addrsize);
printf("sendto err = %d\n", WSAGetLastError());
} }

IPv6通,主要是scope_id

 #define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <stdio.h>
#include <winsock2.h>
#include <ws2ipdef.h>
#include <ws2tcpip.h>
#include <wsipv6ok.h> #pragma comment(lib, "ws2_32.lib") #define BUF_SIZE 30
#define _WINSOCK_DEPRECATED_NO_WARNINGS int main()
{
int iResult = ;
WSADATA wsaData;
SOCKET sock;
char message[BUF_SIZE] = "send...";
int strlen;
int addrsize;
SOCKADDR_IN6 addrto, addrfrom; iResult = WSAStartup(MAKEWORD(, ), &wsaData); sock = socket(AF_INET6, SOCK_RAW, ); /*需要管理员权限*/
if (sock == INVALID_SOCKET)
printf("sock err = %d\n", WSAGetLastError()); addrfrom.sin6_family = AF_INET6;
iResult = inet_pton(AF_INET6, "fc00::d41:0:0:d41:2", addrfrom.sin6_addr.u.Byte);
addrfrom.sin6_port = htons();
addrfrom.sin6_scope_id = ; addrto.sin6_family = AF_INET6;
iResult = inet_pton(AF_INET6, "fc00::d41:0:0:d41:1", addrto.sin6_addr.u.Byte);
addrto.sin6_port = htons();
addrto.sin6_scope_id = ; iResult = bind(sock, (SOCKADDR *)&addrfrom, sizeof(addrfrom));
if (iResult == SOCKET_ERROR)
printf("bind err = %d\n", WSAGetLastError()); while ()
{
addrsize = sizeof(addrto);
strlen = sendto(sock, message, BUF_SIZE, , (SOCKADDR*)&addrto, addrsize);
printf("sendto err = %d\n", WSAGetLastError());
} }

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

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

 #define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <stdio.h>
#include <winsock2.h>
#include <ws2ipdef.h>
#include <ws2tcpip.h>
#include <wsipv6ok.h> #pragma comment(lib, "ws2_32.lib") #define BUF_SIZE 4096
#define _WINSOCK_DEPRECATED_NO_WARNINGS int main()
{
int iResult = ;
WSADATA wsaData;
SOCKET sock;
char message[BUF_SIZE] = "send...";
int strlen;
int addrsize;
SOCKADDR_IN6 addrBOARD, addrPC; iResult = WSAStartup(MAKEWORD(, ), &wsaData); sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); /*需要管理员权限*/
if (sock == INVALID_SOCKET)
printf("sock err = %d\n", WSAGetLastError()); addrPC.sin6_family = AF_INET6;
iResult = inet_pton(AF_INET6, "fc00:0:0:d401::1", addrPC.sin6_addr.u.Byte);
addrPC.sin6_port = htons();
addrPC.sin6_scope_id = ; iResult = bind(sock, (SOCKADDR *)&addrPC, sizeof(addrPC));
if (iResult == SOCKET_ERROR)
printf("bind err = %d\n", WSAGetLastError()); iResult = listen(sock, );
if (iResult == SOCKET_ERROR)
printf("listen err = %d\n", WSAGetLastError()); while ()
{
SOCKET sockConnection; addrsize = sizeof(addrBOARD);
sockConnection = accept(sock, (SOCKADDR *)&addrBOARD, &addrsize);
if (sockConnection == INVALID_SOCKET)
printf("sock accept err = %d\n", WSAGetLastError()); recv(sockConnection, message, BUF_SIZE, ); } }

windows socket ipv6 SOCK_RAW的更多相关文章

  1. windows socket函数详解

    windows socket函数详解 近期一直用第三方库写网络编程,反倒是遗忘了网络编程最底层的知识.因而产生了整理Winsock函数库的想法.以下知识点均来源于MSDN,本人只做翻译工作.虽然很多前 ...

  2. linux tcp/ip编程和windows tcp/ip编程差别以及windows socket编程详解

    最近要涉及对接现有应用visual c++开发的tcp客户端,花时间了解了下windows下tcp开发和linux的差别,从开发的角度而言,最大的差别是头文件(早期为了推广尽可能兼容,后面越来越扩展, ...

  3. 转:Windows Socket五种I/O模型

    原文转自:  Windows Socket五种I/O模型 Winsock 的I/O操作: 1. 两种I/O模式 阻塞模式:执行I/O操作完成前会一直进行等待,不会将控制权交给程序.套接字 默认为阻塞模 ...

  4. Windows Socket五种I/O模型——代码全攻略(转)

    Winsock 的I/O操作: 1. 两种I/O模式 阻塞模式:执行I/O操作完成前会一直进行等待,不会将控制权交给程序.套接字 默认为阻塞模式.可以通过多线程技术进行处理. 非阻塞模式:执行I/O操 ...

  5. Windows Socket知识总结

     目录 0 理解Socket 1 WinSock API 2 阻塞socket 3 非阻塞Socket 4 套接字IO模型  4.1 套接字IO模型:select(选择)  4.2 套接字IO模型:W ...

  6. 高性能 Windows Socket 组件 HP-Socket v3.0.2 正式发布

    HP-Socket 是一套通用的高性能 Windows Socket 组件包,包含服务端组件(IOCP 模型)和客户端组件(Event Select 模型),广泛适用于 Windows 平台的 TCP ...

  7. 高性能 Windows Socket 组件 HP-Socket v3.0.1 正式发布

    HP-Socket 是一套通用的高性能 Windows Socket 组件包,包含服务端组件(IOCP 模型)和客户端组件(Event Select 模型),广泛适用于 Windows 平台的 TCP ...

  8. 高性能 Windows Socket 组件 HP-Socket v2.3.1-beta-2 发布

    HP-Socket 是一套通用的高性能 Windows Socket 组件包,包含服务端组件(IOCP 模型)和客户端组件(Event Select 模型),广泛适用于 Windows 平台的 TCP ...

  9. 高性能 Windows Socket 组件 HP-Socket v2.3.1-beta-1 发布

    HP-Socket 是一套通用的高性能 Windows Socket 组件包,包含服务端组件(IOCP 模型)和客户端组件(Event Select 模型),广泛适用于 Windows 平台的 TCP ...

随机推荐

  1. mongodb的文本搜索

    1.当mongodb进程文本搜索的时候, 一个collection,只有一个文本查询的索引. 2.全文索引的定义,搜索的是有意义的词,不是字母 一开始是简单的用中文姓名的姓,如:张,来查询,但是无效, ...

  2. CF 558 C

    Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experime ...

  3. 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第五节:数据流处理的那些事儿

    为什么说到数据流了呢,因为上一节中介绍了一下异步发送请求.同样,在数据流的处理上,C#也为我们提供几个有用的异步处理方法.而且,爬虫这生物,处理数据流是基础本能,比较重要.本着这个原则,就聊一聊吧. ...

  4. Java入门 - 语言基础 - 06.变量类型

    原文地址:http://www.work100.net/training/java-variable-type.html 更多教程:光束云 - 免费课程 变量类型 序号 文内章节 视频 1 概述 2 ...

  5. NanoProfiler-Step1翻译

    NanoProfiler NanoProfiler is a light weight profiling library written in C# which requires (NanoProf ...

  6. 关于github显示不出来图片的问题

    今天打开github,突然发现图标图片等都显示不出来了. 控制台看了一下 百度查找了Failed to load resource: net::ERR_CERT_COMMON_NAME_INVALID ...

  7. 定义可选URL片段 定义自定义片段变量 精通ASP-NET-MVC-5-弗瑞曼

  8. CF990G

    题意 https://codeforces.com/contest/990/problem/G 思考 在200000以内,因数个数最多的数位166320,共有160个因数.可以知道,从一个节点向下走最 ...

  9. iOS开发tip-图片方向

    概述 相信稍微接触过iOS图片相关操作的同学都遇到过图片旋转的问题,另外使用AVFoundation进行拍照的话就会遇到前后摄像头切换mirror问题就让人更摸不着头脑了.今天就简单和大家聊一下iOS ...

  10. ios--->cell里面 self 和self.contentview的区别

    一般我们向cell中添加子视图,有两种方式 [cell addSubview:] [cell.contentView addSubview:] 区别在于进行cell编辑时,比如cell内容向左移或者右 ...