Windows套接字无法绑定VPN IP地址

Windows Socket unable to bind on VPN IP address

本文关键字:VPN IP 地址 绑定 套接字 Windows      更新时间:2023-10-16

我试图绑定到通过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)。