Problem when Creating AsyncSocket
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Problem when Creating AsyncSocket

  1. #1
    Join Date
    May 1999
    Posts
    117

    Problem when Creating AsyncSocket

    I have two network cards on my machine and I retrieve their info by the following code, mainly their IP addresses:

    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
    	}
    }
    Later I user this address in setting up some sockets:
    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 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

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Problem when Creating AsyncSocket

    [ Moved thread ]
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center