Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
UDP broadcast from Windows
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Reed Wilson <cedilla@g...>
Subject: UDP broadcast from Windows
Hi guys!

I'm in the middle of writing a program which does a UDP broadcast, but I 
noticed it doesn't work on Windows. Here's the source:

try (
   let sock = Unix.socket Unix.PF_INET Unix.SOCK_DGRAM 0 in
   Unix.setsockopt sock Unix.SO_BROADCAST true;
   let sent = Unix.sendto sock "ping" 0 4 [] (Unix.ADDR_INET 
((Unix.inet_addr_of_string ""), 12345)) in
   Printf.printf "Sent %d\n" sent;
) with
   | Unix.Unix_error (x,y,z) -> Printf.printf "%S - %s,%s\n" 
(Unix.error_message x) y z

I noticed that when I force the address by replacing:
Unix.inet_addr_of_string ""

Obj.magic "\255\255\255\255"

it _seems_ to work fine. I looked around in the OCaml source, and it 
looks like the broadcast address is specifically disallowed unless 
HAS_INET_ATON is defined? Why is this? And am I breaking anything by 
using magic?