English 中文(简体)
Maintaining many socket connections with a single thread
原标题:
  • 时间:2009-11-18 08:43:19
  •  标签:
  • sockets
  • tcp

Many tutorials on socket communication I see seem to use 1 thread per socket. But on a server used for online gaming, you might have 10k concurrent users - 10k threads isn t probably a wonderful idea. I came across a tool (SmartFox) which claims to use a single thread for monitoring all socket connections, potentially thousands of them. This app happens to be in Java, but I figure C++ or C# could do the same... how would you achieve this?

问题回答

The C10K problem talks about this question.

Implement a queueing system with one thread polling the network and x threads acting as workers. You will need to implement a critical section around the code which dequeues and queues the connections.

If you are using C++, have a look at boost::asio.

Making your own is fun too, of course.

Since you mentioned C++...

If you re on a Windows platform then you should be looking at I/O Completion Ports for this kind of scalability. I/O Completion Ports allow you to perform asynchronous I/O on sockets (and other devices) using a small number of threads to service many thousands of I/O operations (i.e. connections).

The way this works is that the I/O Completion Port is, essentially, a queue but the operating system optimises how threads are released to work on work items within that queue to prevent too many threads being released at once and to ensure that a thread that has just been used is more likely to be used again. See here: http://msdn.microsoft.com/en-us/library/aa365198(VS.85).aspx for the MSDN information on IOCP and here: http://www.serverframework.com/products---the-free-framework.html for the source code to my free client/server framework that uses IOCP under the covers.

As an example of the scalability possible, in this blog posting (http://www.lenholgate.com/blog/2005/11/windows-tcpip-server-performance.html) I detail how I was able to achieve over 70,000 concurrent connections on a Windows Server 2003 machine with only 760MB ram.

Note that the C# async socket operations use IOCPs under the hood.





相关问题
Checking a local TCP port is not open in Java

Is there a simple way to make sure that a local port is not already open. Some TCP socket servers (eg Grizzly) don t seem to do this check by default. When this check is missing, the server appears ...

Client/Server: Integer always received as 1 (C-programming)

I m building a client and a server program that exchanges data over TCP and I m having trouble sending an ACK-confirmation from the server back to the client when an operation is successfull. I ve ...

Long Lived Persistent TCP Connection on the Android

I ve read some articles on the web and some questions on StackOverFlow, but no one seems to have a definite answer over a) If google uses Long Lived TCP connections for Gmail, Mail etc, and b) If ...

Maintaining many socket connections with a single thread

Many tutorials on socket communication I see seem to use 1 thread per socket. But on a server used for online gaming, you might have 10k concurrent users - 10k threads isn t probably a wonderful idea. ...

SSL_accept with blocking socket

I made a server with SSL and blocking sockets. When I connect with telnet (so it does not do the handshake), the SSL_accept blocks indefinitely and blocks every new handshake/accept (and by definition ...

How to list all devices in my wifi range in iphone

I am using the reachability code from apple to find if my iphone is connected to the wifi. Next i would like to list all the devices that are in my wifi range. How should i do this. The other devices ...

Sending struct over TCP (C-programming)

I have a client and server program where I want to send an entire struct from the client and then output the struct member "ID" on the server. I have done all the connecting etc and already managed ...

Idle tcp file descriptor after failed connect on HPUX

I have a client tcp socket (in c++) that has a loop where it retries to open a socket and connect to a server at a certain interval until it succeeds. A bug in the program caused close not to be ...

热门标签