Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Connect to multiple Tcp Servers

Posted on 2010-09-20
2
Medium Priority
?
505 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
[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
2 Comments
 
LVL 16

Accepted Solution

by:
kris_per earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

604 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