Hi all,
I am writing an application that is acting as
an web client, that needs to connect to
multiple servers. The web servers are small
embedded web servers that talk to a host
of other electronics. I need my app to
pole each web server to check for changes.
I can connect to the two device I have currently,
first one then the other.
However, if I then attempt to connect to the first
web server again it fails!
This is my code to connect to the server
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnConnect.Click
m_Client.Close()
m_Client = Nothing
m_Client = New TcpClient(IP_Addr, PORT_NUM)
If m_Client.GetStream.CanRead = False Then
Debug.WriteLine("Can't read from stream")
Else
Debug.WriteLine("Can read from stream")
End If
m_Client.GetStream.BeginRead(m_ReadBuffer, 0,
READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
End Sub
When the connection is remade no data is sent back to
DoRead and the connection is not made.
DoRead is used as a asynchronous reader for the stream.
Private Sub DoRead(ByVal ar As IAsyncResult)
Dim bytesRead As Integer
Dim message As String
Try
bytesRead = m_Client.GetStream.EndRead(ar)
If bytesRead < 1 Then
Debug.WriteLine("No bytes received on doRead")
Debug.Flush()
Else
message = Encoding.ASCII.GetString(m_ReadBuffer, 0,
bytesRead - 2)
ProcessCommands(message)
End If
' Start a new asynchronous read into readBuffer.
m_Client.GetStream.BeginRead(m_ReadBuffer, 0,
READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
Catch ex As SocketException
MessageBox.Show("DoRead: " & ex.Message &
ControlChars.CrLf & ex.ErrorCode)
Catch ex As Exception
MessageBox.Show("DoRear: " & ex.Message)
End Try
End Sub
Any help with why this is not working, or suggestions
as to how I can connect and disconnect as a tcpClient would
be most appreciated.
Many thanks,
Jason.