Single threaded TCP server

Started by
11 comments, last by hplus0603 13 years, 4 months ago
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.
Advertisement
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.
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.

******************************************************************************************
Youtube Channel

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.

enum Bool { True, False, FileNotFound };
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???
I believe most MUDs actually use select(). Then again, most MUDs run on UNIX, not Windows.
enum Bool { True, False, FileNotFound };
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 to fdset
-select()
-for i=0 to nclient
--if socket 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)
Quote:Original post by Bow_vernon
-for i=0 to nclient
--add socket to fdset
-select()
-for i=0 to nclient
--if socket 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).
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.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
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...

This topic is closed to new replies.

Advertisement