this code sends a so called magic packet to a WOL capable pc on the network :
Code: Select all
; Author: ABBKlaus Dresen
; Date: 10. April 2004
; Modified: 15. November 2007
; PB4.xx compatible
InitNetwork()
Structure mac
mac1.b[6] ; MAC Adress is 6 Bytes long
EndStructure
Structure magic
magic1.b[102] ; Magic Packet 6x$FF 16xMAC = 102 Bytes
EndStructure
; Magig Packet Example :
;
; FFFFFFFFFFFF <- Packet Header
; 000C6EB8CEEB <- 01. MAC Adress to wake up
; 000C6EB8CEEB <- 02. MAC Adress to wake up
; 000C6EB8CEEB <- 03. MAC Adress to wake up
; 000C6EB8CEEB <- 04. MAC Adress to wake up
; 000C6EB8CEEB <- 05. MAC Adress to wake up
; 000C6EB8CEEB <- 06. MAC Adress to wake up
; 000C6EB8CEEB <- 07. MAC Adress to wake up
; 000C6EB8CEEB <- 08. MAC Adress to wake up
; 000C6EB8CEEB <- 09. MAC Adress to wake up
; 000C6EB8CEEB <- 10. MAC Adress to wake up
; 000C6EB8CEEB <- 11. MAC Adress to wake up
; 000C6EB8CEEB <- 12. MAC Adress to wake up
; 000C6EB8CEEB <- 13. MAC Adress to wake up
; 000C6EB8CEEB <- 14. MAC Adress to wake up
; 000C6EB8CEEB <- 15. MAC Adress to wake up
; 000C6EB8CEEB <- 16. MAC Adress to wake up
Procedure.s WOL_Storemac(macadress$,*mac_adress.mac)
Protected pointer1.b=0
Protected pointer2.b=0
Protected value.b=0
Protected tmac.l=0
Protected wert.l=0
Protected a$=""
For i=1 To Len(macadress$)
a$=Mid(macadress$,i,1)
value=0
If a$>="0" And a$=<"9"
wert=Asc(a$)-48
value=1
EndIf
If a$>="a" And a$=<"f"
wert=Asc(a$)-87
value=1
EndIf
If a$>="A" And a$=<"F"
wert=Asc(a$)-55
value=1
EndIf
If value = 1
If pointer1=0
tmac=tmac+wert*16
Else
tmac=tmac+wert
EndIf
pointer1+1
If pointer1>1
*mac_adress\mac1[pointer2]=tmac
pointer1=0
pointer2+1
tmac=0
EndIf
EndIf
Next
EndProcedure
Procedure.l WOL_SendMagicPacket(macadress$,client_ip.s="255.255.255.255")
Protected mac_adress.mac
Protected Success.l=0
Protected client_bip.l
Protected client_port.l
Protected client_bport.w
Protected client_socket.l
Protected socket_keepalive.b
Protected server_mode.l
Protected magic_packet.magic
WOL_Storemac(macadress$,@mac_adress)
For j=0 To 16
debugmac$=""
For i=0 To 5
If j=0
magic_packet.magic\magic1[i+j*6] = 255
Else
magic_packet.magic\magic1[i+j*6] = mac_adress.mac\mac1[i]
EndIf
debugmac$+RSet(Hex(magic_packet.magic\magic1[i+j*6]),3,"00 ")
Next
Debug RSet(Hex(j*6),2,"0")+":"+debugmac$
Next
client_bip = inet_addr_(client_ip)
client_port = 0
client_bport = htons_(client_port) ; convert to long
client_socket = SOCKET_(#AF_INET, #SOCK_DGRAM, #IPPROTO_UDP) ; create the socket
If client_socket
socket_keepalive = 1
If setsockopt_(client_socket, #SOL_SOCKET, #SO_BROADCAST, @socket_keepalive, 1) = 0
client_sockaddr.SOCKADDR_IN
client_sockaddr\sin_family = #AF_INET
client_sockaddr\sin_port = client_bport
client_sockaddr\sin_addr = client_bip
server_mode = 0
If ioctlsocket_(client_socket,#FIONBIO,@server_mode) = 0
send.l = sendto_(client_socket, @magic_packet, SizeOf(magic_packet), 0, @client_sockaddr, 16)
If send=SizeOf(magic_packet) And WSAGetLastError_()=0
Debug "Magic packet send"
Success=1
EndIf
EndIf
EndIf
closesocket_(client_socket)
EndIf
ProcedureReturn Success
EndProcedure
If OpenWindow(0, 245, 73, 296, 110, "Wake on Lan", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
If CreateGadgetList(WindowID(0))
TextGadget(1, 7, 10, 83, 20, "Broadcast")
TextGadget(2, 7, 40, 83, 20, "Target-MAC")
StringGadget(3, 100, 10, 190, 20, "255.255.255.255")
StringGadget(4, 100, 40, 190, 20, "00-00-00-00-00-00")
ButtonGadget(5, 190, 70, 100, 30, "Send")
EndIf
EndIf
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Gadget
If EventGadget() = 5
TargetAddress$ = GetGadgetText(3)
TargetMAC$ = GetGadgetText(4)
If TargetAddress$
WOL_SendMagicPacket(TargetMAC$,TargetAddress$)
Else
WOL_SendMagicPacket(TargetMAC$)
EndIf
EndIf
EndIf
Until Event = #PB_Event_CloseWindow
End