Page 1 of 1

[SOLVED]Winsock problem

Posted: Thu Apr 23, 2009 6:34 pm
by Joeyotrevor
I'm making a MUD in C++ and I'm using winsock for the networking. At first I had the server spawn a new thread for every connected client, but this would obviously be extremely slow if lots of people were to connect. I changed it so the Server class has an array of Clients, and in the main loop it loops through all of them, and they each call recv() on their respective sockets. It works fine when only 1 client is connected, but when there is more apparently recv() waits until a message is received, so the other clients have to wait for the first once to do anything before the server can get to their message.

Is there anyway to make this work without having to create a new thread for every client?

Re: Winsock problem

Posted: Fri Apr 24, 2009 3:55 pm
by MarauderIIC
Yes. Use an asynchronous receive [select()]. Also you'll probably want to make a thread to process incoming data.
Read Beej's Guide to Network Programming, there's a link to it in the Guides & Resources topic here in this forum.

Re: Winsock problem

Posted: Fri Apr 24, 2009 10:12 pm
by Joeyotrevor
I used ioctlsocket() to make the sockets non-blocking.

Re: [SOLVED]Winsock problem

Posted: Fri May 08, 2009 2:45 am
by szdarkhack
Also, as your server will have to do a lot of stuff quickly, while most of them block, make sure to use a Thread Pool. In case you're unfamilliar with the term, it just contains a number of threads that the rest of the server can use to call functions asyncronously. There are a few ThreadPool classes availible on the internet, however before you use it do make sure that you understand it's operation, it's very good practice in multithreaded development.

Finally, if you are using C++, you have to design the class inheritances very well beforehand, otherwise the object orientation will turn against you instead of helping :)