tags:

views:

999

answers:

3

Hi,

i'm using this example implementation found at http://tangentsoft.net/wskfaq/examples/basics/select-server.html

This is doing most of what I need, handles connections without blocking and does all work in its thread (not creating a new thread for each connection as some examples do), but i'm worried since i've been told winsock will only support max 64 client connectios :S

Is this 64 connections true?

What other choices do I have? It would be cool to have a c++ example for a similar implementation.

Thanks

+6  A: 

Alternative library:

You should consider using boost asio. It is a cross platform networking library which simplifies many of the tasks you may have to do.

You can find the example source code you seek here.


About the 64 limit:

There is no hard 64 connection limit that you will experience with a good design. Basically if you use some kind of threading model you will not experience this limitation.

Here's some information on the limit you heard about:

4.9 - What are the "64 sockets" limitations?

There are two 64-socket limitations:

The Win32 event mechanism (e.g. WaitForMultipleObjects()) can only wait on 64 event objects at a time. Winsock 2 provides the WSAEventSelect() function which lets you use Win32's event mechanism to wait for events on sockets. Because it uses Win32's event mechanism, you can only wait for events on 64 sockets at a time. If you want to wait on more than 64 Winsock event objects at a time, you need to use multiple threads, each waiting on no more than 64 of the sockets.

The select() function is also limited in certain situations to waiting on 64 sockets at a time. The FD_SETSIZE constant defined in winsock.h determines the size of the fd_set structures you pass to select(). It's defined by default to 64. You can define this constant to a higher value before you #include winsock.h, and this will override the default value. Unfortunately, at least one non-Microsoft Winsock stack and some Layered Service Providers assume the default of 64; they will ignore sockets beyond the 64th in larger fd_sets.

You can write a test program to try this on the systems you plan on supporting, to see if they are not limited. If they are, you can get around this with threads, just as you would with event objects.

Source

Brian R. Bondy
+2  A: 

@Brian:

    if ((gConnections.size() + 1) > 64) {
     // For the background on this check, see
     // www.tangentsoft.net/wskfaq/advanced.html#64sockets
     // The +1 is to account for the listener socket.
     cout << "WARNING: More than 63 client "
       "connections accepted.  This will not "
       "work reliably on some Winsock "
       "stacks!" << endl;
    }

To the OP:

Why would you not want to use winsock2? You could try to look at building your own server using IOCP, although making this cross-platform is a little tricky. You could look at Boost::asio like Brian suggested.

Daniel
where is this code from? When do you select on more than 64 connections? You should instead use a threading model so you don't have to.
Brian R. Bondy
This code is from:http://tangentsoft.net/wskfaq/examples/basics/select-server.cppCan be found on the page that OP links to.
Daniel
+1  A: 

Before you decide that you need 'alternatives to winsock2" please read this: Network Programming for Microsoft Windows.

In summary, you DON'T need an 'alternative to Winsock2' you need to understand how to use the programming models supplied to full effect on the platform that you're targeting. Then, if you really need cross platform sockets code that uses async I/O then look at ASIO, but, if you don't really need cross platform code then consider something that actually focuses on the problems that you might have on the platform that you do need to focus on - i.e. something windows specific. Go back to the book mentioned above and take a look at the various options you have.

The most performant and scalable option is to use IO Completion Ports. I have some free code available from here that makes it pretty easy to write a server that scales and performs well on a windows (NT) based platform; the linked page also links to some articles that I've written about this. A comparison of my framework to ASIO can be found here: http://www.lenholgate.com/archives/000810.html.

Len Holgate