Results 1 to 6 of 6

Thread: [RESOLVED] Winsock problem? Not sure -- Shuttingdown Multiple PCs on Network

  1. #1

    Thread Starter
    Member
    Join Date
    May 2008
    Location
    Moorhead, MN
    Posts
    37

    Question [RESOLVED] Winsock problem? Not sure -- Shuttingdown Multiple PCs on Network

    I've included my source. Basically I am looking for help. I believe that the problem is in the Reciever portion (server that recieves the command to shutdown) I have the shutdown disabled in the code and it is just supposed to display a msgbox saying that the shutdown happned but I'm not getting any result and I included the whole thing since I cant get the reciever portion to work properly I don't know if something in the way it is sending it out is correct either if you don't want to download the source I have included the code here:

    So to recap jus one more time... I am not getting any response when the command is sent it doesnt even look like its trying to send it to me. Any help is greatly appreciated this is for a medium size network of approximately 50 computers and the IP range will be edited before it is sent out....

    Thanks again in advance!

    Reciever Code:
    1. Private Sub Form_Load()
    2. '    Socket1.Listen
    3. '    frmSys32Control.Hide
    4. End Sub
    5.  
    6. Private Sub Socket1_ConnectionRequest(ByVal requestID As Long)
    7.     Socket1.Accept requestID
    8. End Sub
    9.  
    10. Private Sub Socket1_DataArrival(ByVal bytesTotal As Long)
    11.     'sckServer(Index).GetData strData, vbString, bytesTotal
    12.     Dim strData As String
    13.     Socket1.GetData strData, vbString, bytesTotal
    14.    
    15.     If strData = "shutdown" Then
    16.         MsgBox "shutdown"
    17.         'shell "shutdown -s -t 10"
    18.     ElseIf strData = "restart" Then
    19.         MsgBox "restart"
    20.         'shell "shutdown -r -t 10"
    21.     Else
    22.         'do nothing stupid
    23.     End If
    24.    
    25. End Sub

    Sender Code:
    1. Option Explicit
    2. Dim varA As String
    3. Dim strCommand As String
    4. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    5. Dim Result As String
    6.  
    7. Public Sub Wait(HalfSeconds As Single)
    8.  Dim lMilliSeconds As Long
    9.  lMilliSeconds = HalfSeconds * 500
    10.  Sleep lMilliSeconds
    11. End Sub
    12.  
    13. Public Sub CmdSend1()
    14.     Dim strHostIP As String
    15.     Dim strHostPort As String
    16.     strHostIP = "0.0.0.0"
    17.     strHostPort = "0"
    18.     If varA >= 256 Then
    19.         Exit Sub
    20.     ElseIf varA < 256 Then
    21.         strHostPort = 4955
    22.         sckSend.RemotePort = strHostPort
    23.         strHostIP = "192.168.0." & varA
    24.         varA = varA + 1
    25.         sckSend.RemoteHost = strHostIP
    26.         sckSend.SendData strCommand
    27.         Call Wait(1)
    28.         Call CmdSend2
    29.     Else
    30.         MsgBox "Talk to Josh if the problem persists.", vbCritical
    31.     End If
    32. End Sub
    33.  
    34. Public Sub CmdSend2()
    35.     Dim strHostIP As String
    36.     Dim strHostPort As String
    37.  '   Dim Result As String
    38.     strHostIP = "0.0.0.0"
    39.     strHostPort = "0"
    40.     If varA >= 256 Then
    41.         Exit Sub
    42.     ElseIf varA < 256 Then
    43.         strHostPort = 4955
    44.         sckSend.RemotePort = strHostPort
    45.         strHostIP = "192.168.0." & varA
    46.         varA = varA + 1
    47.         sckSend.RemoteHost = strHostIP
    48.         sckSend.SendData strCommand
    49.         Call Wait(1)
    50.         Call CmdSend1
    51.     Else
    52.         MsgBox "Talk to Josh if the problem persists.", vbCritical
    53.     End If
    54.     If Result = 1 Then
    55.         'do nothing because the question has already been answered
    56.     Else
    57.         If strCommand = "restart" Then
    58.             Result = MsgBox("Restart is now complete. Exit the program if you wish.", vbOKOnly)
    59.             If Result = 1 Then
    60.                 Exit Sub
    61.             Else
    62.                 MsgBox "***? how did you manage that?!??!!"
    63.             End If
    64.         ElseIf strCommand = "shutdown" Then
    65.             Result = MsgBox("Shutdown is now complete. Exit the program if you wish.", vbOKOnly)
    66.             If Result = 1 Then
    67.                 Exit Sub
    68.             Else
    69.                 MsgBox "***? how did you manage that?!??!!"
    70.             End If
    71.         End If
    72.     End If
    73. End Sub
    74.  
    75. Private Sub cmdRestart_Click()
    76.     Dim Result As String
    77.     Result = MsgBox("Are you sure you want to Restart all PC's?", vbYesNo)
    78.     If Result = 6 Then
    79.         MsgBox "Do not press anything for approxamitely one or two minutes. Restart in progress", vbCritical
    80.         varA = 0
    81.         strCommand = "restart"
    82.         Call CmdSend1
    83.     Else
    84.         'do nothing
    85.     End If
    86. End Sub
    87.  
    88. Private Sub cmdShutdown_Click()
    89.     Dim Result As String
    90.     Result = MsgBox("Are you sure you want to Shutdown all PC's?", vbYesNo)
    91.     If Result = 6 Then
    92.         MsgBox "Do not press anything for approxamitely one or two minutes. Shutdown in progress", vbCritical
    93.         varA = 250
    94.         strCommand = "shutdown"
    95.         Call CmdSend1
    96.     Else
    97.         'do nothing
    98.     End If
    99. End Sub
    100.  
    101. Private Sub Form_Load()
    102.     Result = 0
    103. End Sub

    Just in case anyone wonders stupid in my code refers to me so don't be offended its my way of telling myself something
    Attached Files Attached Files
    Last edited by Genovah; May 4th, 2008 at 04:53 PM. Reason: better description
    -- Josh Smith
    C#, VB, PHP, SQL

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: [Not Resolved] Winsock problem? Not sure -- Shuttingdown Multiple PCs on Network

    I see a few problems.

    1. You're using the UDP protocol so the ConnectionRequest() event won't fire (UDP is a "connectionless" protocol).

    2. On client (Shutdown.vbp) you are specifying the RemoteHost as "0.0.0.0". That should be the IP of the computer you are shutting down. If you are shutting down all computers on the network I would enumerate all computers on the network and loop through them (I can help with this if you want).

    3. On client again, you are specifying RemotePort as "0". Make this a different number (1 - 65535). The server (System32control) needs to listen on this same port.

    4. On server (System32control), you're using the TCP protocol. Both programs need to be using the same protocol (UDP). Instead of Socket1.Listen it should be: Socket1.Bind <Port>, "192.168.1.101" which is the LAN IP of the server.

    I don't use UDP myself, but try fixing those things and see what happens. I can write an example if you need me to.

  3. #3

    Thread Starter
    Member
    Join Date
    May 2008
    Location
    Moorhead, MN
    Posts
    37

    Re: [RESOLVED] Winsock problem? Not sure -- Shuttingdown Multiple PCs on Network

    Quote Originally Posted by DigiRev
    I see a few problems.

    1. You're using the UDP protocol so the ConnectionRequest() event won't fire (UDP is a "connectionless" protocol).

    2. On client (Shutdown.vbp) you are specifying the RemoteHost as "0.0.0.0". That should be the IP of the computer you are shutting down. If you are shutting down all computers on the network I would enumerate all computers on the network and loop through them (I can help with this if you want).

    3. On client again, you are specifying RemotePort as "0". Make this a different number (1 - 65535). The server (System32control) needs to listen on this same port.

    4. On server (System32control), you're using the TCP protocol. Both programs need to be using the same protocol (UDP). Instead of Socket1.Listen it should be: Socket1.Bind <Port>, "192.168.1.101" which is the LAN IP of the server.

    I don't use UDP myself, but try fixing those things and see what happens. I can write an example if you need me to.
    1. Should be fixed.

    2. It was because I got an error at first but that was when the code was not complete so it was just a piece of code I should have removed but didnt. (My mistake) However, I am not in full understanding of how to use a loop for this properly (I tried and ended up just doing the multiple subs back and forth as my "loop") So any help creating the loop would be much appreciated.

    3. Should be fixed.

    4. Method changed to UDP now. Wasn't sure where sckRecieve.bind was supposed to go so I put it in the form load assuming if it was declared one time that would make it be declared throughout the entirety of the program? However I did just use the LocalIP was that incorrect? Do I need to define one or does pulling it from the local machine suffice?

    Heres my new code:

    Reciever Code:
    1. Private Sub Form_Load()
    2.     'frmSys32Control.Hide
    3.     sckRecieve.Protocol = sckUDPProtocol
    4.     sckRecieve.LocalPort = 4955
    5.     sckRecieve.RemotePort = 4955
    6.     sckRecieve.Bind ("4955"), LocalIP
    7. End Sub
    8.  
    9.  
    10. Private Sub sckRecieve_DataArrival(ByVal bytesTotal As Long)
    11.     Dim strData As String
    12.     sckRecieve.GetData strData
    13.     If strData = "shutdown" Then
    14.         MsgBox "shutdown"
    15.         'shell "shutdown -s -t 10"
    16.     ElseIf strData = "restart" Then
    17.         MsgBox "restart"
    18.         'shell "shutdown -r -t 10"
    19.     Else
    20.         'do nothing
    21.     End If
    22.    
    23. End Sub

    Sender Code:
    1. Option Explicit
    2. Dim varA As String
    3. Dim strCommand As String
    4. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    5. Dim Result As String
    6.  
    7. Public Sub Wait(HalfSeconds As Single)
    8.  Dim lMilliSeconds As Long
    9.  lMilliSeconds = HalfSeconds * 500
    10.  Sleep lMilliSeconds
    11. End Sub
    12.  
    13. Public Sub CmdSend1()
    14.     Dim strHostIP As String
    15.     If varA >= 256 Then
    16.         Exit Sub
    17.     ElseIf varA < 256 Then
    18.         sckSend.RemotePort = strHostPort
    19.         strHostIP = "192.168.0." & varA
    20.         varA = varA + 1
    21.         sckSend.RemoteHost = strHostIP
    22.         sckSend.SendData strCommand
    23.         Call Wait(1)
    24.         Call CmdSend2
    25.     Else
    26.         MsgBox "Talk to Josh if the problem persists.", vbCritical
    27.     End If
    28. End Sub
    29.  
    30. Public Sub CmdSend2()
    31.     Dim strHostIP As String
    32.     If varA >= 256 Then
    33.         Exit Sub
    34.     ElseIf varA < 256 Then
    35.         strHostIP = "192.168.0." & varA
    36.         varA = varA + 1
    37.         sckSend.RemoteHost = strHostIP
    38.         sckSend.SendData strCommand
    39.         Call Wait(1)
    40.         Call CmdSend1
    41.     Else
    42.         MsgBox "Talk to Josh if the problem persists.", vbCritical
    43.     End If
    44.     If Result = 1 Then
    45.         'do nothing because the question has already been answered
    46.     Else
    47.         If strCommand = "restart" Then
    48.             Result = MsgBox("Restart is now complete. Exit the program if you wish.", vbOKOnly)
    49.             If Result = 1 Then
    50.                 Exit Sub
    51.             Else
    52.                 MsgBox "Error, more info here"
    53.             End If
    54.         ElseIf strCommand = "shutdown" Then
    55.             Result = MsgBox("Shutdown is now complete. Exit the program if you wish.", vbOKOnly)
    56.             If Result = 1 Then
    57.                 Exit Sub
    58.             Else
    59.                 MsgBox "Error, more info here"
    60.             End If
    61.         End If
    62.     End If
    63. End Sub
    64.  
    65. Private Sub cmdRestart_Click()
    66.     Dim Result As String
    67.     Result = MsgBox("Are you sure you want to Restart all PC's?", vbYesNo)
    68.     If Result = 6 Then
    69.         MsgBox "Do not press anything for approxamitely one or two minutes. Restart in progress", vbCritical
    70.         varA = 0
    71.         strCommand = "restart"
    72.         Call CmdSend1
    73.     Else
    74.         'do nothing
    75.     End If
    76. End Sub
    77.  
    78. Private Sub cmdShutdown_Click()
    79.     Dim Result As String
    80.     Result = MsgBox("Are you sure you want to Shutdown all PC's?", vbYesNo)
    81.     If Result = 6 Then
    82.         MsgBox "Do not press anything for approxamitely one or two minutes. Shutdown in progress", vbCritical
    83.         varA = 250
    84.         strCommand = "shutdown"
    85.         Call CmdSend1
    86.     Else
    87.         'do nothing
    88.     End If
    89. End Sub
    90.  
    91. Private Sub Form_Load()
    92.     Result = 0
    93.     sckSend.Protocol = sckUDPProtocol
    94.     sckSend.RemoteHost = "192.168.0.0"
    95.     sckSend.RemotePort = 4955
    96. End Sub

    There code should be cleaned up a bit... Thanks DigiRev for the help so far!

    It works! Excellent, thank you again DigiRev about that loop to clean it up though? Could you help me with that? I don't quite understand how to use the loop in anyway that doesn't crash my computer (which I've been told is counter-productive)
    Last edited by Genovah; May 4th, 2008 at 08:34 AM. Reason: Resolved
    -- Josh Smith
    C#, VB, PHP, SQL

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: [Not Resolved] Winsock problem? Not sure -- Shuttingdown Multiple PCs on Network

    Sorry it took so long to respond, was up real late last night and had to take a nap.

    Try this, it works for me. It scans all computers on the network and sends a shutdown/restart command to all of them.

    I wrote it from scratch to clean up the code, so you may have to add some stuff back in there. You will have to add the restart/shutdown code in the DataArrival() event on the receiver, I forgot to put it back in (right now it just shows a message box).

    Code to enumerate the network was taken from here:
    http://www.pscode.com/vb/scripts/Sho...61827&lngWId=1
    Attached Files Attached Files

  5. #5

    Thread Starter
    Member
    Join Date
    May 2008
    Location
    Moorhead, MN
    Posts
    37

    Re: [Not Resolved] Winsock problem? Not sure -- Shuttingdown Multiple PCs on Network

    Thanks Digi!
    -- Josh Smith
    C#, VB, PHP, SQL

  6. #6
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: [RESOLVED] Winsock problem? Not sure -- Shuttingdown Multiple PCs on Network

    You're welcome.

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