#### Archived

This topic is now archived and is closed to further replies.

# C++/MFC async sockets

This topic is 6020 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone. I've been having a bit difficulty with my server/client setup. Both run using MFC and have async setup. For some reason, it seems neither my client nor server are calling the user defined message when an event occurs (FD_CONNECT, FD_READ, etc). I've done all the correct error checking for bind, connect, listen. On the client side, connect() returns with a value of 0 meaning it's connected, but my custom message isn't being called. For example: In my client application, i have #define WM_WSAASYNC (WM_USER + 5) ... BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd) ON_WM_PAINT() ON_WM_CREATE() ON_MESSAGE(WM_WSAASYNC, WSAAsync) END_MESSAGE_MAP() ... WSAAsyncSelect(port, main_window_handle, WM_WSAASYNC, FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE); but, say, when connect() returns (with 0), WSAAsync() isn't being called I've also tested for sending/recieving data. I've confirmed data was sent, say from client to server, but again, server doesn't call WSAAsync. Anyone have any clue about this? Thanks in advance! Edited by - belgare on February 18, 2002 11:54:20 PM

##### Share on other sites
Does anyone have any clue what I''m talking about? Hope so, still stumped. As a test, I made the server into a regular ''ol non-blocking server in a console app. It shows that the client is connecting to it, but the client still doesn''t call my custom message WSAAsync() when it connects and when the server sends data to it. Still stumped =/

##### Share on other sites
Your code looks OK to me, except that you are using the identifier name "port" as the first parameter to WSAAsyncSelect. The first parameter for this function is a socket descriptor, not a port. Maybe you''ve got that stored in "port", but if so that is a horrible naming convention as those are two completely different things.

If your function isn''t being called then maybe there is something wrong in the message map. Check function declaration looks like this:

afx_msg LRESULT CYourApp::OnSocketRead(WPARAM wParam, LPARAM lParam);

A few minor quibbles... why call your function WSAAsync? That''s just going to cause confusion. And why select on every possible operation? Have handler functions for FD_READ, FD_CONNECT, etc. And then name them something informative like OnSocketRead() and OnSocketConnect(). Of course, I''m just biased because those are the names I used.

##### Share on other sites
I just thought of something, if I got my main_window_handle wrong, would that also cause the message to not be called? I''m obtaining (in the right way i hope) during this:

HWND main_window_handle;
...
BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();

main_window_handle = m_pMainWnd->GetSafeHwnd();
return TRUE;
}

Is this the wrong way to get the HWND? Oh, and are the OnSocketRead, OnSocketAccept, etc, supposed to be in the CWinApp derived class, or CFrameWnd derived class message maps? Thanks

##### Share on other sites
Uh, yea that would definitely stop the message from being sent to your app! It''s being sent to another window (and hence another message handler). Do not create another window.

The frame window already has an m_hWnd property which you do not need to specifically set, MFC does this for you. Just get it by doing some thing like:

this->GetSafeHwnd();

Hell, in my code I was just using WSAAsyncSelect(s, this->m_hWnd, ...). Obviously using GetSafehWnd is preferable since that function exists.

And of course whether or not you can do that is determined by where you handle the message. But judging by your message map, I''d say this is a safe bet.

##### Share on other sites
Yay!! Thank you SO much Pyabo! I''ve now got it fixed!

1. 1
2. 2
frob
15
3. 3
4. 4
5. 5

• 14
• 13
• 14
• 69
• 22
• ### Forum Statistics

• Total Topics
632138
• Total Posts
3004319

×