multiple TCPclients to different servers

travisdh

New member
Joined
Nov 20, 2012
Messages
1
Programming Experience
Beginner
Hello,

I am quite new to using VB.net, and am trying to put together a solution based on examples (as i am still learning), anyway i am using the code example below to connect to a server and keep the connection open as an async connection so that when the server sends out data, the client receives it (and will process it later). Because my requirement is sort of backwards I am having trouble finding any code samples that would allow me to modify the code below (or any suitable code) to allow me to create a variable number of TCPclients based on the length of an array, that is I would store the IP address information in an array, and use that as the address a client would connect to, so whilst 192.168.45.20 is hard coded, the array would be "192.168.45.20","192.168.45.21","192.168.45.22" which would then create three separate TCPclients with one connected to each IP address,

So my question is, how would I best go about modifying this example to allow multiple clients to connect to multiple servers, and following on from that, how would i best identify which client an async received message has come from?
Appreciate any help anyone could provide a newbie who is impressed with sockets and TCPclient, but has a million more things to learn before he is proficient (understatement)

VB.NET:
Imports SystemImports System.Data
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
Imports System.Text
Imports System.ComponentModel
Imports System.Text.RegularExpressions

Module TcpClientConsole


    Private Stream As NetworkStream
    Public Sub Main()


        Dim Client As New TcpClient()


        Try
            ' Try to connect to the server on port 9090. 
            Client.Connect(IPAddress.Parse("192.168.45.20"), 9090)
            Console.WriteLine("* TCP Client *")
            Console.WriteLine("Connection established.")
            Console.WriteLine(New String("-", 40))
            Console.WriteLine()


            ' Retrieve the network stream. 
            Stream = Client.GetStream()


            ' Create a new thread for receiving incoming messages. 
            Dim ReceiveThread As New Thread(AddressOf ReceiveData)
            ReceiveThread.IsBackground = True
            ReceiveThread.Start()


            ' Create a BinaryWriter for writing to the stream. 
            Dim w As New BinaryWriter(Stream)


            ' Loop until the word QUIT is entered. 
            Dim Text As String
            Do
                Text = Console.ReadLine()


                ' Send the text to the remote client. 
                If Text <> "QUIT" Then w.Write(Text)


            Loop Until Text = "QUIT"


            ' Close the connection socket. 
            Client.Close()


        Catch Err As Exception
            Console.WriteLine(Err.ToString())
        End Try


    End Sub




    Private Sub ReceiveData()
        Dim myReadBuffer(515) As Byte
        Dim myCompleteMessage As StringBuilder = New StringBuilder()
        Dim numberOfBytesRead As Integer = 0


        ' Create a BinaryReader for the stream. 
        Dim r As New BinaryReader(Stream)


        Do
            ' Display any received text. 
            Try
                If Stream.DataAvailable Then


                    numberOfBytesRead = Stream.Read(myReadBuffer, 0, myReadBuffer.Length)
                    'recieve the message and store it as myCompleteMessage 
                    myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
                    Console.WriteLine("")
                    Console.WriteLine("")
                    Console.WriteLine(myCompleteMessage.ToString)
                    Console.WriteLine("")
                    Console.WriteLine("")
                    Console.WriteLine("")


                    Stream.Flush()
                    myCompleteMessage.Clear()
                End If


            Catch Err As Exception
                Console.WriteLine(Err.ToString())
            End Try
        Loop
    End Sub


End Module
 
That's really not so good. The TcpClient and NetworkStream classes already have asynchronous support built in. I suggest that you take a look at this:

Asynchronous TcpListener & TcpClient
 
Back
Top