[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


WSAAsyncSelect not returning messages

Posted on 2001-07-25
Medium Priority
Last Modified: 2013-11-13
I am trying to use the wsock32.dll to connect, and I am having problems with not receiving messages.  I call:

rc = WSAAsyncSelect(socket, sockets.txtSock.hWnd, &H202, FD_CONNECT)

to register my little textbox as wanting to receive messages, and then:

rc = WSAStartup(&H101, StartupData)
Call ioctlsocket(socket, FIONBIO, 1)
rc = connect(socket, socketbuf, Len(socketbuf))

which results in rc = WSAEWOULDBLOCK
so i know it is at least thinking about connecting

but my callback(?) procedure:

Private Sub txtSock_MouseUp(iMessageType As Integer, Shift As Integer, X As Single, Y As Single)

never gets any messages at all :(

I looked at the packets, and it seems:
I send a SYN packet out
then I get a ACK + SYN packet back
I send a ACK packet out
(which seems to be exactly the correct packet sequence for a TCP connection, i watched some other programs do the same thing, right before sending thier login/password packets)

so why am i not getting any notification of connection back?
Question by:MrBunny
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Author Comment

ID: 6319921
I saw some sample code where they called the WSAAsyncSelect like 10 times a second, not just for connect, but also for READ and CLOSE.  why would anyone do that?  does WSAAsyncSelect wear off or something?

Author Comment

ID: 6319933
Also, does anyone know what the 3rd argument is exactly?
Sometimes it is: &H202 or &H208 or &H205?  Those might correspond with vbLeftMouseButton/rightMB/middleMB, but I'm not sure.

Expert Comment

ID: 6320116
Why are you using the WSA commands ?
Why not use the winsock control that comes with VB. All this stuff is wrapped nicely for you already.

This type of socket programming is best left to multithreaded programs like VC++.

The WSAAsyncSelect and WSAEventSelect functions automatically set a socket to nonblocking mode. If WSAAsyncSelect or WSAEventSelect has been issued on a socket, then any attempt to use ioctlsocket to set the socket back to blocking mode will fail with WSAEINVAL. To set the socket back to blocking mode, an application must first disable WSAAsyncSelect by calling WSAAsyncSelect with the lEvent parameter equal to zero, or disable WSAEventSelect by calling WSAEventSelect with the lNetworkEvents parameter equal to zero.

WSAStartup should be called before issuing any other WSA commands.
The WSAStartup function must be the first Windows Sockets function called by an application or DLL. It allows an application or DLL to specify the version of Windows Sockets required and to retrieve details of the specific Windows Sockets implementation. The application or DLL can only issue further Windows Sockets functions after a successfully calling WSAStartup.

With a nonblocking socket, the connection attempt cannot be completed immediately. In this case, connect will return SOCKET_ERROR, and WSAGetLastError will return WSAEWOULDBLOCK. In this case, there are three different steps you can take:

1. Use the select function to determine the completion of the connection request by checking to see if the socket is writeable.
2. If the application is using WSAAsyncSelect to indicate interest in connection events, then the application will receive an FD_CONNECT notification indicating that the connect operation is complete (successfully or not).
3. If the application is using WSAEventSelect to indicate interest in connection events, then the associated event object will be signaled indicating that the connect operation is complete (successfully or not).
Until the connection attempt completes on a nonblocking socket, all subsequent calls to connect on the same socket will fail with the error code WSAEALREADY, and WSAEISCONN when the connection completes successfully. Due to ambiguities in version 1.1 of the Windows Sockets specification, error codes returned from connect while a connection is already pending may vary among implementations. As a result, it is not recommended that applications use multiple calls to connect to detect connection completion. If they do, they must be prepared to handle WSAEINVAL and WSAEWOULDBLOCK error values the same way that they handle WSAEALREADY, to assure robust execution.

The reson for calling the WSAAsynchSelect continuously is to wait for the connection to establish or for data to be read or written to the socket.

&H202 is Left button up
&H208 is middle button up
&H205 is Right button up

Let me know what you are trying to do.


Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Expert Comment

ID: 6326661
True multithread VB source code winsock control:


Expert Comment

ID: 6326724
UN1 this stuff is not freeware it costs $20 to register it and the winsock control that comes with VB does the job.


Author Comment

ID: 6330661
Vin, you are a badass.

Sorry for the delay, i was working in Lake Tahoe (on vacation, no less), and my NIC died :(

Vin, my goal is to create a multi-threaded app that downloads files from multiple sources simultaneously.  After see how painful doing it "the right way" with system calls, I have respect'd the project, and am going to try and complete the tasks with lighter/easier protocols.  In particular, I'm going to use a great FTP control from Mabry's to handle most of the file transfer, and the communication will be more UDP based from a winsock control.  

I had been hesitant to use a winsock control because they USED to have a multiple incoming event issue (fixed), and most of the sample code using winsock controls was written terribly.  The "good" sample code used APIs, but perhaps that is just because the people using APIs by definition have coded a lot, and thus tend to be better at writing code.

I really appreciate the detail you gave in your answers.  Each of the 6 points taught me something :)

I'm planning on implementing these modules a little bit at a time, and then incorporating them as they work out.  If you happen to have any nicely designed code (with comments :) i would love to see how a winsock control is properly used.  


Accepted Solution

VincentLawlor earned 400 total points
ID: 6333539
The winsock control works fine in VB for simple applications that only need to send and receive data synchronously. Usually client side comms with a single thread of execution. When you go into server side comms where you can have multiple clients connecting to a socket at the same time VB becomes pretty unusable as there are multiple threads of execution and the only way to deal with then would be to either syunchronise the socket connections using a Queue or try to multithread your VB application (arrrgh).

My recommendation would be to use VC++ if you know it to write the multithreaded stuff.

As to some sample code I'll see what I can find I usually write this type of stuff in C++ wrap it as a COM object and then use it from VB.


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question