UDP broadcast

Mac OSX specific forum
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

UDP broadcast

Post by jesperbrannmark »

Hi.
I tried yesterday doing this
http://www.purebasic.fr/english/viewtop ... st#p365942
It does two things:
1. Set up a UDP server and monitor incomming connections
2. Set up a UDP client and send stuff to broadcast address (ie like 192.168.0.255 so it gets distributed to all computers in that network range)
So what you send should be received on all computers in the netwrok.

From the PC side this works great.
On the MAC side it works great to receive, but to send UDP to broadcast address doesnt work. Is there a trick to this?
Here is a small sample program:

Code: Select all

InitNetwork()
OpenWindow(0,0,0,640,480,"")
StringGadget(0,0,0,320,50,"TEXT")
ButtonGadget(1,350,0,60,20,"SEND")
CreateNetworkServer(0, 5556, #PB_Network_UDP)
*DataBuffer = AllocateMemory(2048)
*ipbuf = AllocateMemory(1000)
ExamineIPAddresses() 
Repeat
  Result = NextIPAddress()
  If Not result
    Break
  EndIf
  Debug  IPString(result)
  String$ + IPString(result)+";"
ForEver
PokeS(*ipbuf,string$,Len(string$))
Repeat
  event=WindowEvent()
  If event=#PB_Event_CloseWindow
    End
  ElseIf event=#PB_Event_Gadget And EventGadget()=1
    For ss=1 To CountString(string$,";")
      resultt.s=StringField(string$,ss,";")
      myip$ =StringField(resultt.s,1,".")+"."+StringField(resultt.s,2,".")+"."+StringField(resultt.s,3,".")+".255"    
      Connection = OpenNetworkConnection(myip$, 5556, #PB_Network_UDP) ;chjange to my IP broadcast
      Debug "DOCONNECTION "+myip$
      If Connection   
        *DataBuffer2 = AllocateMemory(2048)
        PokeS(*DataBuffer2, GetGadgetText(0))
        SendNetworkData(Connection, *DataBuffer2, Len(GetGadgetText(0)))
        CloseNetworkConnection(Connection)
        FreeMemory(*DataBuffer2)
      EndIf
    Next    
  EndIf
  If NetworkServerEvent()
    Client = EventClient()
    No = ReceiveNetworkData(Client, *DataBuffer, 2048)
    If No
      call1.s=FormatDate("%yyyy-%mm-%dd %hh:%ii",Date())
      Debug PeekS(*DataBuffer)
      SendNetworkData(Client, *ipbuf, Len(String$))
    EndIf
    CloseNetworkConnection(Client)
  EndIf
  Delay(50)  
ForEver
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

API call for UDP send?

Post by jesperbrannmark »

Does anyone have a alternative API call for send UDP message (sendnetworkdata similar) ?
I want to do like this

SendUDP("Message","192.168.0.255")

But on Mac OS X, nothing ever gets sent if its not a valid IP address.
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: UDP broadcast

Post by fsw »

If you look here:
https://developer.apple.com/library/mac ... ayers.html

It says:
...
By contrast, UDP supports three things that TCP does not:

Broadcast messages in IPv4—packets sent to a broadcast address are received by every host within its broadcast domain (which is usually its subnet).
...
And here:
https://developer.apple.com/library/mac ... ivery.html

it talks about:
...
The broadcast address is an address in which the host part is all ones. If you send a packet to this address, it is received by every host within the same broadcast domain (usually the subnet), and it is never routed beyond the local area network.
...
Looking at the provided IPv4 table the host part are the last 3 chunks.
So instead of doing 192.168.0.255 you could try 192.255.255.255.

Because I'm not an expert in this I could be totally wrong though...
...at least I tried :mrgreen:

I am to provide the public with beneficial shocks.
Alfred Hitshock
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Re: UDP broadcast

Post by Niffo »

Hello,

Is it confirmed that UDP Broadcast does not work in PureBasic on MacOS ? (seems to work here on Windows but not on Snow Leopard)
Niffo
Ocean
New User
New User
Posts: 6
Joined: Wed Mar 19, 2014 10:39 am
Location: Europe
Contact:

Re: UDP broadcast

Post by Ocean »

fsw wrote:If you look here:
https://developer.apple.com/library/mac ... ayers.html

It says:
...
By contrast, UDP supports three things that TCP does not:

Broadcast messages in IPv4—packets sent to a broadcast address are received by every host within its broadcast domain (which is usually its subnet).
...
And here:
https://developer.apple.com/library/mac ... ivery.html

it talks about:
...
The broadcast address is an address in which the host part is all ones. If you send a packet to this address, it is received by every host within the same broadcast domain (usually the subnet), and it is never routed beyond the local area network.
...
Looking at the provided IPv4 table the host part are the last 3 chunks.
So instead of doing 192.168.0.255 you could try 192.255.255.255.

Because I'm not an expert in this I could be totally wrong though...
...at least I tried :mrgreen:
That last pointer is partly correct only. The 'host part' of an IP4 address is defined by the subnet mask of the subnet. If the subnet in question is a Class-C network the 192.168.0.255 target address would indeed be correct.

This question is of interest of me, will do some testing early next week.

cheers
Ocean
Post Reply