Multithreaded TCP Socket server.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dusted
    New Member
    • Nov 2011
    • 3

    Multithreaded TCP Socket server.

    I'm working on a server program that will allow multiple clients to connect then send and receive the catch is that i don't want all the date from 1 client sent to every other client. I only want Client 1 to receive data from Client 2 or Client 3 if they send numbers that are in a certain range. my main problem right now is getting the server to accept multiple clients and send them any data back at all. i don't know if it's important but the clients do not disconnect after data is sent the server will have to keep the thread open and wait for more data, only closing the thread if the Client clicks a disconnect button.

    here is what i have so far (i left some parts out as they don't have anything to do with Clients connecting)
    Code:
    static void Main()
            {
    
            static TcpListener ServerSocket = new TcpListener(16487);
            static TcpClient clientSocket = default(TcpClient);
    
     while (true)
                {
                    if (blnAllowConnect == true)
                    {
                    ClientCounter += 1;
                    clientSocket = ServerSocket.AcceptTcpClient();
                    Console.WriteLine("Client :" + Convert.ToString(ClientCounter) + " has started!");
                    handleClinet client = new handleClinet();
                    client.startClient(clientSocket);
                    }
                }
            }
    
    public class handleClinet
        {
    
            TcpClient clientSocket;
            string clNo;
            public void startClient(TcpClient inClientSocket)
            {
                this.clientSocket = inClientSocket;
                Thread WorkThread = new Thread(TransferData);
                WorkThread.Start();
            }
            private void TransferData()
            {
                //Here is where I'm getting stuck,
                //I've tried a few different things
                //but none have quite worked out
            }     
        }
    as for the client side its basically just sends a String
    with something like 55 in it and waits for data back.
    the idea is that the data it gets back is only from Clients 10 numbers above or below 55.

    thank you for all the help I've been stuck trying different methods over the past couple days.
Working...