1 Replies - 200 Views - Last Post: 26 July 2013 - 06:00 AM Rate Topic: -----

#1 Hypermx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 72
  • Joined: 06-August 09

Question about threading and Sockets

Posted 26 July 2013 - 05:31 AM

Hello lovely dreamin coders :)


I have a question about threading and sockets.


Im currently writing a chat-a-like code, and my goal is for the server to support multiple cilents at the same time.
The chat should be able to support video and voice at some point, so keep the data traffic amount in your head.

What i want to happen is something like:

The server has a main thread where its listening for new clients.
If a client connect, make a new thread and handle all the new socket data transfer in the new thread. This would to my understanding make it easyer to support multiple clients.

I've written this so far, but im stuck :(

Server:
        static TcpListener tcpL = new TcpListener(System.Net.Dns.GetHostEntry("hostnamehere").AddressList[0], 6222);

        static void Main(string[] args)
        {
            tcpL.Start();
            Socket conChecker = tcpL.AcceptSocket();
            if (conChecker.Connected)
            {
                string rDest = conChecker.RemoteEndPoint.ToString();
                Thread newThread = new Thread(() => newCon(rDest));
                newThread.Start();
                
            }
        }

        static void newCon(string rDest)
        {
            Console.WriteLine(rDest + " connected!");
            Console.ReadLine();
        }

    }



My current problems/questions:
Console dont reuse the conChecker. As soon as the newCon thread is done it closes the application, instead of listing for another client.
Should i use the RemoteDestination as a tracker for allready existing threads with this client?

How would a professional coder, design this? Should i stop using sockets for data transfer and use something else?

I have more questions, but my head is cold after spending allday making server/client, so ill type them in as they come to my head.



Feel free to ask me any questions. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Question about threading and Sockets

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3534
  • View blog
  • Posts: 10,939
  • Joined: 05-May 12

Re: Question about threading and Sockets

Posted 26 July 2013 - 06:00 AM

If your look at your main thread, you are not doing anything to keep it going. When the main thread ends, it will go and also stop the application. You are not doing anything to wait for other connections and accepting them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1