用个Timer,以下代码实现3秒内连接不成功自动终止,楼主试试看可用不?Private Sub Command1_Click() Winsock1.RemoteHost = "12.34.56.78" Winsock1.RemotePort = 12345 Timer1.Enabled = True Winsock1.Connect End SubPrivate Sub Form_Load() Timer1.Interval = 3000 Timer1.Enabled = False End SubPrivate Sub Timer1_Timer() Debug.Print Winsock1.State If Winsock1.State <> 0 Then Winsock1.Close Debug.Print Winsock1.State MsgBox "连接被终止!" End If Timer1.Enabled = False End Sub
private bln as boolean private waitcount as long private sub form_load() bln=false winsock.connect ip,port timer1.enable=true timer1.Interval =100 end subPrivate Sub winsock_Connect() bln=true end subPrivate Sub Timer1_Timer() waitcount=waitcount+1 if waitcount<50 then if bln then end if elseend if end sub
Private Sub Timer1_Timer() waitcount=waitcount+1 if waitcount<50 then'连接时间自己设,我设成5秒 if bln then Timer1.Enabled = False waitcount=0 end if else msgbox "连接超时" end if end sub
Winsock1.RemoteHost = "12.34.56.78"
Winsock1.RemotePort = 12345
Timer1.Enabled = True
Winsock1.Connect
End SubPrivate Sub Form_Load()
Timer1.Interval = 3000
Timer1.Enabled = False
End SubPrivate Sub Timer1_Timer()
Debug.Print Winsock1.State
If Winsock1.State <> 0 Then
Winsock1.Close
Debug.Print Winsock1.State
MsgBox "连接被终止!"
End If
Timer1.Enabled = False
End Sub
private waitcount as long
private sub form_load()
bln=false
winsock.connect ip,port
timer1.enable=true
timer1.Interval =100
end subPrivate Sub winsock_Connect()
bln=true
end subPrivate Sub Timer1_Timer()
waitcount=waitcount+1
if waitcount<50 then
if bln then end if
elseend if
end sub
waitcount=waitcount+1
if waitcount<50 then'连接时间自己设,我设成5秒
if bln then
Timer1.Enabled = False
waitcount=0
end if
else
msgbox "连接超时"
end if
end sub
为保证可靠性,需要不断的监视状态,最好是单线程。
若出现断开,则重新连接。现在发现,若不断的Close后再Connect,
时间一长,如24小时后,就会出现程序僵死的现象,
不知道问题出在哪里?