Advertisement

Newbie: WSAAsyncSelect() Problem

Started by July 22, 2002 07:11 PM
8 comments, last by Evil Bill 22 years, 6 months ago
Hi, i''m trying to write a really simple chat program using Winsock 2. The problem is, i never recieve a message telling me that i can accept() or connect(). My code goes roughly like this: Startup:

Create sockListen
Bind sockListen to port
listen() on sockListen
WSAAsyncSelect(..., FD_ACCEPT)
 
When user clicks on "Connect":

Create sockConnect
WSAAsyncSelect(...,FD_CONNECT)
 
And this is my window proc:
  
LRESULT CChatDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
   switch(message)
   {
   case WM_SOCKET_ACCEPT: OnSocketAccept(); break;
   case WM_SOCKET_CONNECT: OnSocketConnect(); break;
   }
   return CDialog::WindowProc(message, wParam, lParam);
}
  
But i never recieve those messages. I''m using TCP/IP and a home network of 2 machines. Any ideas what i''m doing wrong? Any help is appreciated. Cheers, Steve Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
Ok, it works if i just call conect() instead of WSAAsyncSelect(...,FD_CONNECT), but obviously this means that it blocks my main app.
Any ideas whats wrong? Should i just use a thread for connecting?

Cheers, Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
Advertisement
hei mate

you can't tell mister client to connect the server with only using WSAASyncSelect(blah, FD_CONNECT);
hehe
you have to call connect() or somethign like this.
note that a call to WSAAsyncSelect(s,..) will make socket s non-blocking so if you do:

WSAAsyncSelect(s, ..., FD_CONNECT);
connect(s,...);

connect() will not block. and you'll get FD_CONNECT.
of course you have to check stuff in FD_CONNECT but you probably check it already

ok now please explain me what is this icon i used on my post, is it a hat??

May The Gzoo Be With You!
~Lord Gzoo

[edited by - Lord Gzoo on July 22, 2002 10:10:21 PM]
--Amir
You need to use WSAConnect()
It should return immediately and then you''ll get an FD_CONNECT message when the connection completes successfully.
Lord Gzoo: If i do that, then connect() returns WSAEWOULDBLOCK.

Anonymous Poster: Thanks, i''m trying that now.
The only thing is, i need to specify the target to connect to twice - once for WSAConnect, and then again, later for connect() - is this right?

Thanks for the help guys,
Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
ok some copy+paste from the sdk documentation for you sir

related to the connect() function:

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 possible scenarios:

(and i'll copy only one of 'em )

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

amazing, isn't it?

here's a piece of code i used once

    WSAAsyncSelect(mySocket, hWindow, WM_WINSOCK, FD_CONNECT);if (connect(mySocket, (SOCKADDR*)&addr, sizeof(addr)) == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK) throw 1;    


with this you'll get your FD_CONNECT. walla~!

May The Gzoo Be With You!
~Lord Gzoo

[edited by - Lord Gzoo on July 23, 2002 7:54:09 PM]
--Amir
Advertisement
Just skimmed the other replies, and I haven''t used async sockets, but one thing I recall. Isn''t all socket messages backpacked on some other message? I''ll look it up
Aren''t you supposed to do like this in the wndproc?
switch(msg){case WM_WSAASYNC:  {    // what word?    switch(WSAGETSELECTEVENT(lParam))    {    case FD_ACCEPT:      {        // check for an error        if (!WSAGETSELECTERROR(lParam))          return(FALSE);        // process message        sAccept =  accept(wParam, NULL, NULL);          return(0);      }    }  }} 

Grabbed that bit from Drew Sikora''s Programming with Asynchronous Sockets.
Lord Gzoo: Yeah, i thought that was the case. I''m just doing a connect() in a loop just now, until i get an error other than WSAEWOULDBLOCK. It might not be the best way to do it, but tis the way Stefan Hajnoczi does it in WinSock2 For Games.

CWizard: Yeah, i changed that around too because i just have the one WSAAsyncSelect() call.

Thanks, Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
connect() in a loop
hmmm....
it''s NOT a good way
you need only one connect() call.

do this thingy:
check the return value from connect()
if it''s SOCKET_ERROR be afraid.
if GetLastError() is WSAEWOULDBLOCK say:
"hahhhaha! and i thought it''s a real error!"
if the error is different from WSAEWOULDBLOCK it''s a REAL error and you should handle it amazingly.
but if it''s WSAEWOULDBLOCK? don''t do anything. you''ll get your FD_CONNECT.

trust winsock

May The Gzoo Be With You!
~Lord Gzoo
--Amir

This topic is closed to new replies.

Advertisement