[Newbies] Sending UDP broadcasts

Tobias Pape Das.Linux at gmx.de
Fri Feb 4 13:46:36 UTC 2022


Hi Christian

> On 4. Feb 2022, at 09:56, Christian Kellermann <ckeen at pestilenz.org> wrote:
> 
> Dear List,
> 
> I am trying to send a UDP broadcast on a Linux host with the following code:
> 
> ```
> s := Socket newUDP.
> 
> s setOption:  'SO_BROADCAST' value: 1.
> [[(Delay forSeconds: numberOfSeconds) wait.
> 	 s sendUDPData: shoutout toHost: '255.255.255.255' port: (self portNumber) ]
> repeat] fork.
> ```
> 
> I always get a Primitive Failed error for the sendUDPData message.
> shoutout is a string in this case.
> 
> What am I missing for this example?

The host is not a string but rather an address. Try this:

```
addr :=  NetNameResolver addressForName: '255.255.255.255' timeout: 10.
s := Socket newUDP.

s setOption:  'SO_BROADCAST' value: 1.
[[(Delay forSeconds: numberOfSeconds) wait.
	 s sendUDPData: shoutout toHost: addr port: (self portNumber) ]
repeat] fork.
```

see also the lone sender of #sendUDPData:toHost:port:  and subsequently 
#sendData:toHost:port: in Socket>>timeTestUDP:

```
"..."
	serverAddr := NetNameResolver addressForName: serverName timeout: 10.
	serverAddr = nil 
		ifTrue: [self error: 'Could not find the address for ' , serverName].
	s := self newUDP.	"a 'random' port number will be allocated by the system"
"..."
	s 
		sendData: '!'
		toHost: serverAddr
		port: 13.	"13 is the daytime service"
	Transcript show: 'the time server reports: ' , s receiveData.
```

Best regards
	-Tobias

> 
> Versions used are: 
> 
> Squeak5.3
> latest update: #19470
> unix linux-gnu x86_64
> 
> Open Smalltalk Cog[Spur] VM [CoInterpreterPrimitives VMMaker.oscog-eem.3142]
> Unix built on Feb  4 2022 08:41:04 Compiler: Clang 13.0.0
> platform sources revision VM: 202201220125 
> 
> Kind regards,
> 
> Christian





More information about the Beginners mailing list