Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

IronDragon

Multithreading a server?

This topic is 6100 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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. =)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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. =)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!