COMMUNITY

UdpSocket: can't send to broadcast addresses


(Alan) #1

Hello everyone,

I’m trying to learn haxe programming by implementing some toy example projects to evaluate its usage in other multi-platform projects I’m currently working on.

One of these toy projects involves sending an UDP packet to a broadcast address on a specific port. For context, this is done to perform an autodetection of other hosts/services on the local net that are listening on that given UDP port.
When I tried using the sys.net.UdpSocket#sendTo() method to send the packet bytes, I noticed a number of issues:

  • --interp mode always errors out – this is explained in issue #5501 and I guess I can live without it by compiling to the neko platform;
  • although the java platform is listed in the “available” platforms in the class docs, the generated java code throws a “Not available on this platform” HaxeException as soon as the class constructor is called – is this a documentation error or did I understand wrong what “availability for platform X” means?
  • sending to “normal” addresses works (verified using Wireshark), but when I specify a broadcast address destination (be it 255.255.255.255 or a more specific one, like 192.168.0.255), the UdpSocket#sendTo() function always throws an Exception (see below). I guess the underlying socket needs a flag to be set for correctly broadcasting packets to the destination (like what is done in C via setsockopt):
Called from sys.net.UdpSocket::$statics line 1
Called from Main::main line 6
Called from Example1::find line 30
Called from sys.net.UdpSocket::sendTo line 40
Uncaught exception - Custom(std@socket_send_to)
  • when using the native binary produced by hxcpp, the Exception becomes Error : Custom(EOF). Is this normal?

All the tests were done on a Linux x86_64 machine using haxe-3.4.7, hxcpp-3.4.188, hxjava-3.2.0.


(Alan) #2

Well, by fooling around a little in haxe, neko and hxcpp code bases I guess I implemented a seemingly working Socket.setBroadcast(b: Bool) method, that I already successfully tested with my UdpSocket-based toy project both with the neko and hxcpp targets on my Linux system.

Code is here:

@ncannasse @RealyUniqueName @mark.knol : sorry to bug everyone, but I don’t know who should be notified in this case…
If deemed appropriate, I’ll gladly submit issues + PRs to the relevant projects.


(Aleksandr Kuzmenko) #3

I think it’s worth creating those PRs so that responsible people won’t miss your work.


(Alan) #4

Thanks Aleksandr, I’ve just done that. I created both issues and PRs that’ll automatically close them when merged:


(Alan) #5

PRs merged! :smile:


(Jeff Ward) #6

Cool! I had a similar hackjob experiment: https://github.com/jcward/hx-multicast – specifically I inject / monkey patch hxcpp here: https://github.com/jcward/hx-multicast/blob/master/src/multicast/impl/Bind_HXCPP.hx

I’ll have to give the patched versions a try.