Results 1 to 16 of 16

Thread: Send a wake up packet to UDP port 9 [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    Resolved Send a wake up packet to UDP port 9 [RESOLVED]

    My PC's ethernet card supports wake on LAN, and I want to write a small VB app to wake up my PC remotely so I can connect to the VNC server I have running on it. I don't like to leave my computer on all the time.

    How would I go about doing this? DSLReports.com has a web based tool that will do it, but I want it coded into my program for simplicity.

    I should mention that the PC is not within the same subnet and so I need to send the code over the Internet. I found some really short code that will send the wake up over a subnet using Winsock, and if I can adapt the Winsock control to send it to a specific IP address, that would be brilliant.


    VB Code:
    1. 'in a form with a command button and textbox
    2. Private Sub Command1_Click()
    3. Dim data As String
    4. data = "FFFFFFFFFFFF"
    5. For i = 1 To 16
    6. data = data & Text1.Text
    7. Next i
    8. data = data + hex2ascii(data)
    9. Winsock1.SendData (data)
    10. End Sub
    11.  
    12. Public Function hex2ascii(ByVal hextext As String) As String
    13. For Y = 1 To Len(hextext)
    14.     num = Mid(hextext, Y, 2)
    15.     Value = Value & Chr(Val("&h" & num))
    16.     Y = Y + 1
    17. Next Y
    18.  
    19. hex2ascii = Value
    20. End Function
    Last edited by Chris H; Apr 24th, 2005 at 05:40 PM.
    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    I figured it out after a bit of detective work using the code above I just had to change the Windosck control's properties. Winsock seems to work quite welll for this.
    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    Post the code. I think my server (W2K) will wake up workstations, and all new OS's will support it. I was a M$ demo that used the WOL feature to image a server using W2K3. It looked cool.
    Having the code available would be good for a few rep points, I'd imagine, if nothing else.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    I thought the code above worked but it doesn't seem to be sending the MAC address properly. I think the first set of bytes in the packet should be the MAC address but instead I get 00 0F DB 25 81 76.

    I don't know enough about winsock to correct this.
    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    Okay, I finally got it working by altering the code slightly.

    I'll post it once I refine it a bit.
    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    Here's the SLOPPY code, that works, but I haven't cleaned up and probably won't for fear of breaking it

    VB Code:
    1. Private Const IP_SUCCESS As Long = 0
    2. Private Const MAX_WSADescription As Long = 256
    3. Private Const MAX_WSASYSStatus As Long = 128
    4. Private Const WS_VERSION_REQD As Long = &H101
    5. Private Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD \ &H100 And &HFF&
    6. Private Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF&
    7. Private Const MIN_SOCKETS_REQD As Long = 1
    8. Private Const SOCKET_ERROR As Long = -1
    9. Private Const ERROR_SUCCESS As Long = 0
    10.  
    11. Private Type WSADATA
    12.    wVersion As Integer
    13.    wHighVersion As Integer
    14.    szDescription(0 To MAX_WSADescription) As Byte
    15.    szSystemStatus(0 To MAX_WSASYSStatus) As Byte
    16.    wMaxSockets As Long
    17.    wMaxUDPDG As Long
    18.    dwVendorInfo As Long
    19. End Type
    20.  
    21. Private Declare Function gethostbyname Lib "wsock32.dll" _
    22.   (ByVal hostname As String) As Long
    23.  
    24. Private Declare Sub CopyMemory Lib "kernel32" _
    25.    Alias "RtlMoveMemory" _
    26.   (xDest As Any, _
    27.    xSource As Any, _
    28.    ByVal nbytes As Long)
    29.  
    30. Private Declare Function lstrlenA Lib "kernel32" _
    31.   (lpString As Any) As Long
    32.  
    33. Private Declare Function WSAStartup Lib "wsock32.dll" _
    34.    (ByVal wVersionRequired As Long, _
    35.     lpWSADATA As WSADATA) As Long
    36.    
    37. Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
    38.  
    39. Private Declare Function inet_ntoa Lib "wsock32.dll" _
    40.   (ByVal addr As Long) As Long
    41.  
    42. Private Declare Function lstrcpyA Lib "kernel32" _
    43.   (ByVal RetVal As String, _
    44.    ByVal Ptr As Long) As Long
    45.                        
    46. Private Declare Function gethostname Lib "wsock32.dll" _
    47.    (ByVal szHost As String, _
    48.     ByVal dwHostLen As Long) As Long
    49. Private Sub Command1_Click()
    50. Command1.Caption = "Resolving"
    51. Command1.Enabled = False
    52.  
    53. Dim sHostName As String
    54.  
    55. If SocketsInitialize() Then
    56.       Dim mac As String
    57.             Dim IPAddress As String
    58.            
    59.            
    60.             mac = Replace(txtMAC.Text, ":", "")
    61.             mac = Replace(mac, "-", "")
    62.             mac = Replace(mac, " ", "")
    63.      
    64.            
    65.             Dim data As String
    66.             data = "FFFFFFFFFFFF"
    67.             For i = 1 To 16
    68.             data = data & mac
    69.             Next i
    70.            
    71.             data = hex2ascii(data)
    72.             WinsockUDP.RemotePort = Val(txtPort)
    73.             WinsockUDP.RemoteHost = GetIPFromHostName(txtIP.Text)
    74.            
    75.             WinsockUDP.LocalPort = Val(txtlocalport)
    76.             SaveSetting "WakeMeUp", "defaults", "IPAddress", GetIPFromHostName(txtIP.Text)
    77.             SaveSetting "WakeMeUp", "defaults", "MACAddress", mac
    78.             SaveSetting "WakeMeUp", "defaults", "RemotePort", txtPort
    79.             SaveSetting "WakeMeUp", "defaults", "LocalPort", txtlocalport
    80.             SaveSetting "WakeMeUp", "defaults", "Packets2Send", cmbQTY.ListIndex
    81.             For x = 0 To cmbQTY.ListIndex
    82.                 If Len(data) > 15 Then
    83.                     WinsockUDP.SendData (data)
    84.                     WinsockUDP.Close
    85.                 Else
    86.                 MsgBox "Could not resolve host name."
    87.                 End If
    88.             Next x
    89.             MsgBox cmbQTY.ListIndex + 1 & " packet(s) sent to " & GetIPFromHostName(txtIP.Text)
    90.                  
    91. Else
    92.                     MsgBox "Could not resolve host name."
    93. End If
    94.  
    95. Command1.Caption = "Send Packet(s)"
    96. Command1.Enabled = True
    97.  
    98.  
    99. End Sub
    100.  
    101. Public Function hex2ascii(ByVal hextext As String) As String
    102. For Y = 1 To Len(hextext)
    103.     num = Mid(hextext, Y, 2)
    104.     Value = Value & Chr(Val("&h" & num))
    105.     Y = Y + 1
    106. Next Y
    107.  
    108. hex2ascii = Value
    109. End Function
    110.  
    111. Private Sub Form_Load()
    112. txtIP = GetSetting("WakeMeUp", "defaults", "IPAddress", "")
    113. txtMAC = GetSetting("WakeMeUp", "defaults", "MACAddress", "FFFFFFFFFFFF")
    114. txtPort = GetSetting("WakeMeUp", "defaults", "RemotePort", 9)
    115. txtlocalport = GetSetting("WakeMeUp", "defaults", "LocalPort", "3340")
    116. tmp = GetSetting("WakeMeUp", "defaults", "Packets2Send", "2")
    117. If Val(tmp) < 0 Or tmp > 19 Then
    118.     tmp = 2
    119. End If
    120. cmbQTY.ListIndex = tmp
    121.  
    122. End Sub
    123. Private Function SocketsInitialize() As Boolean
    124.  
    125.    Dim WSAD As WSADATA
    126.    Dim success As Long
    127.  
    128.    SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS
    129.    
    130. End Function
    131.  
    132. Private Function GetIPFromHostName(ByVal sHostName As String) As String
    133.  
    134.   'converts a host name to an IP address
    135.  
    136.    Dim nbytes As Long
    137.    Dim ptrHosent As Long  'address of HOSENT structure
    138.    Dim ptrName As Long    'address of name pointer
    139.    Dim ptrAddress As Long 'address of address pointer
    140.    Dim ptrIPAddress As Long
    141.    Dim ptrIPAddress2 As Long
    142.  
    143.    ptrHosent = gethostbyname(sHostName & vbNullChar)
    144.  
    145.    If ptrHosent <> 0 Then
    146.  
    147.      'assign pointer addresses and offset
    148.  
    149.      'Null-terminated list of addresses for the host.
    150.      'The Address is offset 12 bytes from the start of
    151.      'the HOSENT structure. Note: Here we are retrieving
    152.      'only the first address returned. To return more than
    153.      'one, define sAddress as a string array and loop through
    154.      'the 4-byte ptrIPAddress members returned. The last
    155.      'item is a terminating null. All addresses are returned
    156.      'in network byte order.
    157.       ptrAddress = ptrHosent + 12
    158.      
    159.      'get the IP address
    160.       CopyMemory ptrAddress, ByVal ptrAddress, 4
    161.       CopyMemory ptrIPAddress, ByVal ptrAddress, 4
    162.       CopyMemory ptrIPAddress2, ByVal ptrIPAddress, 4
    163.  
    164.       GetIPFromHostName = GetInetStrFromPtr(ptrIPAddress2)
    165.  
    166.    End If
    167.  
    168. End Function
    169.  
    170.  
    171. Private Function GetStrFromPtrA(ByVal lpszA As Long) As String
    172.  
    173.    GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
    174.    Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)
    175.  
    176. End Function
    177.  
    178.  
    179. Private Function GetInetStrFromPtr(Address As Long) As String
    180.  
    181.    GetInetStrFromPtr = GetStrFromPtrA(inet_ntoa(Address))
    182.  
    183. End Function
    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    You get the info for the settings, but where did you set them from?

    Do you know the IP address and the MAC address of the computer to wake?

    You send the packet over the Internet? I assume that you have a port that gets forwared thru a router?

    I don't have a network set up or I'd try it. Maybe when I get to my buddies, I'll try it out.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    The settings come from the registry if there are any that were saved from the previous use. It saves them automatically. I did that since this program is really just for me to use and I only wake up one computer. This way it remembers the MAC address, and location and port.

    It resolves the IP address from a no-ip.org address. The MAC address is a known value. The packet is a UDP packet on port 9, but you can change the port. If you have a firewall or router that does not have IP pass through enabled you would need to forward the port. to the specific PC you want to wake up.

    You could wake up mulitple PCs behind a router by doing some fancy port forwarding on the router.
    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    Thanks. I'll let you know when I test it out. It should be within a few weeks.

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    What changes do I have to make to the Winsock? And what is cmbQTY?
    I just filled a listbox with the numbers 0 to 5.

    Now, I get an error on this line:

    VB Code:
    1. WinsockUDP.SendData (data)

    Runtime: 40006
    Wrong protocol or connection state for the requested transaction or request.

    Should I post the project for you to take a look at?

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    Quote Originally Posted by dglienna
    What changes do I have to make to the Winsock? And what is cmbQTY?
    I just filled a listbox with the numbers 0 to 5.

    Now, I get an error on this line:

    VB Code:
    1. WinsockUDP.SendData (data)

    Runtime: 40006
    Wrong protocol or connection state for the requested transaction or request.

    Should I post the project for you to take a look at?
    cmbQty is a drop down combobox that lets you choose how many wake up packets to send. It has to be at least 1.
    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    It's set to 2. It is trying to send a very long binary field. Would you mind sending your project, or would you like me to post what I have?

    Thanks again!

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

  14. #14
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    Thanks. I'll try it when I get back home tonight.

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    Hmmm. It seems to be the same code, but it didn't work, even after I turned WOL on in BIOS. I am sending to I was wondering how the program new the IP address of the machine after it had been turned off, so I tried using the broadcast address, and IT WORKED!

    I guess the IP address has to be static to turn on an individual machine?

    I have DHCP turned on thru my router.

    Thanks, again!

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    Re: Send a wake up packet to UDP port 9 [RESOLVED]

    Quote Originally Posted by dglienna
    Hmmm. It seems to be the same code, but it didn't work, even after I turned WOL on in BIOS. I am sending to I was wondering how the program new the IP address of the machine after it had been turned off, so I tried using the broadcast address, and IT WORKED!

    I guess the IP address has to be static to turn on an individual machine?

    I have DHCP turned on thru my router.

    Thanks, again!
    Yep, static IP or broadcast packet. I've noticed some routers (such as my POS Westell from Verizon) will forget the IP of the PC if you shut it down before it boots up an initializes its network connections, even when you have a static IP set.

    If you setup a no-ip.og address you can send the packet to that instead of the ip address as the code will resolve the domain name.
    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width