Is there a better way to use asynchronous TCP sockets in C++ rather than poll or select? -


i started writing c++ code uses sockets, i'd asynchronous. i've read many posts how poll , select can used make sockets asynchronous (using poll or select wait send or recv buffer), on server side have array of struct pollfd, every time listening socket accepts connection, adds array of struct pollfd can monitor socket's recv (pollin).

my problem if have 5000 sockets connected listening socket on server, array of struct pollfd of size 5000, since monitoring connected sockets way know how check if recv socket ready, looping through items in array of struct pollfd find ones revents equals pollin. seems kind of inefficient, when number of connected sockets because large. there better way this?

how boost::asio library handle async_accept, async_send, etc...? how should handle it?

what heck, go ahead , write answer.

i going ignore "asynchronous" vs "non-blocking" terminology because believe irrelevant question.

you worried performance when handling thousands of network clients, , right worried. have rediscovered c10k problem. when web young, people saw need small number of fast servers handle large number of (relatively) slow clients. existing select/poll type interfaces require linear scans -- in both kernel , user space -- across sockets determine ready. if many sockets idle, server can wind spending more time figuring out work doing actual work.

fast-forward today, have 2 approaches dealing problem:

1) use 1 thread per socket , issue blocking reads , writes. simplest code, in opinion, , modern operating systems quite @ letting idle threads sleep peacefully out of way without imposing significant performance overhead. in experience, approach works hundreds of clients; cannot how work thousands.

2) use 1 of platform-specific interfaces introduced tackle c10k problem. means epoll (linux), kqueue (bsd/mac), or completion ports (windows). (if think epoll same poll, again.) of these notify application sockets ready, avoiding wasteful linear scan across idle connections. there several libraries make these platform-specific interfaces easier use, including libevent, libev, , boost.asio. find of them invoke epoll on linux, kqueue on bsd, , on, whenever such interfaces available.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -