I have two network cards on my machine and I retrieve their info by the following code, mainly their IP addresses:
Later I user this address in setting up some sockets:Code:hostent *host = NULL; char hostName[128] = ""; sockaddr_in sockAddr; char m_UIWIp[2][16]; CString tempIP; gethostname(hostName, sizeof(hostName)); host = gethostbyname(hostName); if (!host) { AfxMessageBox("Error getting local IP address", MB_OK); } else { //set IPs for both network cards for (int i = 0; host->h_addr_list[i]; i++) { memcpy(&sockAddr.sin_addr, host->h_addr_list[i], host->h_length); tempIP = inet_ntoa(sockAddr.sin_addr); <snip> m_UIWIp[0] = tempIP } }
The problem was one of the network cards wasn't connected to anything and so the IP address being returned was "". So when I tried creating the socket, I was getting the error of WSAEADDRNOTAVAIL which isn't on the list of valid returns for the Create function.Code:for (i = 0; i < 2; i++) { if (!m_WrTCMSockFd[i].Create(WRITE_PORT,SOCK_DGRAM , FD_WRITE, m_UIWIp[i])) { nLastError = m_WrTCMSockFd[i].GetLastError(); // Obtain the error code switch (nLastError) { case WSAEAFNOSUPPORT: MidOutMsg,"Address family is not supported\n"; break; case WSAEMFILE: MidOutMsg="No more file descriptors available\n"; break; case WSAENOBUFS: MidOutMsg="No buffer space is available\n"; break; case WSAEPROTONOSUPPORT: MidOutMsg="Specified port not supported\n"; break; case WSAEPROTOTYPE: MidOutMsg="Specified port is the wrong type for this socket\n"; break; case WSAESOCKTNOSUPPORT: MidOutMsg="Specified socket type is not supported in this address family\n"; break; default: MidOutMsg="Unrecognized error\n"; break; } } }
The problem that I was having was the bad address was the first ip address and when I tried creating the second ip address, nothing could be created.
Now if the valid address was first, I was able to create it, but not if it was second.
I know I can solve the problem of making sure that the address is valid, but why was the second Create failing if the first Create had a "" for an address


Reply With Quote

Bookmarks