Sign in to follow this  
Bow_vernon

Single threaded TCP server

Recommended Posts

Bow_vernon    322
Hi uh umm, I've been rewriting my TCP wrapper library. I was using thread per client (plus WSAEventSelect and WSAWaitForMultipleEvents) but damn that's slow (My Client APP will NEVER go faster than 31FPS). Now I think if it's possible to make it single threaded. For the Client APP it's easy (as it's only one socket). But for server, how?? My friends suggested several solution:

1. Use non blocking socket and poll all client socket for data (recv until it says "WSAEWOULDBLOCK")
2. Use Async socket and tie them to Windows Message or use EventSelect. I dunno how to EventSelect for more than one socket per thread. How do I poll events for more than one socket?? how the WSAWaitForMultipleEvents works for more than one socket???
3. Use select(), still it's just the same as polling (well, it's selective polling) but it requires adding socket to a set and blah I hope to avoid them

My friend said that MUD-server works like no 1 or 3. And he said that polling 5000 sockets wont kill the server. I'd like to go to solution 2 but my school server PC uses Linux, but I'd like to support windows though(so I could test at home, then put it in the school server <hehe I sneak to the admin room once>).

Again, I'm not sure to use which one. Btw select() in windows is a hack (I guess I read that somewhere, as the first parameter is unused, included only for the sake of compatibility).

I need advice from u guys, I know some guys here are expert in those kind of things. And dont tell me to use IOCP, completion port or Use some existing library. Puh-lease, I'd love the experience I get by going thru the hard way.

Share this post


Link to post
Share on other sites
frob    44902
Your friend is correct.

Those solutions are sufficient and can scale for most situations where processing is minimal. The network bandwidth will only handle a few Mbps on a consumer-grade Internet connection. That is likely where you will reach saturation first.

Even a high-capacity Internet connection with several million bytes per second is tiny compared to the billions of processor cycles per second with multiple cores and with RAM transferring at potentially several hundred Gbps.

Please read the links within the forum FAQ, especially A1, A8, and A11.

Share this post


Link to post
Share on other sites
LancerSolurus    630
I use the second method, it's fairly easy. If you set it up with WSAAsyncSelect then the message will contain the socket and type.

The lParam will contain the type in the lower 16 bits (such as FD_ACCEPT) and the wParam contains the socket number which can be used to differentiate between clients.

The reason I use this method is there is no polling involved.

Share this post


Link to post
Share on other sites
hplus0603    11347
First, a link: Forum FAQ

Second, select() works fine with up to 64 sockets on Windows. It's not a "hack" on modern Windows, because it just translates to something akin to WaitForMultipleObjectsEx(). A socket on modern Windows is a handle like a file or anything else. However, FD_SET on Windows is not a bitfield like on UNIX, because handles are not "small integers" on Windows.

Share this post


Link to post
Share on other sites
Bow_vernon    322
So which one do you suggest? Im kinda confuse at the moment. But if MUD in the past worked like no 1 I'll definitely go that way. Since it wont get bigger than 100 I suppose. I dont like the double work using select(loop all socket to put and test 4 events). Is it true MUD worked like no 1???

Share this post


Link to post
Share on other sites
Bow_vernon    322
Oh damn, I wish I dont have to select. Well if it works that way, then I have no choice. Im writing a MUD server, and yeah hplus, all MUD server I found today run on UNIX (mostly). I havent found a windows portable version (damn, do they hate bilge gates??). And hey, they all have size < 16k (IMPRESSIVE!!)

So now Im looking for a simple single threaded chat server that use plain BSD socket function. If anyone knows a good example, please share. I think I'll post some of my search result here, too. Unh I wish I could use plain polling.

anyway, please correct me about how select() works:

-for i=0 to nclient
--add socket[i] to fdset
-select()
-for i=0 to nclient
--if socket[i] in fdset
---recv()

and for listening socket I have to
-put listening socket to fdset
-select()
-if listening socket in fdset
--accept()

am I right??Well that doesnt sound too bad(except for the double looping, once for putting socket, once for checking if socket in fdset)

Share this post


Link to post
Share on other sites
fitfool    113
Quote:
Original post by Bow_vernon
-for i=0 to nclient
--add socket[i] to fdset
-select()
-for i=0 to nclient
--if socket[i] in fdset
---recv()


Correct, but even better, put as many descriptors as you can in the set. Basically, poll 64 sockets at a time instead of just 1 (assuming you have that many connections).

Share this post


Link to post
Share on other sites
SimonForsman    7642
Quote:
Original post by Bow_vernon
Oh damn, I wish I dont have to select. Well if it works that way, then I have no choice. Im writing a MUD server, and yeah hplus, all MUD server I found today run on UNIX (mostly). I havent found a windows portable version (damn, do they hate bilge gates??). And hey, they all have size < 16k (IMPRESSIVE!!)


MUDs running on UNIX/Linux is mostly due to historical reasons, most popular MUDs ran on university servers (which ran UNIX) back in the days when Windows was nothing more than a graphical shell on top of DOS, porting the code from UNIX to Linux is trivial (in many cases all you need is a recompile), Writing a MUD server for Windows is fairly pointless since:

A) If all you intend to do is host a text based MUD Windows will eat far more resources than Linux. (Most of todays MUDs can run flawlessly on a 486 with a stripped down Linux distribution while the same machine would be unable to even boot any Windows version newer than Windows95 (and trust me, you don't want to run Windows95 on anything))

B) Given the hardware requirements of the average MUD server Windows also has a very high license cost (You are looking at server hardware in the $50-$100 range adding as much or more for the OS is silly)

That said though, most MUDs are fairly trivial to port to Windows and some MUDs allready have Windows ports. (CircleMUD is one) and in recent years there have even been a few Windows only MUD servers.

Share this post


Link to post
Share on other sites
Bow_vernon    322
So, youre saying that MUD server in windows is ridiculous?oh damn, Im not used to using linux, I wish to program in windows but run it in linux. well I believe it can be done. And umm I think I found a practical and robust way of using select()...I'll try it at weekend...

Share this post


Link to post
Share on other sites
SimonForsman    7642
Quote:
Original post by Bow_vernon
So, youre saying that MUD server in windows is ridiculous?oh damn, Im not used to using linux, I wish to program in windows but run it in linux. well I believe it can be done. And umm I think I found a practical and robust way of using select()...I'll try it at weekend...


It depends on what you're aiming at, Today there really isn't any problems with running a MUD in Windows other than the licensing costs for the OS, If you are hosting the server yourself and allready have a license for the machine in question that cost is irrelevant (Since you've allready payed it).

If you intend to rent a server going with Windows usually increase costs by $10-$120 per month (Varies between companies and Windows versions), pretty much all cheap hosts are Linux or BSD only.

Writing crossplatform software isn't that difficult and in the case of a MUD server the amount of platform specific code needed is quite small, i think Boost includes pretty much everything you need.

You could download a virtualization program to run Linux with for testing purposes. (VirtualBox is good and free and there are even premade images for various Linux distributions for it so that you don't have to bother with installing the OS)

Share this post


Link to post
Share on other sites
Bow_vernon    322
thx for your input. Id like to program it in my pc (windows) yet run it on my school server pc(suse linux).
I dont want to use third party library...I think I'll go my way (select + nonblocking + single threaded), almost all MUD server I've seen use epoll (not select)because they're UNIX...

Share this post


Link to post
Share on other sites
hplus0603    11347
Consumer versions of Windows don't work well for large servers, because of some mostly artificial limitations.
This is why there is a Windows Server version -- which costs a lot more to license. On the upside, I believe you get a copy of IIS and a copy of SQL Server with your Windows Server license.
But, when Ubuntu comes for free, can run as many sockets as you want, and comes with Apache and MySQL for free, it's kind-of hard to argue with that...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this