How do declare the first (4 sec.) timer and how do you add the event
handler?
I've tried to include all the relevent code without the extra bits... Let me
know if it isn't sufficient. (Also let me know if you want me to post again
with wrapping disabled)
uTimeout has a value of 4 at the start of the program. The timeout timer
(timTimeout) is not enabled until the CONNECT sub is called and is
succesful.
'Open a connection to a server to the specified address and port.
Public Function Connect(ByVal Server As String, ByVal port As Int16) As
Boolean
If uConnected Then 'If we are already connected we should disconnect first
Disconnect()
End If
Try 'Error trap in case connection fails
client = New TcpClient(Server, port) 'Create connection
'Attach our "DoRead" sub to handle incoming data.
client.GetStream.BeginRead(ReadBuffer, 0, ReadBufferSize, AddressOf
DoRead, Nothing)
Clear() 'Clear the display
MarkConnected() 'Set state of this control to connected (enable timeout
timer, set flags, etc.)
Return True 'No errors, so we were successful. Return TRUE.
Catch ex As Exception
'Error occurred while trying to connect
DisplaySystem("Error - '" & ex.Message & "'") 'Write error to console
MarkDisconnected() 'Mark this control as disconnected (disable timeout
timer, set flags, etc.)
Return False 'No connection so return FALSE
End Try
End Function
'This routine handles incoming data from our TCP "client" object
Private Sub DoRead(ByVal ar As IAsyncResult)
Dim BytesRead As Integer 'Number of bytes in buffer
Dim Data As String 'String read from buffer
Try 'Error trapping
BytesRead = client.GetStream.EndRead(ar) 'Get # of bytes available
If BytesRead < 1 Then 'If there is no data to read, we should assume
we're disconnected.
MarkDisconnected()
Exit Sub
End If
ResetTimeout() 'Reset the timeout timer since there is incoming data
'Convert incoming data to a string...
Data = Encoding.ASCII.GetString(ReadBuffer, 0, BytesRead)
DisplayIn(Data) 'Display incoming data to display
'Start a new asyncronous read into buffer
client.GetStream.BeginRead(ReadBuffer, 0, ReadBufferSize, AddressOf
DoRead, Nothing)
Catch ex As Exception
'Error occured
DisplaySystem("Error - '" & ex.Message & "'") ' Display error
MarkDisconnected() 'Set as disconnected since there was an error.
End Try
End Sub
'Send data to remote connection, appending a CR/LF to data. Return TRUE if
successful
Public Function SendLn(ByVal data As String) As Boolean
Return Send(data & Chr(10) & Chr(13))
End Function
'Send data to remote connection. Return TRUE if successful
Public Function Send(ByVal data As String) As Boolean
Try
Dim writer As New IO.StreamWriter(client.GetStream) 'Declare stream
writer
writer.Write(data) 'Send data to stream
writer.Flush() 'Flush stream to remote connection
DisplayOut(data) 'Send outgoing data to display
ResetTimeout() 'Reset timeout timer
Return True 'Success! Return TRUE
Catch ex As Exception
'Error occurred. Not successful.
DisplaySystem("Error - '" & ex.Message & "'")
MarkDisconnected() 'Assume disconnected because of error.
Return False 'Unsuccessful send. Return FALSE.
End Try
End Function
'Send incoming text to the display.
Private Sub DisplayIn(ByVal text As String)
rtbDisplay.SelectionColor = uInColor
rtbDisplay.SelectionFont = uInStyle
rtbDisplay.AppendText(text)
End Sub
'Send outgoing text to the display.
Private Sub DisplayOut(ByVal text As String)
rtbDisplay.SelectionColor = uOutColor
rtbDisplay.SelectionFont = uOutStyle
rtbDisplay.AppendText(text)
End Sub
'Send system text to the display.
Private Sub DisplaySystem(ByVal text As String)
rtbDisplay.SelectionColor = uSysColor
rtbDisplay.SelectionFont = uSysStyle
rtbDisplay.AppendText(text)
End Sub
'Send user text to the display. This is externally generated test.
Public Sub DisplayUser(ByVal text As String)
rtbDisplay.SelectionColor = uUserColor
rtbDisplay.SelectionFont = uUserStyle
rtbDisplay.AppendText(text)
End Sub
'Send user text to the display, followed by a carriage return.
Public Sub DisplayUserLine(ByVal text As String)
DisplayUser(text & Chr(13))
End Sub
'Close current connection. Return TRUE if close was normal.
Public Function Disconnect()
If uConnected = False Then
Return True 'We're already disconnected. Nothing to do.
End If
Try 'Error trap
client.Close() 'Try and close connection
client = Nothing 'Destroy our connection object
MarkDisconnected() 'Set state of this control to disconnected
Return True 'Close was normal. Return TRUE.
Catch ex As Exception
DisplaySystem("Error - '" & ex.Message & "'")
MarkDisconnected() 'There was an error during disconnect. Assume
disconnect.
Return False 'Normal close did not occur. Return FALSE.
End Try
End Function
'Set this control to disconnected state
Private Sub MarkDisconnected()
If uConnected Then 'Only do this if we aren't already in a disconnected
state
uConnected = False
timTimeout.Enabled = False 'Turn off timeout timer
DisplaySystem("Disconnected" & Chr(13))
RaiseEvent StateChanged(False, Me)
End If
End Sub
'Set this control to a connected state
Private Sub MarkConnected()
If Not uConnected Then 'Only do this if we aren't already connected
uConnected = True
timTimeout.Interval = uTimeout * 1000 'Set timeout interval for
timeout timer
timTimeout.Enabled = True 'Enable the timeout
timer
DisplaySystem("Connected..." & Chr(13)) 'Send CONNECTED to display
RaiseEvent StateChanged(True, Me) 'Raise event to indicate state
change.
End If
End Sub
'Clear display area
Public Sub Clear()
rtbDisplay.Clear()
End Sub
'Timeout has fired. We are disconnecting.
Private Sub timTimeout_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles timTimeout.Tick
Disconnect()
DisplaySystem("Application timeout. Disconnected.")
End Sub
'Reset the timeout timer
Private Sub ResetTimeout()
timTimeout.Enabled = False
timTimeout.Interval = uTimeout * 1000
timTimeout.Enabled = True
End Sub