Click here to Skip to main content
15,885,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is wrong with following code:
C++
IGDDiscoverProcess::IGDDiscoverResult IGDDiscoverProcess::InitializeUDPConnection( void )
{
	int error(0);

	pimpl->sock = socket( 
		AF_INET, SOCK_DGRAM, 0
		);
	if( INVALID_SOCKET == pimpl->sock )
	{
		BHDebug( 
			false
			, L"socket had returned a invalid socket." );
		return InvalidSocket;
	}

	sockaddr_in addr = {0};
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = htonl(INADDR_ANY);
	u_short port = 20000;
	while( true )
	{
		addr.sin_port = htons( port );
		if( 
			::bind( 
			pimpl->sock, (sockaddr*)&addr, sizeof(addr) ) 
			)
		{
			break;
		}

		error = WSAGetLastError();
		if( WSAEADDRINUSE == error )
		{
			port++;
		}
		else if( 0 != error )
		{
			closesocket( pimpl->sock );
			pimpl->sock = INVALID_SOCKET;
			return Failed;
		}
	}

// ttl/non-blocking setting for m-search
	unsigned int ttl = 4;
	unsigned long flags = 1;
	error = setsockopt(
		pimpl->sock
		, IPPROTO_IP, IP_MULTICAST_IF
		, (char*)(&ttl), sizeof(ttl)
		);
	BHDebug( 0 == error, L"setsockopt error." );
	wchar_t local_buffer[20];
	_itow_s(WSAGetLastError(),local_buffer,20,10);
	BHDebug( 0 == error, local_buffer);


Network error (WSAGetLastError) is "10049" after executing this.

Error is here:
C++
error = setsockopt(
		pimpl->sock
		, IPPROTO_IP, IP_MULTICAST_IF
		, (char*)(&ttl), sizeof(ttl)
		);


What is wrong with this function call?

UPDATE:
This is a part of code from UPnP article from www.codeproject.com .

I have not found any description for setsockopt function usage with option IPPROTO_IP.
Have you know or found any?
Posted
Updated 15-Jan-16 1:39am
v5
Comments
[no name] 15-Jan-16 2:24am    
Did you check what the error code means?
https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx

Cannot assign requested address.
The requested address is not valid in its context. This normally results from an attempt to bind to an address that is not valid for the local computer.
Jochen Arndt 15-Jan-16 3:46am    
You are not specifying a protocol for your socket (when passing zero it is choosen by the service provider). I don't know if this is the reason, but you might try to use IPPROTO_UDP.

Maybe this link is also helpful: http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedmulticast9a.html
Сергей Козлов 15-Jan-16 7:34am    
Changing to this does not help:

pimpl->sock = socket(
AF_INET, SOCK_DGRAM, IPPROTO_UDP//0
);
Same error in the same place.
Jochen Arndt 15-Jan-16 7:48am    
It was just a try.

But I think I found the error and posted a solution.

The error is here:
while( true )
{
    addr.sin_port = htons( port );
    if(
        ::bind(
        pimpl->sock, (sockaddr*)&addr, sizeof(addr) )
        )
    {
        break;
    }
    // ...
}

Your code will loop until bind() fails (returns non-zero) because upon success the following code lines do nothing (error is zero).
So it should be:
if (0 == ::bind(pimpl->sock, (sockaddr*)&addr, sizeof(addr)) 
    break;
 
Share this answer
 
Comments
Сергей Козлов 15-Jan-16 7:59am    
While looking for solution of one error, found 2 errors.
Jochen Arndt 15-Jan-16 8:03am    
Indeed. I just saw your solution.

If the errors are also on the page from where you got the code, you should post a comment there (maybe with a link to here).
Сергей Козлов 15-Jan-16 8:08am    
In that article on www.codeproject.com are a lot of errors, because it is back to 2004 year.
Сергей Козлов 15-Jan-16 8:12am    
Here is the source article:
http://www.codeproject.com/Articles/35853/Portmappings-on-UPnP-NAT-s-using-C-winsock-xerces
Jochen Arndt 15-Jan-16 8:15am    
Thank you for the link. I just searched for it but had no success (maybe because that is from 2009).
Found the error.
Here should be IP_MULTICAST_TTL option:
C++
// ttl/non-blocking setting for m-search
	unsigned int ttl = 4;
	unsigned long flags = 1;
	error = setsockopt(
		pimpl->sock
		, IPPROTO_IP, IP_MULTICAST_TTL//IP_MULTICAST_IF
		, (char*)(&ttl), sizeof(ttl)
		);


as written here IP Multicasts[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900