[Gambas-user] Sending Udp broadcast?
Ron
ron at ...1740...
Thu Nov 13 09:57:24 CET 2008
Werner schreef:
> Ron wrote:
>
>> Hi,
>>
>> Need some help in how to send a broadcast udp message.
>>
>> Like a wake on lan Magic Packet.
>> I know how to create the message, but how do you sent a broadcast
>> message with udpclient?
>>
>> Came up with this, but it results in a system error.
>>
>> hWOL = NEW UdpSocket AS "WOLSocket"
>> hWOL.Broadcast = TRUE
>> hWOL.Bind(0)
>> IF hWOL.Status = Net.Active THEN
>> PRINT "Active!"
>> WRITE #hWOL, sMsg, Len(sMsg)
>> ELSE
>> PRINT "NotActive!"
>> END IF
>>
>> Thanks.
>>
>> Regards,
>> Ron_2nd.
>>
>>
> '------------- Broadcast class -----------------------------------
> ' Gambas class file
> 'broadcasts a UDP message to all computers (including self)
> 'on the same class C subnet(s)
>
> PUBLIC TXmitter AS UdpSocket
>
> PUBLIC SUB _new()
> TXmitter = NEW UdpSocket AS "TXmitter"
> TXmitter.Broadcast = TRUE
> TXmitter.Bind(0)
> END
>
> PUBLIC SUB Send(thePort AS Integer, OPTIONAL theMessage AS String = " ")
> DIM host AS String
> DIM IPs AS String[]
> DIM IP AS String
> DIM thisIP AS String[]
> TXmitter.TargetPort = thePort
> EXEC ["hostname", "-i"] TO host
> IPs = Split(Trim$(host), " ")
> FOR EACH IP IN IPs
> thisIP = Split(IP, ".")
> IF CInt(thisIP[0]) = 127 THEN CONTINUE
> TXmitter.TargetHost = thisIP[0] & "." & thisIP[1] & "." & thisIP[2]
> & "." & "255"
> WRITE #TXmitter, theMessage, Len(theMessage)
> NEXT
> END
>
> PUBLIC SUB TXmitter_Error()
> PRINT "Error" & Error.Text
> END
>
> PUBLIC SUB Close()
> IF TXmitter.Status > 0 THEN CLOSE #TXmitter
> END
>
>
> '--------------------
> 'using the Broadcast class:
>
> 'declaration section:
> PUBLIC BCast AS NEW Broadcast 'UDP broadcaster
>
> 'in the code:
> BCast.Send(12345, "Hello World!")
>
> '==============================================================
> 'UDP Receiver (same port as transmitter):
>
> PUBLIC netReceiver AS UdpSocket 'for incoming lap change information
>
>
> PUBLIC SUB Form_Open()
> netReceiver = NEW UdpSocket AS "netReceiver"
> netReceiver.Bind(12345)
> END
>
> PUBLIC SUB Form_Close()
> IF netReceiver.Status > 0 THEN CLOSE #netReceiver
> END
>
>
> PUBLIC SUB netReceiver_Read()
> DIM receivedData AS String
> DIM receivedFields AS String[]
> READ #netReceiver, receivedData, Lof(netReceiver)
> 'your code here
> END
>
> '--------------------------
>
> Best Regards
> Werner
>
>
>
Thanks!
I have noticed that Ubuntu 8.04 only returns localhost address on
hostname -i
ron at ...2013...:~$ hostname -i
127.0.1.1
But thats no problem in my case.
Regards,
Ron_2nd.
More information about the User
mailing list