Using WSAsyncsockets
Jonas Almfeldt -- jonal@kuai.se
Tuesday, January 28, 1997
Environment: Win95, VC++ 4.2b
I have a question about connecting a WSAsync socket.
I keep getting the winsock error "WSA Would block" all the time when I try
to connect my socket to an IRC server. How ever this works just fine when I
use the old socket type instead of the WSAsync socket class.
My question is: how do I make a successful socket connection with a WSAsync
socket ?
I am using MSVC++ 4.2 Enterprise Edition under Windows 95.
/ Jonas Almfeldt
SCS.010@mch.scn.de
Thursday, January 30, 1997
[Mini-digest: 2 responses]
Hi,
The whole thing has to do with blocking and nonblocking use of sockets. In
blocking use of sockets any function call will return only after the whole
operation is completed. In nonblocking use of sockets the call will return
immediately irrespective of successfull completion of call or not. The
successfull completion of call is notified by a window message (in
CAsyncSocket, you have to override a notification function). e.g. let us see
the use of connect function. The connect function might take some time before
the connection is actually established with the remote PC (maybe a few
seconds). In blocking use of sockets this connect call will only return after
the connection is actually established. In nonblocking use of sockets the
connect call returns immediately irrespective of whether the connection is
actually made or not. If the connection is established TRUE is returned,
otherwise FALSE is returned. In case the connection will take some time to
establish, FALSE is returned and GetLastError() will give the error code
"WSAEWOULDBLOCK" and as soon as the connection is established the OnConnect
function will be called. The same is true will all other socket functions
like read, write etc..
So in your case you have to override the notification function OnConnect and
it will be called as soon as the connection is complete.
You can enable blocking or nonblocking mode by calling the function
AsyncSelect. If you use CAsyncSocket the default mode is nonblocking.
You can see the usefullness of nonblocking use of sockets if you do some
socket programming for Windows 3.1, which is not a preemptive multitasking
system and a blocking socket call can block the whole PC for some time. Also
nonblocking use of sockets is a wonderfull way to provide support for
multiple simultaneous connections without using threads (again the only way
in Windows 3.1).
Hope it helps,
Tarun Mehta.
-----From: Mario Contestabile
>My question is: how do I make a successful socket connection with a WSAsync
>socket ?
You may wish to check
http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/sock/src/wsapiref_4.htm
where it states:
"On a blocking socket, the return value indicates success or failure of the
connection attempt.
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.
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.
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."
mcontest@universal.com
| Вернуться в корень Архива
|