none
Multithreaded Server

    Question

  • Hello everyone :)
    I'm currently learning networking in general, and I decided to choose C# as my learning platform.

    My aim at the moment is to create a multithreaded server:
    • One thread to listen for incoming connections
    • Other threads to handle the transfer with the clients
    My code so far has allowed a client to connect and a thread to be created, however I do not know how to make the "relay" part, where the thread will have its own connection/socket with the client. Here is my code:




    Server
    using System; 
    using System.Net; 
    using System.Net.Sockets; 
    using System.Threading; 
    using Core; 
     
    class Server 
        public static void Main() 
        { 
            TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 1337); 
            tcpListener.Start(); 
     
            while (true
            { 
                if (tcpListener.Pending()) 
                { 
                    Console.WriteLine("Incoming Socket Request"); 
                    TcpClient tcpClient = tcpListener.AcceptTcpClient(); 
                    Console.WriteLine("Accepted Socket Request (" + tcpClient.Client.RemoteEndPoint.ToString() + ")"); 
     
                    //This is the relay part I've been talking about, I need help here. 
                    Console.WriteLine("Transfer Socket Thread"); 
                    new Thread(new ParameterizedThreadStart(delegate { Individual(tcpClient.Client.RemoteEndPoint); })).Start(); 
     
                    tcpClient.Close(); 
                    Console.WriteLine("Closed Socket"); 
                } 
            } 
        } 
     
        //This is the relay part I've been talking about, I need help here. 
        public static void Individual(EndPoint Remote) 
        { 
            TcpClient MyClient = new TcpClient(); 
            Console.WriteLine(Remote.ToString()); 
        } 




    Client

    using
     System; 
    using System.Net; 
    using System.Net.Sockets; 
     
    class Client 
        public static void Main() 
        { 
            TcpClient tcpClient = new TcpClient(); 
            tcpClient.Connect("127.0.0.1", 1337); 
            tcpClient.Close(); 
        } 

    Thanks!
    -Lazlo
    Sunday, February 01, 2009 1:39 AM

Answers

  •  Study Classics.
    AlexB
    • Proposed as answer by Harry Zhu Wednesday, February 04, 2009 3:12 AM
    • Marked as answer by Harry Zhu Friday, February 06, 2009 3:53 AM
    Sunday, February 01, 2009 3:23 AM
  • You can put a wrapper class over your client Socket and in that class you can use Async Socket calls to get the work done you are trying to do. Creating threads in way which you are doing is not a good Choice and your application will suffer when a lot of clients are connected (ultimately a lot of threads created).

    Your best bet would be to use SocketAsyncEventArgs Class which has been specially added to .Net Framework 3.5 for implementing High Performance Socket Servers in .Net.

    Have a look at the class in MSDN, I hope this will help.

    Best Regards,
    RizwanShap
    Microsoft MVP - Visual Developer, C#
    • Proposed as answer by Harry Zhu Wednesday, February 04, 2009 3:12 AM
    • Marked as answer by Harry Zhu Friday, February 06, 2009 3:53 AM
    Sunday, February 01, 2009 1:24 PM
  • You need to pass the tcpClient to Individual(), not just its endpoint. The TcpClient returned by Accept is the new connection. (So, don't close it in your main thread, either).

           -Steve
    • Proposed as answer by Harry Zhu Wednesday, February 04, 2009 3:12 AM
    • Marked as answer by Harry Zhu Friday, February 06, 2009 3:53 AM
    Monday, February 02, 2009 2:14 PM

All replies

  •  Study Classics.
    AlexB
    • Proposed as answer by Harry Zhu Wednesday, February 04, 2009 3:12 AM
    • Marked as answer by Harry Zhu Friday, February 06, 2009 3:53 AM
    Sunday, February 01, 2009 3:23 AM
  • You can put a wrapper class over your client Socket and in that class you can use Async Socket calls to get the work done you are trying to do. Creating threads in way which you are doing is not a good Choice and your application will suffer when a lot of clients are connected (ultimately a lot of threads created).

    Your best bet would be to use SocketAsyncEventArgs Class which has been specially added to .Net Framework 3.5 for implementing High Performance Socket Servers in .Net.

    Have a look at the class in MSDN, I hope this will help.

    Best Regards,
    RizwanShap
    Microsoft MVP - Visual Developer, C#
    • Proposed as answer by Harry Zhu Wednesday, February 04, 2009 3:12 AM
    • Marked as answer by Harry Zhu Friday, February 06, 2009 3:53 AM
    Sunday, February 01, 2009 1:24 PM
  • You need to pass the tcpClient to Individual(), not just its endpoint. The TcpClient returned by Accept is the new connection. (So, don't close it in your main thread, either).

           -Steve
    • Proposed as answer by Harry Zhu Wednesday, February 04, 2009 3:12 AM
    • Marked as answer by Harry Zhu Friday, February 06, 2009 3:53 AM
    Monday, February 02, 2009 2:14 PM