Windows套接字无法绑定VPN IP地址
Windows Socket unable to bind on VPN IP address
我试图绑定到通过VPN网络的特定IP,我能够ping它,连接它,也能够在特定端口上telnet,但我的windows MFC程序给出错误代码10049,我无法进一步帮助调试这个问题将受到赞赏,我在Visual Studio 2012 win7上运行这个,远程客户端在Linux变体上运行。
这是我得到错误的代码的一部分,基本上IP地址是可配置的,但我硬编码它来调试。
CStarDoc *p_doc = (CStarDoc*) lpparam;
BOOL fFlag = TRUE;
const int MAX_MSGLEN = max(sizeof(DISP_INFO_T ), sizeof(REASON_STRING_T ));
char buffer[MAX_MSGLEN];
DISP_INFO_T *p_disp_info_buffer = (DISP_INFO_T *) buffer;
DISP_INFO_T disp_info_combined; //receiving combined butter
DISP_INFO_T_1 *p_disp_info_buffer1; //receiving buffer pointer for DispInfo1
DISP_INFO_T_2 *p_disp_info_buffer2; //receiving buffer pointer for DispInfo2
int msgReceived = 0; // Initially, is 0.
// For the same msgNumber, when the program receives the first portion of buffer, set to 1,
// When the program receives both portions, set it to 0.
// When the program misses any portion for the same msgNumber, set to 0 also.
int currentMsgNum1 = 0;
int currentMsgNum2 = 0;
int err;
CString msg;
SOCKADDR_IN saUDPPortIn;
SOCKADDR_IN From;
struct addrinfo *result = NULL;
struct addrinfo *ptr = NULL;
struct addrinfo hints;
::memset( &hints,0, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
//hints.ai_socktype = SOCK_DGRAM;
//hints.ai_protocol = IPPROTO_UDP;
char asideip[] = "192.168.1.129";
BOOL OtherSideIsStandby = FALSE;
static BOOL DoFirstMsg = TRUE;
// p_disp_info_combined = & disp_info_combined;
p_doc->ThreadRunning = TRUE;
p_doc->udpsocket = socket(AF_INET, SOCK_DGRAM, 0);
if (INVALID_SOCKET == p_doc->udpsocket)
{
CString msg = "Invalid socket: "+ WSAGetLastError();
AfxMessageBox(msg);
return(-1);
}
long ip = 0;
int sockbufsize = 0;
int timeout = 2000;
// This is the IP that matches the IP of the QNX machines in all but the last octet.
// Note: it is in host byte format.
int errcode = getaddrinfo(asideip,NULL,&hints,&result);
for(ptr = result;ptr != NULL ;ptr=ptr->ai_next)
{
switch (ptr->ai_family)
{
default: break;
case AF_INET :
ip = p_doc->MyIP;
saUDPPortIn.sin_family = AF_INET;
saUDPPortIn.sin_addr.s_addr = (((SOCKADDR_IN*) ptr->ai_addr)->sin_addr).s_addr;
saUDPPortIn.sin_port = htons(p_doc->port_addr );
int length = sizeof(buffer) *2;
//err = setsockopt(p_doc->udpsocket,SOL_SOCKET, SO_REUSEADDR, (char *)&fFlag, sizeof(fFlag));
//err = setsockopt(p_doc->udpsocket,SOL_SOCKET, SO_BROADCAST, (char *)&fFlag, sizeof(fFlag));
err = setsockopt(p_doc->udpsocket, SOL_SOCKET, SO_RCVBUF, (char *)&length, sizeof(length));
// Keep from hanging forever.
err = setsockopt(p_doc->udpsocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
err = bind(p_doc->udpsocket, (SOCKADDR FAR *)&saUDPPortIn, sizeof(SOCKADDR_IN));
if (err == SOCKET_ERROR)
{
int errcode = WSAGetLastError();
closesocket(p_doc->udpsocket);
/* msg.Format("Network Connectivity failed, Please Check Network. ");
AfxMessageBox(msg);
closesocket(p_doc->udpsocket);
p_doc->udpsocket = -1; // task is trying to attach to the port.
return(1);*/
}
}
}
谢谢
您无法绑定到远程地址,正如您的错误所示,这就是这种情况。你使用bind系统调用本地IP和端口。
MSDN给出的错误提示如下:
WSAEADDRNOTAVAIL 10049
无法分配请求的地址。请求的地址在…中无效它的上下文。这通常是由于试图绑定到对本地计算机无效的地址。这也可能导致from connect, sendto, WSAConnect, WSAJoinLeaf,或WSASendTo远程地址或端口对远程计算机(为)无效例如,地址或端口0)。
相关文章:
- 我正在开发服务器,ip作为参数传递不起作用
- 如何使用 Boost Asio 在 Android 上获取我的本地 udp IP 地址?
- 我可以与 python 服务器而不是 c++ 客户端建立 tcp/ip 套接字吗?
- C++ boost::asio::ip::tcp::acceptor 有时不接受连接器?
- 从网址获取 IP (C++)
- 为什么 WinInet 在通过 FQDN 连接时无法通过协商自动进行身份验证,但如果通过 IP 连接则成功?
- 如何修复无效的API密钥,IP或操作权限错误?
- C++ Winsock2 客户端未通过远程 IP 连接到服务器
- PHP Profiler语言 - 使用 C++ 从 PHP 扩展获取客户端 IP 和 URL
- 根据所选应用程序为 macOS 配置 VPN 拆分隧道
- 为什么 boost::asio::ip::tcp::basic_stream_socket::available 的运行
- 如何使用 libcurl 在本地 IP 上获取 HTML?
- 在C++中使用宏验证 IP 时出错
- 使用 IP 帮助程序的 Windows 中活动 UDP 连接的远程地址
- 将静态 IP 分配给我的 ESP32 服务器后不再响应
- C++ / Qt:如何检测主机名或IP地址何时引用当前系统?
- "acceptor"在从TCP迁移到UDP时不是"boost::asio::ip::udp
- 用于C++的 TCP/IP 解析器和响应器
- C++获取本地IP地址,区分VPN和本地网络
- Windows套接字无法绑定VPN IP地址