【问题标题】:Windows Socket unable to bind on VPN IP addressWindows Socket 无法绑定 VPN IP 地址
【发布时间】:2014-03-24 13:34:46
【问题描述】:

我正在尝试绑定到通过 VPN 网络的特定 IP,我能够 ping 它、连接它并且还能够在特定端口上 telnet,但我的 windows MFC 程序给出错误代码 10049,我无法进一步了解调试此问题的任何帮助将不胜感激,我在 Visual Studio 2012 Win 7 上运行它,远程客户端在 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);*/
           }
       }
   }

谢谢

【问题讨论】:

    标签: c++ c windows sockets mfc


    【解决方案1】:

    您无法绑定到远程地址,正如您的错误所示,就是这种情况。您使用本地 IP 和端口绑定系统调用。

    这是 MSDN 关于您的错误的说明:

    WSAEADDRNOTAVAIL 10049

    无法分配请求的地址。请求的地址在 它的上下文。这通常是由于尝试绑定到 对本地计算机无效的地址。这也可能导致 从 connect、sendto、WSAConnect、WSAJoinLeaf 或 WSASendTo 时 远程地址或端口对远程计算机无效(对于 例如,地址或端口 0)。

    【讨论】:

    • 但是套接字是UDP套接字,我正在绑定它,这样我就不必使用sendto调用了。
    • 您使用什么类型的套接字并不重要。绑定调用基本上使您的进程在套接字(IP + 端口)上侦听连接。
    猜你喜欢
    • 2019-07-25
    • 1970-01-01
    • 2013-01-16
    • 2015-02-27
    • 2011-09-02
    • 2016-08-26
    • 1970-01-01
    • 2018-03-27
    • 2019-12-16
    相关资源
    最近更新 更多