Simple begginer question

Started by
2 comments, last by hplus0603 18 years, 10 months ago
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.
Advertisement
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
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.
The Forum FAQ actually talks about this.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement