   How do I handle sockets and threads?
  I'm trying to create a small program that allows me to share files with friends. I've want to create a simple WinSock (2.0) framework that will allow me to connect to multiple friends, and send more then 1 file at a time. Basicly it works like this:1. Create listener socket2. Listener signals for new socket3. Create socket:3.1 More than 6 sockets open, tell client that it should try to contact me later3.2 Less than 6 sockets open, create a new thread anda new socket.Then I have a job manager that keeps track of all the things I and clients want to do (like sending files, chatting etc.)is this a good approach (One thread per socket)?, or is there a better way to do this?Thanks in advance,Max Henger   
  Hi,I once created actually a kind of generic server template class that worked the same way as you describe. However although it does make sense from a design few (and probably works quite well for six threads or so) It would probably give problems with huge amounts of threads.Currently I go for the following solution. I have one listener class (or more if needed) that just has a bunch of sockets classes in a list. The listener class is a wrapper around the select() function. (1)It goes through all the socket classes asking which one wants to select for read/write and or error. Then it blocks on listen(). When it falls through it calls ReadSelected/WriteSelected or Error (whatever is appropiate). After that it goes back to (1).A server socket would create a new socket class when read selected and add that to the listener class (the server self is also in the list). Since I call the listener from within a listener call I have to take special care for the list (the listener is iterating through the list and the server socket is then adding a class possibly invalidating an iterator).Note that the sockets all have flexible buffers so I can always write to the socket class.I think this scales better for large amount of sockets. However for 6 threads your approach should be fine.   ;Quote:Original post by MadMax1992is this a good approach (One thread per socket)?, or is there a better way to do this?On Windows, there exists a set of complementary technologies, the IO Completion ports and overlapped IO.These two are ideal for such applications, since they scale to tens of thousands of users by efficiently and asynchronously handling both network as well as disk IO.Their effective usage however is considerably more complex than blocking IO. Non-Windows systems offer similar facilities.For mostly IO based servers, thread per client will work for small numbers of users. But depending on what kind of features you intend to add, synchronization will become first bottleneck, followed by excessive memory usage. Chat servers in general are problematic with thread per user since they usually depend on large shared state.For C++, the boost::asio offers robust wrapper for this type of networking. Apparently, recent versions support asynchronous file operations as well, but I personally have not used it, so I cannot comment on it.Depending on your skill level, you might want to look at non-blocking sockets as well.   ;  I just started looking at non-blocking ports, with one thread per 40/50 sockets. I guess it's a matter of experimenting and profiling a bit. Thank you for your times.Oh, Antheus, do you have some links to places where I can begin with these IO completion ports and overlapped IO you were talking about (maybe a page on MSDN), because I can't seem to find it...   ;  The top link of this search is what you're looking for.