我试图绑定到特定的IP,这是在VPNnetworking上,我可以ping它,连接它,也能够telnet特定的端口,但我的Windows MFC程序给出错误代码10049,我无法进一步帮助在debugging这个问题将不胜感激,我运行在Visual Studio 2012赢7和远程客户端运行在Linux变种。
这是代码的一部分,我得到的错误基本上IP地址是可configuration的,但我硬编码debugging。
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);*/ } } }
谢谢
你不能绑定到远程地址,就像你的错误显示的那样,是这样的。 您使用本地IP和端口绑定系统调用。
以下是MSDN有关您的错误的内容:
WSAEADDRNOTAVAIL 10049
无法分配请求的地址。 请求的地址在上下文中无效。 这通常是由于尝试绑定到对本地计算机无效的地址所致。 当远程地址或端口对于远程计算机(例如地址或端口0)无效时,也可能由连接,sendto,WSAConnect,WSAJoinLeaf或WSASendTo导致。