在之前调用Winsock控件的Close方法。
下面是一个简单的Telnet客户的例子,其中发送数据的时候也是这样处理的:
Option ExplicitPrivate iniName As StringPrivate Sub MakeInfo() stt.Panels(1).Text = "Now using ini - " & iniNameEnd SubPrivate Sub Form_Load() iniName = (App.Path + "\host.ini")
Call MakeInfoEnd SubPrivate Sub Form_Resize() On Error Resume Next txtData.Height = Me.ScaleHeight - stt.Height
txtResponse.Height = Me.ScaleHeight - stt.Height
txtData.Width = Me.ScaleWidth / 2
txtResponse.Width = Me.ScaleWidth / 2
txtResponse.Left = Me.ScaleWidth / 2 + 1End SubPrivate Sub mnuFileOpen_Click()
On Error GoTo FileError Dim strLine As String txtData.Text = ""
cdlFileOpen.FileName = ""
cdlFileOpen.Filter = "TXT|*.txt"
cdlFileOpen.ShowOpen
If cdlFileOpen.FileName = "" Then
Exit Sub
Else
Open cdlFileOpen.FileName For Input As #2
Do While Not EOF(2)
Line Input #2, strLine
Debug.Print strLine
txtData.Text = txtData.Text + strLine + Chr(13) + Chr(10)
Loop
End If
Close #2
Exit Sub
FileError: MsgBox "Can not read file!"
Err.Clear
End SubPrivate Sub mnuFileQuit_Click() Unload MeEnd SubPrivate Sub mnuFileSave_Click()On Error GoTo CanNotSave cdlFileOpen.FileName = ""
cdlFileOpen.Filter = "TXT|*.txt"
cdlFileOpen.ShowSave
If cdlFileOpen.FileName = "" Then
Exit Sub
Else
Open cdlFileOpen.FileName For Output As #3
Print #3, txtData.Text
Close #3
End If
Exit Sub
CanNotSave: MsgBox "File access error!"
Err.ClearEnd SubPrivate Sub mnuFileUseINI_Click() cdlFileOpen.FileName = ""
cdlFileOpen.Filter = "INI|*.ini"
cdlFileOpen.ShowOpen
If cdlFileOpen.FileName = "" Then
Exit Sub
Else
iniName = cdlFileOpen.FileName
Call MakeInfo
End IfEnd SubPrivate Sub mnuTransData_Click()On Error GoTo MissingIni Dim strLine As String
Dim i As Integer, j As Integer
Dim strFname As String
Dim strHost As String, strPort As String
Dim strKey As String, strVal As String
wsTelnet.Close
Open iniName For Input As #1
For j = 1 To 2
Line Input #1, strLine
i = InStr(1, strLine, "=")
If i <> 0 Then
strKey = LTrim(RTrim(Mid(strLine, 1, i - 1)))
Debug.Print strKey
strVal = LTrim(RTrim(Mid(strLine, i + 1, Len(strLine) - i)))
Debug.Print strVal
Select Case UCase(strKey)
Case Is = "HOST"
wsTelnet.RemoteHost = strVal
Case Is = "PORT"
wsTelnet.RemotePort = strVal
End Select
End If
Next
Close #1
wsTelnet.Connect
Exit Sub
MissingIni:
MsgBox "Missing host.ini!"
Err.Clear
End SubPrivate Sub wsTelnet_Connect() wsTelnet.SendData txtData.TextEnd SubPrivate Sub wsTelnet_DataArrival(ByVal bytesTotal As Long) Dim strResponse As String wsTelnet.GetData strResponse
txtResponse.Text = txtResponse.Text + strResponse
txtResponse.SelStart = Len(txtResponse.Text)End SubPrivate Sub wsTelnet_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 "Network error! - " & DescriptionEnd Sub
下面是一个简单的Telnet客户的例子,其中发送数据的时候也是这样处理的:
Option ExplicitPrivate iniName As StringPrivate Sub MakeInfo() stt.Panels(1).Text = "Now using ini - " & iniNameEnd SubPrivate Sub Form_Load() iniName = (App.Path + "\host.ini")
Call MakeInfoEnd SubPrivate Sub Form_Resize() On Error Resume Next txtData.Height = Me.ScaleHeight - stt.Height
txtResponse.Height = Me.ScaleHeight - stt.Height
txtData.Width = Me.ScaleWidth / 2
txtResponse.Width = Me.ScaleWidth / 2
txtResponse.Left = Me.ScaleWidth / 2 + 1End SubPrivate Sub mnuFileOpen_Click()
On Error GoTo FileError Dim strLine As String txtData.Text = ""
cdlFileOpen.FileName = ""
cdlFileOpen.Filter = "TXT|*.txt"
cdlFileOpen.ShowOpen
If cdlFileOpen.FileName = "" Then
Exit Sub
Else
Open cdlFileOpen.FileName For Input As #2
Do While Not EOF(2)
Line Input #2, strLine
Debug.Print strLine
txtData.Text = txtData.Text + strLine + Chr(13) + Chr(10)
Loop
End If
Close #2
Exit Sub
FileError: MsgBox "Can not read file!"
Err.Clear
End SubPrivate Sub mnuFileQuit_Click() Unload MeEnd SubPrivate Sub mnuFileSave_Click()On Error GoTo CanNotSave cdlFileOpen.FileName = ""
cdlFileOpen.Filter = "TXT|*.txt"
cdlFileOpen.ShowSave
If cdlFileOpen.FileName = "" Then
Exit Sub
Else
Open cdlFileOpen.FileName For Output As #3
Print #3, txtData.Text
Close #3
End If
Exit Sub
CanNotSave: MsgBox "File access error!"
Err.ClearEnd SubPrivate Sub mnuFileUseINI_Click() cdlFileOpen.FileName = ""
cdlFileOpen.Filter = "INI|*.ini"
cdlFileOpen.ShowOpen
If cdlFileOpen.FileName = "" Then
Exit Sub
Else
iniName = cdlFileOpen.FileName
Call MakeInfo
End IfEnd SubPrivate Sub mnuTransData_Click()On Error GoTo MissingIni Dim strLine As String
Dim i As Integer, j As Integer
Dim strFname As String
Dim strHost As String, strPort As String
Dim strKey As String, strVal As String
wsTelnet.Close
Open iniName For Input As #1
For j = 1 To 2
Line Input #1, strLine
i = InStr(1, strLine, "=")
If i <> 0 Then
strKey = LTrim(RTrim(Mid(strLine, 1, i - 1)))
Debug.Print strKey
strVal = LTrim(RTrim(Mid(strLine, i + 1, Len(strLine) - i)))
Debug.Print strVal
Select Case UCase(strKey)
Case Is = "HOST"
wsTelnet.RemoteHost = strVal
Case Is = "PORT"
wsTelnet.RemotePort = strVal
End Select
End If
Next
Close #1
wsTelnet.Connect
Exit Sub
MissingIni:
MsgBox "Missing host.ini!"
Err.Clear
End SubPrivate Sub wsTelnet_Connect() wsTelnet.SendData txtData.TextEnd SubPrivate Sub wsTelnet_DataArrival(ByVal bytesTotal As Long) Dim strResponse As String wsTelnet.GetData strResponse
txtResponse.Text = txtResponse.Text + strResponse
txtResponse.SelStart = Len(txtResponse.Text)End SubPrivate Sub wsTelnet_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 "Network error! - " & DescriptionEnd Sub
Winsock1.Close
End If
Winsock1.RemoteHost = text1
Winsock1.RemotePort = 700
Winsock1.Connect
Winsock1.RemoteHost = text1
Winsock1.RemotePort = 700
Winsock1.Connect 在用一个时间控件 在一个周期后 检查一下