|
本帖最后由 DenQ 于 2010-12-8 21:55 编辑
去掉UDP,反应就不慢了,UDP加在循环里加入,操作数据就很慢(延迟5-10),有时更久。
再问个问题啊,你们的程序为什么就可以写得那么小?我随便写一个msgbox就有 200-300KB左右。
客户端代码:#Include <Date.au3>
#Include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
Opt("TrayMenuMode",1)
Opt("TrayOnEventMode",1)
;Opt("MustDeclareVars", 1)
TraySetClick(16)
opt("GUICloseOnESC",0) ;关闭ESC
$Runreg="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
$Regname="检测服务器"
Local $ini = @ScriptDir&"\Save.ini"
Local $msg, $recv, $listview, $button, $item1, $item2, $item3, $input1
Local $menu, $szitem
Global $nCurCol = -1
Global $nSortDir = 1
Global $bSet = 0
Global $nCol = -1
$GOOEY = GUICreate("机器信息检测服务器" , 650, 350) ; 创建一个图形用户界面消息窗
GUISetBkColor(0x00E0FFFF)
$lv = GUICtrlCreateListView("",10, 10, 600, 300);,$LVS_SORTDESCENDING)
_GUICtrlListView_AddColumn($lv, "机器名", 100, 2)
_GUICtrlListView_AddColumn($lv, "MBR状态", 150, 2)
_GUICtrlListView_AddColumn($lv, "检测时间", 130, 2)
_GUICtrlListView_AddColumn($lv, "数据预览", 500, 2)
_GUICtrlListView_SetExtendedListViewStyle($lv, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES));, $LVS_EX_TRACKSELECT))
_GUICtrlListView_SetColumnWidth($lv, 0, 80)
_GUICtrlListView_SetColumnWidth($lv, 1, 80)
_GUICtrlListView_SetColumnWidth($lv, 3, 90)
_GUICtrlListView_SetColumnWidth($lv, 4, 500)
GUICtrlRegisterListViewSort(-1, "LVSort")
$menu = GUICtrlCreateMenu("菜单")
$szitem = GUICtrlCreateMenuItem("设置", $menu)
$guiexititem = GUICtrlCreateMenuItem("退出", $menu)
;~ GUICtrlSetState(-1, $GUI_DEFBUTTON) ;加深
$contextmenu = GUICtrlCreateContextMenu($lv)
$buttoncontext = GUICtrlCreateContextMenu($button)
$buttonitem = GUICtrlCreateMenuItem("About button", $buttoncontext)
$newsubmenu = GUICtrlCreateMenu("操作", $contextmenu)
$textitem = GUICtrlCreateMenuItem("设置正常", $newsubmenu)
$textitem2 = GUICtrlCreateMenuItem("重新记录", $newsubmenu)
$fileitem = GUICtrlCreateMenuItem("MBR", $contextmenu)
$saveitem = GUICtrlCreateMenuItem("关于", $contextmenu)
GUICtrlCreateMenuItem("", $contextmenu) ; separator
$guiexititem2 = GUICtrlCreateMenuItem("退出", $contextmenu)
$TsReg = RegRead($Runreg, $Regname)
If $TsReg=@ScriptFullPath Then
TrayItemSetState (9,$TRAY_CHECKED)
Else
TrayItemSetState (10,$TRAY_CHECKED)
EndIf
UDPStartup()
$ConnectedSocket = UDPBind(@IPAddress1, 8410)
If @error <> 0 Then Exit
Read()
GUISetState()
While 1 ; 循环图形用户界面消息
_ReduceMemory(@AutoItPID) ;内存整理
$msg = GUIGetMsg()
Switch $msg
Case $guiexititem, $guiexititem2, $GUI_EVENT_CLOSE
ExitLoop ;ExitLoop 关闭图形用户界面
Case $GUI_EVENT_MINIMIZE
GUISetState(@SW_HIDE,$GOOEY)
Case $textitem
$itemid=StringSplit(GUICtrlRead(GUICtrlRead($lv)),"|")
If $itemid[0] = 5 Then
IniWrite($ini,"MBR",$itemid[1],"正常"&"|"&_now()&"|"&$itemid[4])
Read()
EndIf
Case $textitem2
$itemid2=StringSplit(GUICtrlRead(GUICtrlRead($lv)),"|")
IniDelete($ini,"MBR",$itemid2[1])
Read()
Case $lv
$bSet = 0
$nCurCol = $nCol
GUICtrlSendMsg($lv, $LVM_SETSELECTEDCOLUMN, GUICtrlGetState($lv), 0)
DllCall("user32.dll", "int", "InvalidateRect", "hwnd", GUICtrlGetHandle($lv), "int", 0, "int", 1)
EndSwitch
$recv = UDPRecv($ConnectedSocket, 2000)
If $recv <> "" Then
$ip=StringSplit($recv,"|") ;获取$ip数组, 以|为分隔符。
$socket1=UDPOpen($ip[1],8411)
$chose = IniRead($ini, "MBR", $ip[2], "")
If $chose <> "" Then
$fj2=StringSplit($chose,"|")
$chose = $fj2[3]
EndIf
Select
Case $chose = ""
IniWrite($ini, "MBR", $ip[2], "首次记录"&"|"&_now()&"|"&$ip[3])
Read()
Case $chose = $ip[3]
IniWrite($ini, "MBR", $ip[2], "正常"&"|"&_now()&"|"&$chose)
Read()
Case $chose <> $ip[3]
IniWrite($ini, "MBR", $ip[2], "异常"&"|"&_now()&"|"&$chose)
Read()
EndSelect
UDPCloseSocket($socket1)
sleep(10)
EndIf
WEnd
;~ GUIDelete()
Func Exititem()
Exit
EndFunc
Func _ReduceMemory($i_PID = -1);整理内存
If $i_PID <> -1 Then
Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
Else
Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
EndIf
Return $ai_Return[0]
EndFunc ;==>_ReduceMemory
Func Read()
_GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($lv))
If FileExists ($ini) Then
$wl = IniReadSection($ini, "MBR")
If @error=0 Then
For $i = 1 To $wl[0][0]
GUICtrlCreateListViewItem($wl[$i][0]&"|"&$wl[$i][1],$lv)
;~ GUICtrlSetImage(-1, "shell32.dll", 16)
Next
EndIf
EndIf
EndFunc
客户端代码:#include <WinAPI.au3>
#include <array.au3>
Const $Disk0_ID = "\\.\PhysicalDrive0"
Const $Disk1_ID = "\\.\PhysicalDrive1"
$data = _DeviceDataRead ($Disk0_ID,0,1)
;~ FileWrite("aa.txt",StringTrimLeft($data,2))
;~ MsgBox (0,"MBR (0号扇区数据)",$data)
$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled != 0", "WQL", 0x10 + 0x20)
If IsObj($colItems) then
For $objItem In $colItems
Dim $netmask=""
$localIP=$objItem.IPAddress(0)
$ip=StringSplit($localIP,".")
$net=StringSplit($objItem.IPSubnet(0),".")
For $i=1 To $ip[0]
If $i = $ip[0] Then
$netmask=$netmask&BitOR($ip[$i],BitXOR($net[$i],255))
Else
$netmask=$netmask&BitOR($ip[$i],BitXOR($net[$i],255))&"."
EndIf
Next
If Ping($netmask,10) Then
UDPStartup()
$socket = UDPOpen($netmask, 8410)
$socket1 = UDPBind($localIP,8411)
$name = EnvGet ("computername")
$status = UDPSend($socket, $localIP&"|"&$name&"|"&$data)
If $status <> 0 then
UDPCloseSocket($socket)
UDPCloseSocket($socket1)
UDPShutdown()
EndIf
EndIf
Next
EndIf
Func _DeviceDataRead ($DeviceID,$Offset,$Length = 1)
Local $hFile,$Buffer,$ptr,$Read,$Result
;~ $Pos = "0x"&Hex( $Offset * 0x200)
;~ MsgBox(0,0,$Pos)
$Length = $Length * 0x200 ;定义读取长度
;~ MsgBox(0,0,$Length)
$Buffer = DllStructCreate("byte[" & $Length & "]")
$ptr = DllStructGetPtr($Buffer)
$hFile = _WinAPI_CreateFile($DeviceID, 2, 2, 3, 0, 0)
If $hFile = 0 Then Return 0
$Result = _WinAPI_SetFilePointer($hFile, $Offset)
$err = @error
If $Result = 0xFFFFFFFF Then
_WinAPI_CloseHandle($hFile)
Return -3
EndIf
$Result = _WinAPI_ReadFile($hFile, $ptr, $Length, $Read)
If Not $Result Then
_WinAPI_CloseHandle($hFile)
Return -2
EndIf
_WinAPI_CloseHandle($hFile)
If Not $Result Then Return -3
$Result = DllStructGetData($Buffer, 1)
Return $Result
EndFunc
|
|