' initialize variables
Private Sub Form_Load()
txtRemoteIpAddress = "192.168.0.20"
txtRemotePort = "13"
txtSocketStatus = SocketStatus(tcpClient.State)
txtLocalPort = tcpClient.LocalPort
txtLocalIpAddress = tcpClient.LocalIP
txtLocalHostName = tcpClient.LocalHostName
End Sub
' operator wants to connect to the server
Private Sub cmdConnect_Click()
If (tcpClient.State = sckClosed) Then
' Invoke the Connect method to initiate a connection.
tcpClient.RemotePort = txtRemotePort
tcpClient.RemoteHost = txtRemoteIpAddress
tcpClient.Connect
txtRemoteHostName = tcpClient.RemoteHost
Else
MsgBox "ERROR: socket is not closed. Socket must be closed before opening a new connection"
End If
End Sub
Private Sub cmdDisconnect_Click()
'Disconnect from ClientProcedure
' operator wants to exit the program
tcpClient.Close
End
End Sub
' the server disconnected the connection
Private Sub tcpClient_Close()
tcpClient.Close
End Sub
' the server sent us data
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
tcpClient.GetData strData, vbString
txtOutput.Text = strData
End Sub
' winsock control encountered an error
Private Sub tcpClient_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)
Debug.Print "Server Error Number = " + Str(Number) + ", Description = " + Description
txtSocketStatus = Description
End Sub
' this timer simply updates the status of the socket control on the display
Private Sub Timer1_Timer()
txtSocketStatus = SocketStatus(tcpClient.State)
' IF the socket is stuck in the closing or error states THEN close the socket to reset it
If ((tcpClient.State = sckClosing) Or (tcpClient.State = sckError)) Then
tcpClient.Close
End If
End Sub