locked
Multiple TCP Socket connections RRS feed

  • Question

  • Hello,

     i am new to C# socket programming and i am creating an application where my application has to connect to multiple server's(Which are hardware controllers) and communicate with  them. All the server I.P address and port number's are stored in database,once the application is launched my application will iterate through the database table and start sending connection request to each server and once the connection is established,application has to start sending and receiving data from multiple servers. please provide me some information on how can i do this,i mean how can i handle multiple connections at the same time,how can i send to and receive from multiple servers at the same time.out of synchronous and asynchronous communication which one is good for my application,if at all i received some data from some server how can i identified that which server sends that data?

    i am completely lost. please give some sample code and information so that i can i have good design and start implementation.

    Tuesday, August 31, 2010 5:15 AM

All replies

  • What I have done in the past is I store the Socket object inside a List or a Dictionary. Depending on the code you are using, a Dictionary may be easier as you can set the Key to be the IP of the server.

    So create the object.

     

    //Dictionary method.
    
    public Dictionary<IPAddress, Socket> connections = new Dictionary<IPAddress, Socket>();
    
    //List method. public List<Socket> connections = new List<Socket>();

    Then once a connection has been established just use connections.Add();

     

    • Proposed as answer by StefonA Friday, September 3, 2010 1:36 AM
    Friday, September 3, 2010 1:36 AM
  • Wow! That's a lot of sample code. I'll give you an overview and see how it works for you.

    As StefonA said you do need a List or a dictionary (I think it's a preferance thing, some may argue). Create a method that uses Socket.ConnectAsync. On your Completed event accept the socket and store it in your dictionary. You can form all your connections with a few lines of code. Here's a quick example, not debugged so beware.

     

    private List<Socket> ConnectionList = new List<Socket>();
    
     public ClientConnection(string ipaddress,int port)
        {
    EndPoint ServerEndPoint = new IPEndPoint(IPAddress.Parse(ipaddress), port);
          Socket _tSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
          SocketAsyncEventArgs args = new SocketAsyncEventArgs();
          args.Completed +=new EventHandler<SocketAsyncEventArgs>(args_Completed);
          args.RemoteEndPoint = ServerEndPoint;
          _tSock.ConnectAsync(args);
         
    
        }
    
        private void args_Completed(object sender, SocketAsyncEventArgs e)
        {
          ConnectionList.Add ((Socket)sender) ;
          
                
        }
    
    Friday, September 10, 2010 1:44 AM