Broadcast Sockets


It has been a very long time since I have tried using Juce… so please forgive me if I missed something simple.

I need to send a broadcast packet for a client/server application. I have written standard C code using WinSock2 and it works fine. I’m now trying to port this into a JUCE application and I am running into an issue.

Specifically, I need to do two things that I don’t see a way to do:

  1. I need to determine the local IP address, so I can build the proper sub-net address for the broadcast packet. In my C code I do this with gethostbyname(). This appears to not be available to DatagramSocket().

  2. I need to call sendto() to a DatagramSocket (unconnected) and I need to specify the broadcast server address with a sub-net mask of “” (otherwise the router will drop the packet). I have looked through juce_Socket.cpp and I cannot see a way to do this.

I see that DatagramSocket() can have a parameter “allowBroadcast” set to true, but I don’t see that this does anything other than call setSockOpt( …SO_BROADCAST… ), which for Windows at least isn’t really needed.

Since for both OSX and Windows (haven’t looked at iOS or Android yet), most of the socket calls are already cross platform, could I just get the private handle out of DatagramSocket() and use my already working C code?

Thanks for your help,

David A. Hoatson

Last year I posted some socket extensions, which included a get IP address(es) implemented for all platforms. You should be able to find them with a search. Since then I just started maintaining my own socket classes and haven’t really looked at the changes Jule’s has made since then.

Sockets is actually one of the easier things to maintain multi-platform. It is really just the includes and some Microsoft name weirdnesses.

OK, Thanks for the reply. I’ll search and see what I find!