UDP Multicast Client Binding

hey -

i’m running into problems establishing a UDP connection for my client application. all calls made to udp.BindMcast(ipStr, portNum) return the error “can’t bind to port.”

i’m using the following settings:

multicast IP address: 127.0.0.1
portNum: 1267

i double checked to make sure that the firewall is disabled on my mac. What other factors should I be paying attention to? fyi, ofxUDPManager.Create() has also been called.

many thanks!

jeremy

OSX 10.4.10
of_preRelease_v0.05_xcode

I could be totally wrong, but I think you will want to pick an address that’s proper for multicast, as I understand it (from wikipedia)

Multicast: A multicast address is associated with a group of interested receivers. According to RFC 3171, addresses 224.0.0.0 to 239.255.255.255 are designated as multicast addresses. This range was formerly called “Class D.” The sender sends a single datagram (from the sender’s unicast address) to the multicast address, and the routers take care of making copies and sending them to all receivers that have registered their interest in data from that sender.

  • zach

right on. it’s connecting now. i thought i’d need to set the ip address to a local one. thanks for pointing that out.

jeremy

ok, the muticast setup is not working just yet, but i’m making some more headway.
i have both the client and server connecting properly.
fyi, on osx, in order to keep my applications from freezing, i needed to set the timeout for sending and receiving packets to 0.

on the server side, this was done by calling:

udp.SetTimeoutSend(0);

after udp.Create()

and on the client side:

udp.SetTimeoutReceive(0);

unfortunately, the server isn’t sending any packets … i’m just getting lots o’ SOCKET_TIMEOUT errors.

still working away …

jeremy

are you sure the server needs to have a timeout? I’d guess that for multicast, only the client would really need a timeout. The server should just send as much as it can as fast as it can. I can’t imagine a situation in which it could get blocked, but the client easily can get blocked if there is nothing to read, for example.

take care,
zach

fyi - jeremy is working through the mac port of this:

http://www.openframeworks.cc/files/netw-…-xample.rar

which I just uploaded, it’s a pc demo of a non-threaded, completely easy to use mutlicast communication. we’ve recently used it for a project and it’s very flexible – clients can connect and disconnect and run if the server is not running, etc. I recommend it for mutli-computer rendering because you can work from one project, with one h file that defines a struct of data to be passed over… super happy to not use tcp and this system seems very flexible.

  • z

you’re probably right about that one, zach. my server application was freezing on startup, and i thought that was the right way to set the application so that it’s non-blocking.

it turns out that this is the correct way to do this:

int blocking = udp.SetNonBlocking(true);
if(!blocking) printf(“changing blocking state failed\n”);

i’ll post some code soon.

jeremy

hey zach,

i was able to figure out what was keeping udp multicasting from working on a mac.

first of all, there was a timeout loop on the server sending side. based on our conversation, i wasn’t sure that this was necessary, so i commented it out.

  
  
int	ofxUDPManager::Send(const char* pBuff,	const int iSize)  
{  
	if (m_hSocket == INVALID_SOCKET) return(SOCKET_ERROR);  
  
	/*  
  
	if (m_dwTimeoutSend	!= NO_TIMEOUT)  
	{  
		fd_set fd= {1, m_hSocket};  
		timeval	tv=	{NULL, 0};  
		if(select(0, NULL, &fd,	NULL, &tv) == 0)  
		{  
			//printf("SOCKET_TIMEOUT\n");  
			return(SOCKET_TIMEOUT);  
		}  
	}  
	  
	*/  
  
	return (sendto(m_hSocket, (char*)pBuff,	iSize, 0, (sockaddr *)&saClient, sizeof(sockaddr)));  
	//return(send(m_hSocket, pBuff, iSize, 0));  
}  
  

the other issue i was having was that the server could not connect if the client had already connected. diggiding into the code, i discovered that both the client and the server were trying to bind to the same port.

so in, connect multicast, i hardcoded the bind port to 0, which takes the first available port.

  
  
//--------------------------------------------------------------------------------  
bool ofxUDPManager::ConnectMcast(char* pMcast, unsigned short usPort)  
{  
  
	  
	// associate the source socket's address with the socket  
	if (!Bind(0))  
	{  
#ifdef _DEBUG  
		printf("Binding socket failed! Error: %d", WSAGetLastError());  
#endif  
		//printf("failed. failed. failed.\n");  
		  
		return false;  
	}  
  
	// set ttl to default  
	if (!SetTTL(1))  
	{  
#ifdef _DEBUG  
		printf("SetTTL failed. Continue anyway. Error: %d", WSAGetLastError());  
#endif  
	}  
  
	if (!Connect(pMcast, usPort))  
	{  
#ifdef _DEBUG  
		printf("Connecting socket failed! Error: %d", WSAGetLastError ());  
#endif  
		return false;  
	}  
  
	// multicast connect successful  
	return true;  
}  
  

i’m wondering if this will break anything on the pc-side. i’ll give it a shot this afternoon.