一、使用Winsock控件
wincock 控件象其他的控件一样需要注册,并且需要控件证书才可以用。
如果您使用Winsock 控件时遇到问题。请执行下面LAS脚本程序重新注册Winsock控件。
filename = win.openDlg( "请指定MSWINSCK.OCX路径" ,"*.ocx|*.ocx||" ) ;--MSWINSCK.OCX assert ( win.pathOK( filename) ,"请指定一个有效路径" ) ; win.exec( "Regsvr32.exe" ,filename.." /u" ,"" ,5 ,true ) ; --卸载原来的控件 --如果Wincock控件不在系统目录下,移动到系统目录 if ( not string.find ( string.lower ( filename) ,string.lower ( win.getSysDir( ) ) ) ) then os .rename ( filename,win.getSysDir( ) .." \\ Wincock.ocx" ) ;end ; win.exec( "Regsvr32.exe" ,filename,"" ,5 ,true ) ; --重新注册控件 --在注册表中添加Wincock控件证书 reg = regist.open( "HKEY_CLASSES_ROOT \\ Licenses \\ 2c49f800-c2dd-11cf-9ad6-0080c7e7b78d" ) ; reg:setValue( "" ,"mlrljgrlhltlngjlthrligklpkrhllglqlrk" ) ; win.messageBox( "OK!" )
二、使用Winsock下载网页的示例
sckClosed = 0 --缺省值,关闭。 SckOpen = 1 --打开。 SckListening = 2 --侦听 sckConnectionPending = 3 --连接挂起 sckResolvingHost = 4 --识别主机。 sckHostResolved = 5 --已识别主机 sckConnecting = 6 --正在连接。 sckConnected = 7 --已连接。 sckClosing = 8 --同级人员正在关闭连接。 sckError = 9 --错误 vbString = 8 --测试当前连接状态 function wskTestState( ) while ( nStop( ) ) do delay( 1 ) if ( Winsock1.State == SckOpen) then print ( "已打开..." ) elseif ( Winsock1.State == sckClosed) then print ( "已关闭" ) do break end ; elseif ( Winsock1.State == SckListening) then print ( "侦听..." ) elseif ( Winsock1.State == sckConnectionPending ) then print ( "挂起" ) do break end ; elseif ( Winsock1.State == sckResolvingHost) then print ( "识别主机......" ) elseif ( Winsock1.State == sckHostResolved) then print ( "已识别主机!" ) elseif ( Winsock1.State == sckConnecting ) then print ( "正在连接......" ) elseif ( Winsock1.State == sckConnected) then print ( "已连接!" ) do break end ; elseif ( Winsock1.State == sckClosing) then print ( "正在关闭连接!" ) do break end ; elseif ( Winsock1.State == sckError) then print ( "错误!" ) do break end ; end ; end ; end ;
;--引用comx插件
--创建Winsock对象 Winsock1 =comx.CreateObject( "MSWinsock.Winsock" ) --创建一个事件对象(table) winsock_events = { } --声明dataArrival事件回调函数(winsock控件收到数据时触发) function winsock_events:DataArrival( l) str = "test" str = Winsock1:GetData( str,vbString,l) ; print ( "收到数据" ..str) ;end ; --把DataArrival函数 绑定到 Winsock1对象 res, cookie = comx.Connect( Winsock1, winsock_events) if res == nil then win.messageBox( "绑定事件回调函数失败" ,"" ) do return false end ;end Winsock1.Protocol = 0 ; Winsock1.RemoteHost = "" ; Winsock1.RemotePort = 80 ; Winsock1:Connect( ) ; win.consoleOpen( ) ; wskTestState( ) ; Url = "http:///index.html" Cmd = "GET " ..Url.." HTTP/1.0 \r \n \r \n " Winsock1:SendData( Cmd) ;--发送HTTP请求 wskTestState( ) ;--检测Winsock控件状态 -- 需要延时才能调用事件回调函数,否则关闭了这个线程就不能响应回调函数了 delay( 2000 ) -- 一定要释放COM对象 Winsock1 = nil ;collectgarbage ( ) ;
三、使用Winsock获取本机IP地址
;--引用comx插件
ws =comx.CreateObject("MSWinsock.Winsock")
ipAddress=ws.LocalIP
win.messageBox( "本机IP="..ipAddress,"");
ws = nil;--删除变量
collectgarbage(); --回收内存
四、在中使用Winsock
CLASSID="CLSID:248DD896-BB45-11CF-9ABC-0080C7E7B78D">
建立连接
事件回调函数,收到数据时自动触发
阅读(718) | 评论(0) | 转发(0) |