Click to See Complete Forum and Search --> : WSAAsyncSelect, Multiple sockets


paradoxresolved
August 4th, 2008, 06:29 AM
I'm trying to upgrade an old Windows application I created long ago so that it will now support up to 7 sockets for LAN use. I've been reading up on the Windows Sockets and I've seen numerous tutorials and samples. The one question that I haven't seen an answer to is this: Can you call WSAAsyncSelect once for each socket?

for example:

errorint1 = WSAAsyncSelect(Socket1, MyWindowHND, SOCKET1_EVENT, FD_READ | FD_WRITE);
errorint2 = WSAAsyncSelect(Socket2, MyWindowHND, SOCKET2_EVENT, FD_READ | FD_WRITE);
errorint3 = WSAAsyncSelect(Socket3, MyWindowHND, SOCKET3_EVENT, FD_READ | FD_WRITE);
errorint4 = WSAAsyncSelect(Socket4, MyWindowHND, SOCKET4_EVENT, FD_READ | FD_WRITE);
errorint5 = WSAAsyncSelect(Socket5, MyWindowHND, SOCKET5_EVENT, FD_READ | FD_WRITE);
errorint6 = WSAAsyncSelect(Socket6, MyWindowHND, SOCKET6_EVENT, FD_READ | FD_WRITE);
errorint7 = WSAAsyncSelect(Socket7, MyWindowHND, SOCKET7_EVENT, FD_READ | FD_WRITE);


Thanks all.

MikeAThon
August 5th, 2008, 09:51 AM
Can you call WSAAsyncSelect once for each socket?
I don't fully understand the question. Do you mean: "Is it enough to call WSAAsyncSelect only once per socket"? If so, then the answer is "yes".

I'm trying to upgrade an old Windows application I created long ago so that it will now support up to 7 sockets for LAN useWhat is the significance of the number 7 as a limit? In many applications (both client and server) there is no explicit limit or the limit is very high. For example, a client is permitted to make as many connections as it want s to as many servers as it wants, and a server is permitted to accept connections from as many clients as it can until errors (out of memory etc) start cropping up. A limit like "7" is odd.

Mike

paradoxresolved
August 5th, 2008, 01:23 PM
Well, I'm having trouble understanding how to deal with the messages that come from WSAAsyncSelect. By the time they reach the windows routing system and I handle the message, I don't know which socket the message came from. Can this be deciphered this from wParam or lParam??

The only thing I could think of was to assign 7 sockets ahead of time with their own message (SOCKET_EVENT1, SOCKET_EVENT2, etc.) and call WSAAsyncSelect for each. When my handler for OnSocketEvent1 was called, I would know that it involved the Client assigned to socket 1. I chose "7" sockets because 7 clients plus 1 server equals 8 computers, which is the amount I can support on my router.

However, before I could do any of this, I wanted to make sure I could make multiple calls to WSAAsyncSelect, privided that each call was for a different socket. I had some suspicions that multiple calls to WSAAsyncSelect negated previous calls, even if the other calls were for different sockets.

for example:

WSAAsyncSelect(socket_1, . . .); // socket_1 is WSAAsyncSelect()'ed
WSAAsyncSelect(socket_2, . . .); // socket_2 is WSAAsyncSelect()'ed, but now socket_1 is no longer WSAAsyncSelect()'ed


At any rate, can I resolve which socket the event is occuring on from lParam or wParam?

MikeAThon
August 5th, 2008, 03:22 PM
I'm having trouble understanding how to deal with the messages that come from WSAAsyncSelect. By the time they reach the windows routing system and I handle the message, I don't know which socket the message came from. Can this be deciphered this from wParam or lParam??
Yes, the identity of the socket is carried in the wParam for the message. You can use the wParam directly, in calls to Winsock functions, e.g.:

recv(wParam, ...);

The lParam encodes the network event and any error codes, specifically, the low word of lParam specifies the network event that has occurred, and the high word of lParam contains any error code.

See generally the documentation for WSAAsyncSelect at http://msdn.microsoft.com/en-us/library/ms741540(VS.85).aspx

You should use the macros WSAGETSELECTERROR and WSAGETSELECTEVENT to extract the network event and the error code.

paradoxresolved
August 6th, 2008, 08:44 AM
Excellent. Thanks MikeAThon!