By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,890 Members | 1,112 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,890 IT Pros & Developers. It's quick & easy.

Sockets with multiple servers

P: n/a
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 = Nothing

m_Client = New TcpClient(IP_Addr, PORT_NUM)
If m_Client.GetStream.CanRead = False Then
Debug.WriteLine("Can't read from stream")
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

bytesRead = m_Client.GetStream.EndRead(ar)
If bytesRead < 1 Then
Debug.WriteLine("No bytes received on doRead")
message = Encoding.ASCII.GetString(m_ReadBuffer, 0,
bytesRead - 2)
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,

Nov 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.