Solved

Multiple TCP clients within a single app?

Posted on 2010-08-12
3
404 Views
Last Modified: 2012-06-21
Hey guys.  Can someone direct me to some sample code that demonstrates how to initiate multiple TCP clients within a single application?  Each client needs to connect with a different server, but I need to manage all of it within a one VB.Net app.

Thanks!
0
Comment
Question by:JasonWilliam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
arthurmnev earned 500 total points
ID: 33422463
Here is one example:http://www.eggheadcafe.com/articles/20020323.asp

The example shows the use of TCPClient class -- to create multiple connections, you'd create multiple instances of this class and connect those to multiple end points.

0
 
LVL 3

Expert Comment

by:arthurmnev
ID: 33422587

http://msdn2.microsoft.com/en-us/library/system.net.sockets.tcpclient.beginconnect.aspx

http://msdn2.microsoft.com/en-us/library/system.net.sockets.networkstream.beginread.aspx

This might get a little confusing if you don't fully understand the concept of
1) Async operations
2) Function pointers or delegates

Function pointer, is, essentially a variable that holds a reference to a function. The variable can be passed around and when executed it will execute the function to which it points.

Async operations in .net are usually based on the concept of passing a delegate / function pointer to a system managed function. When the system detects a condition, it triggers execution of your function.

To that end, when you begin doing BeginRead on a data stream from tcp client, you are telling the .net framework to execute your function when data shows up. This is important to understand if you have multiple connection streams all sending you the data..




' From http://social.msdn.microsoft.com/forums/en-US/netfxcompact/thread/d6156c86-6efc-429a-9ba5-510f5fba4c5a/
Public Class IQPSocket

    Dim IQPSocket As System.Net.Sockets.TcpClient
    Dim IQPStream As System.Net.Sockets.NetworkStream
    Dim DataBuffer(1024) As Byte
    Dim ByteRead As Integer

   Dim numberOfBytes As Integer



    Dim evtConnEstb As New AsyncCallback(AddressOf onConnectionEstablished)
    Dim evtDataArrival As New AsyncCallback(AddressOf DataProcessing)

    Dim IQPServer As String = My.Settings.iqp_svr
    Dim IQPPort As String = My.Settings.iqp_port

    Sub New()

        IQPSocket = New System.Net.Sockets.TcpClient

        ' IQPSocket.BeginConnect(IQPServer, IQPPort, evtConnEstb, Nothing)

         IQPSocket.Connect(IQPServer, IQPPort)

        Debug.Print("Conn:" & IQPSocket.Connected)
        IQPStream = IQPSocket.GetStream

        IQPStream.BeginRead(DataBuffer, 0, 1024, evtDataArrival, Nothing)

    End Sub


    Public Sub DataProcessing(ByVal dr As IAsyncResult)
        Debug.Print("DataArrival")

        numberOfBytes = IQPStream.EndRead(dr)

        ' At this instant DataBuffer variable is filled with the received data
        IQPStream.Flush()

        IQPStream.BeginRead(DataBuffer, 0, 1024, evtDataArrival, Nothing)
    End Sub

    'Public Sub onConnectionEstablished(ByVal dr As IAsyncResult)
    '    Debug.Print("Connect Test Eventzd")
    '
    'End Sub
'  ============================================

Open in new window

0
 

Author Comment

by:JasonWilliam
ID: 33425455
Yep.  That's the ticket.  Thanks Art!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET/VB: Convert Date and Time to YYYY-MM-DDTHH:MM:SS 3 50
how to make search between pages 17 60
Copying from a network share 3 38
Telerik RadEditor Control Save 8 40
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question