Can i make a socket array with a max number of sockets as the initialization value |
Nothing's stopping you from doing that. You could even have a vector of sockets in case you don't know how many sockets you're going to initialize beforehand.
Also...can i have client connect to server on multiple sockets...The reason i ask is because it would be easier to send specific types of data over each socket rather then sending every type of data over one socket. |
Yes, you can have a client connected to the same server through multiple sockets, however this can get a bit dicey. On the server side, all it sees is an incoming connection, so you would have to setup some kind of identification process. You could have the client randomly give itself an ID (like a GUID: see below) at startup. When the client connects with the server with the first socket, the server would look up this ID and see if the ID has been used before. If not, the server would assign this particular socket to be the first socket from this client. When the client connects again through a different socket, the server would look up the ID and see that this client already has one socket open, so the server would assign this particular socket to be the second socket from this client, and so on.
Problems could occur of course if one client is using the ID of another. To circumvent this, you could have the server generate the ID. However, the handshaking process would be more involved because the client would have to then receive the ID. If the client already has an ID, it should send back to the server the fact that it already has an ID as well as what it is (if the client doesn't have an ID, it could send back -1 for example). Then, the server would be able to figure out that this is socket N from this particular client (if it received -1 then this would be connection 1).
You may find that this whole ID generation and look up business is too much overhead. If so, you could prepend a code to the message from the client, so that the server has a heads up as to what's coming. For instance, you could reserve the first 2 bytes of the message and have the code be an unsigned short (this will give you 65536 possible messages).
http://en.wikipedia.org/wiki/Globally_unique_identifier
also im not seeing how you would set up each socket as non blocking using WSAAsyncSelect() if you have 100's of sockets seeing as each call to WSAAsyncSelect cancels out the last call to it |
You answered your own question on that one (when you call WSAAsyncSelect you pass in a socket, and
that's the socket whose previous WSAAsyncSelect call gets cancelled).
also can you use WSAAsyncSelect() on client side? |
I'm unsure about this but my first instinct is you
can't. Using WSAAsyncSelect requires a call to listen, which in turn requires a bind, which means that it's a server socket. There may be other options out there that allow a client-side socket to use genuine non-blocking calls, however. Also, nothing's stopping you from having a "client" set up a server socket and having it tell the server the connection info of that socket through an already established socket (you'll be setting up a kind of P2P network in this case).
or is there a more effecient way to recv on client side without blocking than by using timer function. |
After the client establishes the socket, you can start a thread that just calls
recv
and then forwards the data in an infinite loop.