Sign in to follow this  
robert_p

Simple begginer question

Recommended Posts

I starting playing with sockets about 2 days ago and have some crappy hello world client/server code. I was about to make it support multiple clients but one thing bothers me a little. I was going to do 2 threads 1 for the accept and one for all the logged in clients, but I notice that every example I've looked at uses a thread for each client(I mean every single one). Am i just missing something here. It dosent seem like a bad method or anything but I'd like to avoid alot of threads everywhere.

Share this post


Link to post
Share on other sites
I believe you do need a child process for each client. I programmed some client server software in Linux and we had to fork() a new child process for every client that connected.

This is obviously necessary because you can't handle all the threads in one exe.

Anyone care to wrap this up?

ace

Share this post


Link to post
Share on other sites
You don't need a separate thread for each client, however that's the easiest way of doing it. You can use the select() function to check if there is data waiting to be received or if there is a client connecting, and only call the blocking functions when they are needed. You can read more at this site. The site is aimed at winsock, but it works the same way in other operating systems using sockets. You should especially take a look at the section about the "select()-based Server". It comes with source code too so it shouldn't be to hard to understand.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this