I have two network cards on my machine and I retrieve their info by the following code, mainly their IP addresses:
hostent *host = NULL;
char hostName = "";
host = gethostbyname(hostName);
AfxMessageBox("Error getting local IP address", MB_OK);
//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);
m_UIWIp = tempIP
Later I user this address in setting up some sockets:
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
MidOutMsg,"Address family is not supported\n";
MidOutMsg="No more file descriptors available\n";
MidOutMsg="No buffer space is available\n";
MidOutMsg="Specified port not supported\n";
MidOutMsg="Specified port is the wrong type for this socket\n";
MidOutMsg="Specified socket type is not supported in this address family\n";
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.
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