WSAAsyncSelect not returning messages

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?
Who is Participating?
VincentLawlorConnect With a Mentor Commented:
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.

MrBunnyAuthor Commented:
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?
MrBunnyAuthor Commented:
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.
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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.


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

MrBunnyAuthor Commented:
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.  

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.