RE: Missing error in WSAAsyncSelect event completion in x64 enviro

Tech-Archive recommends: Fix windows errors by optimizing your registry



Charles here is a snippet

Your last snippit is on the listening side. The problem is when the listener
is down on the server and the code is trying to connect to a downed listener.

Here is the client side setup....

if(WSAAsyncSelect(m_Socket, ghwnd, WMU_CONNECT, FD_CONNECT ) ==
SOCKET_ERROR)
throw h_errno;

PCONNECT pConnection;
pConnection = new CONNECT;
pConnection->s = m_Socket;
pConnection->pSessionObj = this;
connList.Add(pConnection);

sName.sin_family = AF_INET;
sName.sin_addr.s_addr = lAddr.s_addr;
sName.sin_port = htons((short)nPort);

if(WSAConnect(m_Socket, (LPSOCKADDR)&sName, sizeof(sName), NULL, NULL,
NULL, NULL) == SOCKET_ERROR && h_errno != WSAEWOULDBLOCK)
throw h_errno;


Here is the message loop....

LRESULT WINAPI StartWorkWndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM
lParam )
{
HANDLE hThread;
unsigned uThreadID;
SOCKET s;

_set_se_translator( SEH2CTrans );

try
{
switch( msg )
{
case WMU_STARTED:
EMail("Service Started", pIdentity);
break;

case WMU_STOPTHREAD:
{
WaitForSingleObject(hStartEvent, STARTUP_DELAY);
EMail("Service Shutdown", pIdentity);
DestroyWindow(hwnd);
}
break;


case WMU_CONNECT:
{
WSAAsyncSelect((SOCKET)wParam, hwnd, 0, 0);
PCONNECT p = connList.Find((SOCKET)wParam);
if(p)
{
WORD wError = WSAGETSELECTERROR(lParam);
CSession *pSessionObj = p->pSessionObj;
delete (PCONNECT)p;
CreateIoCompletionPort((HANDLE)(SOCKET)wParam, hIOCP,
(ULONG_PTR)pSessionObj, 0);

if(wError != ERROR_SUCCESS)
{
PIOOBJ pIO = (PIOOBJ)pCIOObjs->GetNextObj();
if(pIO && pSessionObj->pPeer != NULL)
{
pIO->Initialize();
pIO->Error = (DWORD)wError;
PostQueuedCompletionStatus(hIOCP, PROCESS_ERROR,
(ULONG_PTR)pSessionObj->pPeer, &pIO->ol);
}
else
PostQueuedCompletionStatus(hIOCP, ABORT_SESSION,
(ULONG_PTR)pSessionObj, NULL);

}
else
PostQueuedCompletionStatus(hIOCP, PROCESS_CONNECT,
(ULONG_PTR)pSessionObj, NULL);
}
else if(fDebug)
Report("NULL returned on search for PCONNECT", ERROR_NO_DATA);
}
break;


case WM_DESTROY:
ExitWorkThread(hwnd);
PostQuitMessage(0);
break;

default: return DefWindowProc( hwnd, msg, wParam, lParam );
}
}
catch(CSEHException e)
{
Report("Message Thread Exception", e.Code);
}

return 0;
}


wError is 0x0000 on the x64 OS and is 0x274D on a 32 bit OS.
--
Timothy Jewett
Jewettware@xxxxxxxxxxxxx


"Charles Wang[MSFT]" wrote:

Hi Timothy,
I am not sure if I totally understand your scenario. To let me better
understand your issue, could you please describe your issue more detailed
or send me (changliw_at_microsoft_dot_com) your test project for further
research?

My server side code snippets are as follows and it worked fine with
listenning set up:
---------------------------------------------------------------------------
int APIENTRY _tWinMain(...)
{...
WSADATA wsd;
SOCKET Listen;
SOCKADDR_IN InternetAddr;

WSAStartup(MAKEWORD(2,2), &wsd);
Listen = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);

WSAAsyncSelect(Listen,hWnd, WM_SOCKET, FD_ACCEPT|FD_WRITE|FD_READ);

InternetAddr.sin_family = AF_INET;
InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InternetAddr.sin_port = htons(5150);

bind(Listen, (PSOCKADDR) &InternetAddr,sizeof(InternetAddr));
listen(Listen, 5);
......
}

LRESULT CALLBACK WndProc(...)
{
......
SOCKET Accept;

switch (message)
{
case WM_SOCKET:
if (WSAGETSELECTERROR(lParam))
{
closesocket( (SOCKET) wParam);
break;
}

switch(WSAGETSELECTEVENT(lParam))
{
case FD_ACCEPT:
// Accept an incoming connection
Accept = accept(wParam, NULL, NULL);

// Prepare accepted socket for read,
// write, and close notification
WSAAsyncSelect(Accept, hWnd,
WM_SOCKET,FD_READ|FD_WRITE|FD_CLOSE);
break;

case FD_READ:
// Receive data from the socket in wParam
break;

case FD_WRITE:
// The socket in wParam is ready for sending
data
break;
case FD_CLOSE:
// The connection is now closed
closesocket( (SOCKET)wParam);
break;
}
break;
......
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
----------------------------------------------------------------------------
--------------

If you have any other questions or concerns, please feel free to let me
know. It is my pleasure to be of assistance.


Best regards,
Charles Wang
Microsoft Online Community Support
=====================================================
Get notification to my posts through email? Please refer to:
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications

If you are using Outlook Express, please make sure you clear the check box
"Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.


Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
======================================================
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
======================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================


.



Relevant Pages