Solved

Connect to multiple Tcp Servers

Posted on 2010-09-20
2
492 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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