broadcasting with ofxOsc

I know this has been an issue for others too, but was wondering if anyone found a solution.

I’d like to broadcast OSC Messages to my local subnet, and usually with UDP this happens by sending the message to FF.FF.FF.FF or keeping the subnet mask and setting all the bits (e.g. 192.168.0.FF if subnet is 192.168.0 and mask is FF.FF.FF.0). This also works when sending with OSC - except when using oscPack, and unfortunately ofxOsc is built on oscPack so doesn’t work. Does anyone know what the best solution to this is? (even if it is a hack for now).

yes, you need to do some minimal change and recompile oscpack to enable the socket option to broadcast.

in oscpack/ip/poxis/UdpSocket.cpp change the Implementation constructor to:

  
Implementation()  
  
		: isBound_( false )  
  
		, isConnected_( false )  
  
		, socket_( -1 )  
  
	{  
  
		if( (socket_ = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 ){  
  
            throw std::runtime_error("unable to create udp socket\n");  
  
        }  
  
		int on=1;  
		setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char*)&on, sizeof(on))  
  
		memset( &sendToAddr_, 0, sizeof(sendToAddr_) );  
  
        sendToAddr_.sin_family = AF_INET;  
  
	}  

and recompile, then you just need to use the typical broadcast address 255.255.255.255

Hey Arturo, unfortunately couldn’t get it to work (1st param I think is socket_ instead of socket for it to compile btw.). I’m trying on iPhone so that may have something to do with it. ofxOsc works perfectly on iPhone - (which I was a bit surprised to be honest to be able to use the normal libOsc.a! Do you not need iphone specific libs?) but broadcasting doesn’t. Maybe its not supported on iPhone?

it works with mac version of the lib??¿?

i was googling and it seems it should be supported. anyway the broadcast address i wrote was wrong, it should be your network address xor network mask.

so if you have 192.168.0.1 as ip address and 255.255.255.0 as network mask the broadcast address should be 192.168.0.255

in mac ifconfig should tell you the broadcast address

it works with mac version of the lib??¿?

Yea! pretty rad hey :stuck_out_tongue:

Hello,

Sorry to bring this old thread back, but the fix didn’t work for me as proposed by arturo.

From another post, someone mentionned to set the option after the connection. I tried and I is working. So for me the fix is:

Add these 3 lines

  
  
		//set multicast = true  
		int on=1;  
		setsockopt(socket_, SOL_SOCKET, SO_BROADCAST, &on,sizeof(on));  
  

in file ofxOsciPhone/libs/oscpack/ip/posix/UdpSocket.cpp :

  
  
	void Connect( const IpEndpointName& remoteEndpoint )  
	{  
		SockaddrFromIpEndpointName( connectedAddr_, remoteEndpoint );  
		  
		if (connect(socket_, (struct sockaddr *)&connectedAddr_, sizeof(connectedAddr_)) < 0) {  
			throw std::runtime_error("unable to connect udp socket\n");  
		}  
  
		//set multicast = true  
		int on=1;  
		setsockopt(socket_, SOL_SOCKET, SO_BROADCAST, &on,sizeof(on));  
		  
		isConnected_ = true;  
	}  
  

I tried several values for the broadcast address, but as memo said, it has to be built from the IP and the subnet mask. Did you manage to get this information (by code) ?

One more thing worth to mention is that with the iPhone simulator, the app receives its own broadcasted message, but on iPod Touch it does not.

hi guys :slight_smile:

i’m trying to get broadcast working on linux64.

i’m a bit confused by this discussion though – can i simply recompile oscpack with the changes arturo mentions?

i’ve tried:

  • 255.255.255.255
  • my broadcast address (via ifconfig) of 192.168.8.255
  • the names of individual clients themselves (e.g., 192.168.8.3)

255.255.255.255 doesn’t throw any errors but doesn’t work, and 192.168.8.255 says:

  
  
terminate called after throwing an instance of 'std::runtime_error'  
  what():  unable to connect udp socket  
  

but addressing clients individually works well.

i’m working on port 8888, which i don’t think is an issue.

i just downloaded the source from the svn, and added arturo’s code with memo’s fixes.

on linux64 you also have to manually add “OSC_HOST_LITTLE_ENDIAN” to the #define section in the build settings.

with that in place i was able to use my broadcast address, which i got using ifconfig (192.168.8.255).

to get this address programmatically, there is probably something smart you could do with API-ish things against linux… but in the worst case you could always parse the result of calling ifconfig :slight_smile:

just found another feature/bug of oscpack that i had to mod.

if you want multiple apps on the same host listening to the same UDP port, you need an extra socket option. add these two lines:

  
  
int reuse = 1;  
setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof reuse);  
  

in the same location arturo mentions (in the middle of the in oscpack/ip/poxis/UdpSocket.cpp Implementation constructor).

Oh wow, I didn’t even know this was an option, this is great!

Yeah, super helpful for what I’m doing at the moment.

I’m talking to the oscpack author about adding these two options (broadcast, address reuse) into oscpack as options that can be set. Hopefully they’ll be incorporated.

I can get the broadcasting/multicasting to work using the changes above. But as soon as more than one client application on the same local host tries to “listen in”, it gets rejected with a “unable to bind udp socket” error.

Kyle, Memo, what’s the current status of this in 007? Did those options get incorporated into oscpack, and are they in ofxOsc in 007? It seems at least address reuse is not implemented, that doesn’t work with stock ofxOsc for me. Is this endianness-fix for 64bit still necessary in 007?

It doesn’t appear this got implemented. Can someone post a compiled lib (personally I’m looking for ofxiPhone compliation)? Trying to send to 255.255.255.255 using an iOS device isn’t working.

no it’s not implemented yet, you can fix oscpack and recompile it though

Thanks Arturo, but I couldn’t figure out how to recompile it. I changed the things it said in the makefile for OSX and ran ‘make’ and ‘make lib’ but it doesn’t create the .a. I don’t use make enough to know what I’m doing heh.

edit: I wasn’t able to compile it as a seperate lib .a, but I was able to compile it as part of the project I’m using instead by including the source. So, that works for now. It still doesn’t work with 255.255.255.255 though (anyone have that working?), but it does work with 192.168.1.255 which it didn’t before.

255.255.255.255 is not always the broadcast address. check with ifconfig to see the broadcast address.

yes actually 255.255.255.255 is not the broadcast address. you can get the broadcast address by doing an binary or of the ip address and the negated subnet mask. so if your address is 192.168.1.xxx and your subnet mask 255.255.255.0:

192.168.1.xxx | 0.0.0.255 = 192.168.1.255

255.255.255.255 is the broadcast address for the local network and though it should work it doesn’t for some systems like the iphone

Ah ok thanks. I guess the problem is, what if you want devices to connect to each other without having to manually input an IP. Is there a way for them to auto discover the broadcast IP? Does oscpack have a getIP function? I couldn’t find it.

that’s called bonjour and/or avahi.

there’s also this thing called libmapper, which adds another layer to OSC, so to say, to add things like autodiscover, mapping of messages, etc.
I think it could be worthwhile to get this over to oF, there’s even a guy who began ofxlibmapper, but i don’t have the time and skills for that. I want(ed) to build something like osculator, just crossplatform and opensource, with it.
https://github.com/radarsat1/libmapper