1 Attachment(s)
[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:
Private Sub Form_Load()
' Socket1.Listen
' frmSys32Control.Hide
End Sub
Private Sub Socket1_ConnectionRequest(ByVal requestID As Long)
Socket1.Accept requestID
End Sub
Private Sub Socket1_DataArrival(ByVal bytesTotal As Long)
'sckServer(Index).GetData strData, vbString, bytesTotal
Dim strData As String
Socket1.GetData strData, vbString, bytesTotal
If strData = "shutdown" Then
MsgBox "shutdown"
'shell "shutdown -s -t 10"
ElseIf strData = "restart" Then
MsgBox "restart"
'shell "shutdown -r -t 10"
Else
'do nothing stupid
End If
End Sub
Sender Code:
Option Explicit
Dim varA As String
Dim strCommand As String
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim Result As String
Public Sub Wait(HalfSeconds As Single)
Dim lMilliSeconds As Long
lMilliSeconds = HalfSeconds * 500
Sleep lMilliSeconds
End Sub
Public Sub CmdSend1()
Dim strHostIP As String
Dim strHostPort As String
strHostIP = "0.0.0.0"
strHostPort = "0"
If varA >= 256 Then
Exit Sub
ElseIf varA < 256 Then
strHostPort = 4955
sckSend.RemotePort = strHostPort
strHostIP = "192.168.0." & varA
varA = varA + 1
sckSend.RemoteHost = strHostIP
sckSend.SendData strCommand
Call Wait(1)
Call CmdSend2
Else
MsgBox "Talk to Josh if the problem persists.", vbCritical
End If
End Sub
Public Sub CmdSend2()
Dim strHostIP As String
Dim strHostPort As String
' Dim Result As String
strHostIP = "0.0.0.0"
strHostPort = "0"
If varA >= 256 Then
Exit Sub
ElseIf varA < 256 Then
strHostPort = 4955
sckSend.RemotePort = strHostPort
strHostIP = "192.168.0." & varA
varA = varA + 1
sckSend.RemoteHost = strHostIP
sckSend.SendData strCommand
Call Wait(1)
Call CmdSend1
Else
MsgBox "Talk to Josh if the problem persists.", vbCritical
End If
If Result = 1 Then
'do nothing because the question has already been answered
Else
If strCommand = "restart" Then
Result = MsgBox("Restart is now complete. Exit the program if you wish.", vbOKOnly)
If Result = 1 Then
Exit Sub
Else
MsgBox "***? how did you manage that?!??!!"
End If
ElseIf strCommand = "shutdown" Then
Result = MsgBox("Shutdown is now complete. Exit the program if you wish.", vbOKOnly)
If Result = 1 Then
Exit Sub
Else
MsgBox "***? how did you manage that?!??!!"
End If
End If
End If
End Sub
Private Sub cmdRestart_Click()
Dim Result As String
Result = MsgBox("Are you sure you want to Restart all PC's?", vbYesNo)
If Result = 6 Then
MsgBox "Do not press anything for approxamitely one or two minutes. Restart in progress", vbCritical
varA = 0
strCommand = "restart"
Call CmdSend1
Else
'do nothing
End If
End Sub
Private Sub cmdShutdown_Click()
Dim Result As String
Result = MsgBox("Are you sure you want to Shutdown all PC's?", vbYesNo)
If Result = 6 Then
MsgBox "Do not press anything for approxamitely one or two minutes. Shutdown in progress", vbCritical
varA = 250
strCommand = "shutdown"
Call CmdSend1
Else
'do nothing
End If
End Sub
Private Sub Form_Load()
Result = 0
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
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". :confused: 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.
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". :confused: 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:
Private Sub Form_Load()
'frmSys32Control.Hide
sckRecieve.Protocol = sckUDPProtocol
sckRecieve.LocalPort = 4955
sckRecieve.RemotePort = 4955
sckRecieve.Bind ("4955"), LocalIP
End Sub
Private Sub sckRecieve_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
sckRecieve.GetData strData
If strData = "shutdown" Then
MsgBox "shutdown"
'shell "shutdown -s -t 10"
ElseIf strData = "restart" Then
MsgBox "restart"
'shell "shutdown -r -t 10"
Else
'do nothing
End If
End Sub
Sender Code:
Option Explicit
Dim varA As String
Dim strCommand As String
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim Result As String
Public Sub Wait(HalfSeconds As Single)
Dim lMilliSeconds As Long
lMilliSeconds = HalfSeconds * 500
Sleep lMilliSeconds
End Sub
Public Sub CmdSend1()
Dim strHostIP As String
If varA >= 256 Then
Exit Sub
ElseIf varA < 256 Then
sckSend.RemotePort = strHostPort
strHostIP = "192.168.0." & varA
varA = varA + 1
sckSend.RemoteHost = strHostIP
sckSend.SendData strCommand
Call Wait(1)
Call CmdSend2
Else
MsgBox "Talk to Josh if the problem persists.", vbCritical
End If
End Sub
Public Sub CmdSend2()
Dim strHostIP As String
If varA >= 256 Then
Exit Sub
ElseIf varA < 256 Then
strHostIP = "192.168.0." & varA
varA = varA + 1
sckSend.RemoteHost = strHostIP
sckSend.SendData strCommand
Call Wait(1)
Call CmdSend1
Else
MsgBox "Talk to Josh if the problem persists.", vbCritical
End If
If Result = 1 Then
'do nothing because the question has already been answered
Else
If strCommand = "restart" Then
Result = MsgBox("Restart is now complete. Exit the program if you wish.", vbOKOnly)
If Result = 1 Then
Exit Sub
Else
MsgBox "Error, more info here"
End If
ElseIf strCommand = "shutdown" Then
Result = MsgBox("Shutdown is now complete. Exit the program if you wish.", vbOKOnly)
If Result = 1 Then
Exit Sub
Else
MsgBox "Error, more info here"
End If
End If
End If
End Sub
Private Sub cmdRestart_Click()
Dim Result As String
Result = MsgBox("Are you sure you want to Restart all PC's?", vbYesNo)
If Result = 6 Then
MsgBox "Do not press anything for approxamitely one or two minutes. Restart in progress", vbCritical
varA = 0
strCommand = "restart"
Call CmdSend1
Else
'do nothing
End If
End Sub
Private Sub cmdShutdown_Click()
Dim Result As String
Result = MsgBox("Are you sure you want to Shutdown all PC's?", vbYesNo)
If Result = 6 Then
MsgBox "Do not press anything for approxamitely one or two minutes. Shutdown in progress", vbCritical
varA = 250
strCommand = "shutdown"
Call CmdSend1
Else
'do nothing
End If
End Sub
Private Sub Form_Load()
Result = 0
sckSend.Protocol = sckUDPProtocol
sckSend.RemoteHost = "192.168.0.0"
sckSend.RemotePort = 4955
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)
1 Attachment(s)
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. :p
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
Re: [Not Resolved] Winsock problem? Not sure -- Shuttingdown Multiple PCs on Network
Re: [RESOLVED] Winsock problem? Not sure -- Shuttingdown Multiple PCs on Network