局域网通讯
本帖最后由 bingxing8000 于 2011-2-11 16:37 编辑小弟现在刚开始接触autoit里的tcp ,现在需要把一台主机的磁盘的使用情况显示到另一台主机
一下是小弟的代码,望指教
服务器端
#include <GUIConstantsEx.au3>
$form=GUICreate("磁盘使用量",300,200)
GUISetState(@SW_SHOW)
_SkinGUI("C:\Program Files\SkinCrafterDll.dll", "C:\Program Files\vista_style.skf", $form)
$c=DriveSpaceFree( "c:\" )/DriveSpaceTotal( "c:\" )*100& "%" ;C盘剩余空间占用的百分比
$d=DriveSpaceFree( "d:\" )/DriveSpaceTotal( "d:\" )*100 & "%" ;d盘剩余空间占用的百分比
$e=DriveSpaceFree( "e:\" )/DriveSpaceTotal( "e:\" ) *100 & "%" ;e盘剩余空间占用的百分比
GUICtrlCreateLabel ("C盘剩余空间",10,30,100,50)
$box1=GUICtrlCreateLabel ("",120,30,120,50)
GUICtrlSetData ($box1,$c)
GUICtrlCreateLabel ("D盘剩余空间",10,80,100,50)
$box3=GUICtrlCreateLabel ("",120,80,120,50)
GUICtrlSetData ($box3,$d)
GUICtrlCreateLabel ("E盘剩余空间",10,130,100,50)
$box5=GUICtrlCreateLabel ("",120,130,120,50)
GUICtrlSetData ($box5,$e)
TCPStartup() ;开始 TCP/UDP 服务
$lianjie=TCPListen(@IPAddress1,44556) ;创建一个套接字(socket)监听.
If $lianjie= -1 Then MsgBox(0,"警告","服务器开启失败") ; 创建监听不成功就退出
TCPAccept($lianjie);尝试接受一个套接字(socket)连接.
$box6= StringToBinary($c, 4);转换字符串为二进制数据. 标志 = 4, 二进制数据为 UTF8 编码
TCPSend($lianjie,$box6) ;在已经连接的套接字(socket)上面发送数据.
While 1
$msg=GUIGetMsg()
Select
Case $msg= -3
_ent()
EndSelect
WEnd
Func _ent()
TCPCloseSocket($lianjie);关闭一个 TCP 套接字(socket).
TCPShutdown () ;停止 TCP/UDP 服务
Exit
EndFunc
Func _SkinGUI($SkincrafterDll, $SkincrafterSkin, $Handle)
$Dll = DllOpen($SkincrafterDll)
DllCall($Dll, "int:cdecl", "InitLicenKeys", "wstr", "1", "wstr", "", "wstr", "1@1.com", "wstr", "1")
DllCall($Dll, "int:cdecl", "InitDecoration", "int", 1)
DllCall($Dll, "int:cdecl", "LoadSkinFromFile", "wstr", $SkincrafterSkin)
DllCall($Dll, "int:cdecl", "DecorateAs", "int", $Handle, "int", 25)
DllCall($Dll, "int:cdecl", "ApplySkin")
EndFunc;==>_SkinGUI
客户端
#include <GUIConstantsEx.au3>
$form=GUICreate("磁盘使用量",300,200)
GUISetState(@SW_SHOW)
_SkinGUI("C:\Program Files\SkinCrafterDll.dll", "C:\Program Files\vista_style.skf", $form)
$box3="10.0.90.214"
TCPStartup() ;开始 TCP/UDP 服务
$lianjie=TCPConnect($box3,44556) ;创建一个套接字(socket)连接到已经存在的服务器..
If $lianjie= -1 Then MsgBox(0,"提醒",$box3 & " 服务器未开启") ; 创建监听不成功就退出
$box5=TCPRecv($lianjie,2048) ;从已经连接的套接字里面接收数据
$box5=BinaryToString($box5, 4);转换一个二进制变量为一个字符串. 标志 = 4, 二进制 数据原先为 UTF8 编码
While 1
$msg=GUIGetMsg()
Select
Case $msg= -3
_ent()
Case $msg=$box4
MsgBox(0,"警告",$box5)
EndSelect
WEnd
Func _ent()
TCPCloseSocket($lianjie);关闭一个 TCP 套接字(socket).
TCPShutdown () ;停止 TCP/UDP 服务
Exit
EndFunc
Func _SkinGUI($SkincrafterDll, $SkincrafterSkin, $Handle)
$Dll = DllOpen($SkincrafterDll)
DllCall($Dll, "int:cdecl", "InitLicenKeys", "wstr", "1", "wstr", "", "wstr", "1@1.com", "wstr", "1")
DllCall($Dll, "int:cdecl", "InitDecoration", "int", 1)
DllCall($Dll, "int:cdecl", "LoadSkinFromFile", "wstr", $SkincrafterSkin)
DllCall($Dll, "int:cdecl", "DecorateAs", "int", $Handle, "int", 25)
DllCall($Dll, "int:cdecl", "ApplySkin")
EndFunc;==>_SkinGUI 标题不明,问题不明.想锁贴? 呵呵,不好意思。就是想用tcp在两台主机中传输数据,但不成功 S:
AutoItSetOption("GUIOnEventMode", 1)
AutoItSetOption("TCPTimeout", 100);这个选项设置似乎没有用,有空再研究下
TCPStartup()
If @error Then Exit MsgBox(16, "WSA初始化错误", "错误代码: " & @error)
$MainSocket = TCPListen("127.0.0.1", 44556)
If $MainSocket = -1 Then Exit MsgBox(16, "错误", "套接字创建失败,程序退出.")
Local $Win = GUICreate("服务端")
GUISetOnEvent(-3, "_GuiEvent")
Global $Edit = GUICtrlCreateEdit("", 8, 8, 382, 382)
GUISetState()
Do
Sleep(1)
$ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1
While 1
Sleep(1)
$recv = TCPRecv($ConnectedSocket, 1024 * 1000, 1)
If @error Then ExitLoop
If $recv <> "" Then GUICtrlSetData($Edit, BinaryToString($recv, 4) & @CRLF & GUICtrlRead($Edit))
WEnd
Func _GuiEvent()
Switch @GUI_CtrlId
Case -3
Exit
EndSwitch
EndFunc ;==>_GuiEvent
C:
AutoItSetOption("GUIOnEventMode", 1)
AutoItSetOption("TCPTimeout", 100);这个选项设置似乎没有用,有空再研究下
TCPStartup()
If @error Then Exit MsgBox(16, "WSA初始化错误", "错误代码: " & @error)
Local $Win = GUICreate("客户端")
GUISetOnEvent(-3, "_GuiEvent")
Global $Edit = GUICtrlCreateEdit("", 8, 8, 382, 382)
GUISetState()
Report()
While 1
Sleep(100)
WEnd
Func _GuiEvent()
Switch @GUI_CtrlId
Case -3
Exit
EndSwitch
EndFunc ;==>_GuiEvent
Func Report()
Local $Report, $i, $Socket
Local $FIXED = DriveGetDrive("FIXED")
If Not @error Then
For $i = 1 To $FIXED
$Report &= StringUpper($FIXED[$i]) & "盘总容量:" & Int(DriveSpaceTotal($FIXED[$i]) / 1024) & "G 剩余空间比例:" & String(Int(DriveSpaceFree($FIXED[$i]) / DriveSpaceTotal($FIXED[$i]) * 100)) & "%" & @CRLF
Next
GUICtrlSetData($Edit, $Report)
$Socket = TCPConnect("127.0.0.1", 44556)
If @error Then
Return MsgBox(16, "错误", "服务端未开启!错误代码: " & @error)
Else
TCPSend($Socket, StringToBinary($Report,4))
If @error Then Return MsgBox(16, "错误", "发送数据错误!错误代码: " & @error)
EndIf
EndIf
EndFunc ;==>Report
看不懂,顶一下吧 喔,又是一个好代码。多谢了。 谢谢 ceoguang前辈了,要是有注释就更好了{:face (303):} {:face (303):} {:face (303):} 要学的还很多!
页:
[1]