🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Multithreading a server?

Started by
12 comments, last by IronDragon 22 years, 5 months ago
Hi all, I am currently working on a game, which is coming along nicely. Basicly what I need to accomplish now is converting the current server over to a multithreaded one, where each client''s sending and receiving of data is it''s own thread. The problem is while I understand threads, I really have no clue how they work, nor how to implement one. If someone can post some code or a tutorial I would greatly appreciate it. Thanks!
Best,IronDragonShocking Games Game Centerwww.Shocking-Games.com
Advertisement
There are two multithreading tutorials over at flipCode. That''s probably a good place to start.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Hey thanks!

I was actually on the right track, but that tutorial cleared it up a ton. Heck, I might have this conversion done tonight. =)

Best,IronDragonShocking Games Game Centerwww.Shocking-Games.com
Ok can anyone answer this?

From my understanding now your CreateThread(...) function calls a ThreadProc. The thread runs until it returns from the ThreadProc.

The problem is I am trying to use this for a TCP/IP Socket. So I have to some how combine WM_ASYNC Messages from the main proc, with the ThreadProc. So how do I do this? I tried a while() loop but this makes the server extremely slow with just 1 person using it.

I am totally confused.... I've got the thread working, but if you don't use a while loop it exits, and if you use one it's slow. I've also been trying SuspendThread() and ResumeThread.

Another thing is I have tried calling the CreateThread(..) when the app receives a send, this way the thread only runs when it is actually handling data sends. This just seems to freeze the server entirely so that no data is sent/received at all!

Totally lost here, please help!



Edited by - IronDragon on February 3, 2002 6:45:35 AM
Best,IronDragonShocking Games Game Centerwww.Shocking-Games.com
You want to use either the Windows socket messages OR multithreaded socket programming, not both. As long as you''re just doing one thread per client, you can just create your sockets in blocking mode and do blocking I/O with the client. Blocking mode is very simple, if you''ve gotten the horrific Windows socket messages working you''ll have little trouble with it.

As a side note, you should use _beginthreadex() instead of CreateThread() if you''ll be using the C runtime library in the thread (this means printf(), rand(), pretty much any of the functions documented in your C book). Also, creating a thread per client won''t scale very well- you''ll start having performance problems with a relatively low number of clients, especially if your server is running on a non-NT system. It probably won''t make any difference for your application, but it''s something to keep in mind.


--
Eric
-- Eric
Unfortunately it *is* going to be a problem in my app. The game should be able to support 200+ users or so, and then from there on out it''s easily branched to spand across multiple servers.

While I do not know if the game will ever see that many users, we''ve already had 8 users on at a time during an early early beta. The game needs to be able to support as many users as possible, even if I have to spand it across multiple servers.

Perhaps i''ll just buy this multithreaded programming and windows socket book I found, supposedly it goes as detailed as MMORPG''s. Which while I don''t need that detailed, I do need this to be expandable and fast.

Also eventually the server is going to be put on linux (I think) so that''s something else I have to keep in mind.

Best,IronDragonShocking Games Game Centerwww.Shocking-Games.com
You may want to look into using events. There is a function similar to the message setting one except it sets an event object for the network event. Then you can just use WaitForSingleObject to wait for that event to trigger. This wait function is a much more efficient wait then a while loop, plus threads do not need to communicate outside of themselves for messages etc.

There is also something called IOCompletionPorts, but I know very little about them.

-Nate
Thanks for the suggestion...

I ordered a book today, hopefully that will solve my problems anyway. Plus it has some other topics I have seriously been wondering about, so hopefully that will solve it.

If anyone else has specific suggestions / ideas, they are always appreciated though. =)

Best,IronDragonShocking Games Game Centerwww.Shocking-Games.com
quote: Original post by IronDragon
Perhaps i''ll just buy this multithreaded programming and windows socket book I found, supposedly it goes as detailed as MMORPG''s. Which while I don''t need that detailed, I do need this to be expandable and fast.


Which Book? just out of interest?

Convict@Large
DanielB - Slayer of Bugs
Well, I was going to buy "Multiplayer Game Programming" by Andre Lamothe however I ended up getting "Developer's Guide to Multiplayer Games" by Andrew Mulholland.

I don't know if either one is better. From reviews I read it sounds like they are both great books. What tipped the scales for me is because the "Developers Guide to Multiplayer Games" also has info on mySQL databases and PERL programming (which are 2 of my strong points), however I didn't know how to use a mySQL database in C++. I already have multithreading and TCP/IP working, I just needed more info on it. So given the other topics I chose that one.

All though at one point I had both ordered I may still get the other, can never have enough resources when your programming.



Edited by - IronDragon on February 4, 2002 10:47:36 AM
Best,IronDragonShocking Games Game Centerwww.Shocking-Games.com

This topic is closed to new replies.

Advertisement