[SOLVED]Winsock problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

[SOLVED]Winsock problem

Post 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?
Last edited by Joeyotrevor on Mon Apr 27, 2009 10:58 pm, edited 1 time in total.

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Winsock problem

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

Re: Winsock problem

Post by Joeyotrevor »

I used ioctlsocket() to make the sockets non-blocking.

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
User avatar
szdarkhack
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 61
Joined: Fri May 08, 2009 2:31 am

Re: [SOLVED]Winsock problem

Post 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 :)
Post Reply