Solved

WSAAsyncSelect not returning messages

Posted on 2001-07-25
7
586 Views
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?
0
Comment
Question by:MrBunny
  • 3
  • 3
7 Comments
 

Author Comment

by:MrBunny
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?
0
 

Author Comment

by:MrBunny
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.
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6320116
First
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++.

Second
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.

Third
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.

Fourth
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.

Fifth
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.

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

Let me know what you are trying to do.

Vin.








0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Expert Comment

by:Un1
ID: 6326661
True multithread VB source code winsock control:

http://www.banasoft.com/DownLoad/BNWinsck.exe
0
 
LVL 4

Expert Comment

by:VincentLawlor
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.

Vin.
0
 

Author Comment

by:MrBunny
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.  

thx,
-Mrbunny
0
 
LVL 4

Accepted Solution

by:
VincentLawlor earned 100 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.

Vin.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now