Thread: Socket Programming (Handing mulitple connections help)

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    35

    Socket Programming (Handing mulitple connections help)

    I have created a simple basic program that uses sockets but it can only handle one connection, how do I make it so it handles multiple connections?

    Server code:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Sockets;
    using System.Net;
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                string input = "";
                string message;
                byte[] buffer=new byte[250];
                IPHostEntry local = Dns.GetHostByName(Dns.GetHostName());
                IPEndPoint iep = new IPEndPoint(local.AddressList[0],8000);
                Socket newServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                newServer.Bind(iep);
                newServer.Listen(10);
                Socket newClient = newServer.Accept();
    
    
    
                if (newClient.Connected)
                {
                    newClient.Receive(buffer);
                    message = Encoding.ASCII.GetString(buffer);
                    Console.WriteLine(message);
                    newClient.Send(Encoding.ASCII.GetBytes("Hello client"));
    
                }
                
    
    
    
                newClient.Close();
    
               
            }
        }
    }


    client code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Sockets;
    using System.Net;
    
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                int number=1;
                string messageClient;
                byte[] bufferClient=new byte[200];
                IPAddress ip = IPAddress.Parse("192.168.30.127");
                IPEndPoint ie = new IPEndPoint(ip, 8000);
                Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                client.Connect(ie);
                
                    if (client.Connected)
                    {
                        client.Send(Encoding.ASCII.GetBytes("hello server"));
                        client.Receive(bufferClient);
                        messageClient = Encoding.ASCII.GetString(bufferClient);
                        Console.WriteLine(messageClient);
                    }
                
               
    
                client.Close();
               
            }
        }
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you need to put your call to accept() inside a while loop, and spawn a thread for each new connection that you receive.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    Quote Originally Posted by Elkvis View Post
    you need to put your call to accept() inside a while loop, and spawn a thread for each new connection that you receive.

    ahh okay thanks, what do you mean by thread though, if i wanted to keep track of each single connection could i put them in a dictionary or list of sockets?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by shivam1992 View Post
    ahh okay thanks, what do you mean by thread though
    a thread is a lightweight process. look at the System.Threading.Thread class.

    Quote Originally Posted by shivam1992 View Post
    if i wanted to keep track of each single connection could i put them in a dictionary or list of sockets?
    you certainly could, but if you're using threads as I suggest, make sure to follow good thread safety practices, so that only one thread tries to access the collection of sockets at once.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    If you're intending to have many clients connecting to your server at the same time I recommend you avoid "thread per client" as it will end up slowing your computer down. Instead you could consider a non-blocking solution which would forego the need for additional threads at all.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    a single-threaded non-blocking model may be better for performance on the server machine, but the perceived performance by the clients could be much much worse. if two clients try to make a request at the same time, one will have to wait for the other while it finishes this could be unpleasant for the clients on long-running requests. one possible solution for this is to spawn a thread for each request, but monitor the raw sockets with a polling loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About Device Driver Programming And Socket Programming
    By pritesh in forum Linux Programming
    Replies: 6
    Last Post: 01-23-2010, 03:46 AM
  2. Best programming practices for multiple connections
    By vasillalov in forum Networking/Device Communication
    Replies: 8
    Last Post: 10-09-2007, 10:14 PM
  3. Mulitple Inheritance
    By DMJ in forum C++ Programming
    Replies: 1
    Last Post: 08-09-2004, 11:48 AM
  4. Mulitple I/O streams
    By thamisfit in forum C Programming
    Replies: 3
    Last Post: 03-22-2003, 04:15 PM
  5. searching a string for mulitple characters
    By cxs00u in forum C++ Programming
    Replies: 1
    Last Post: 04-27-2002, 03:46 PM