Solved

Connect to multiple Tcp Servers

Posted on 2010-09-20
2
493 Views
Last Modified: 2012-06-27

Hello.

I want to connect to multiple TCP/IP servers at the same time.(Different IPs, same port)

When there is one server  my code is like:
TcpClient = new TcpClient()
client.GetStream().BeginRead(readBuffer, 0, 1024, newAsyncCallback(GetFromServer), null);

Now for multiple servers do i need to create  a TcpClient list ? Do i need to worry about thread safety ?

Frankly i could not decide how to design.
0
Comment
Question by:parabellum
  • 2
2 Comments
 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
ID: 33715483
To connect to multiple servers at the same time, you need to create multiple TcpClient instances in a thread.

Folllowing is a model using threads:
public class ServerIno

        {

            public string Address;



            // add any other server info here

        }



        [STAThread]

        static void Main()

        {

            List<ServerIno> listServerInfo = new List<ServerIno>();

            listServerInfo.Add(new ServerIno { Address = "10.10.10.10" });



            for(int s=0; s < listServerInfo.Count; s++)

            {

                Thread t = new Thread(DoWork);

                t.Start(listServerInfo[s]);

            }

        }



        public static void DoWork(object data)

        {

            ServerIno si = (ServerIno)data;



            int portNumber = 25; // use the correct port number here

            TcpClient client = new TcpClient(si.Address, portNumber);



            // use client object to send messages to server and get response from server here

        }

Open in new window

0
 
LVL 16

Expert Comment

by:kris_per
ID: 33715518

Above sample doesn't need to do anything special for thread safety. But depending on further requirement (that will make threads to update a shared variable), then you need to consider thread-safetry issues.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now