Solved

CAsyncSocket - OnAccept method NOT CALLED

Posted on 2002-06-17
7
1,106 Views
Last Modified: 2012-05-04
If anybody knows, what could cause that

  the OnAccept method of the CAsyncSocket class is not called,

please help me!

My code is as follows:

/******************************************************/

CMySocket::CMySocket()
{
  if ( !Create( 80, SOCK_STREAM, FD_ACCEPT ) ) throw CMyException( 1, GetMyLastError() );
  if ( !Listen() ) throw CMyException( 2, GetMyLastError() );
}

CMySocket::~CMySocket()
{
  Close();
}

#if 0
BEGIN_MESSAGE_MAP(CMySocket, CAsyncSocket)
     //{{AFX_MSG_MAP(CMySocket)
     //}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif     // 0

void CMySocket::OnAccept( int nErrorCode )
{
  // HERE IS A BREAKPOINT
  AfxMessageBox( "REQUEST" );
 
  CAsyncSocket::OnAccept(nErrorCode);
}

char *CMySocket::GetMyLastError()
{
  switch ( GetLastError() )
  {
    case WSAEFAULT: return "WSAEFAULT";
    case WSANOTINITIALISED: return "WSANOTINITIALISED";
    case WSAENETDOWN: return "WSAENETDOWN";
    case WSAEINPROGRESS: return "WSAEINPROGRESS";
    default: return "???";
  }
}

/******************************************************/

Note: When I try to connect to the socket from Internet Explorer (watching the comunication on TCPView - something like dynamically refreshing netstat), the socket is listening and then connection is established, but - is I've told - the OnAccept method is never called :-(
Why???

Regards
Aegressy
0
Comment
Question by:aegressy
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 7086195
There is nothing wrong with the code that you have presented - it works in my simple app - how have you initialised the sockets I am assuming you used AfxSocketInit in you InitInstance method of your CWinApp derived class

You also might have had better response if you posted this to the MFC forum
0
 

Author Comment

by:aegressy
ID: 7087884
ShaunWilde,

  thanks for your response, yes, I've done the AfxSocketInit(), I can send you my code if you want - or if you tried this, would you be so kind and send me your program? (If so, my mail address is: aegressy@yahoo.com)

  A very similar code works fine in HTTPSVR sample from the MSDN lib. on the same machine, compiled in the same environment... And after two days I simply don't know what is wrong with this.

Thanks a lot!
Aegressy
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 7088411
please send your code shaun_wilde@hotmail.com
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 9

Accepted Solution

by:
ShaunWilde earned 75 total points
ID: 7089303
ahh - you don't have a message pump - the MFC socket classes are based on WINSOCK and require messages to be pumped in order for them to work

I changed your code slightly to be like this and OnAccept now gets called

try
{
  p_myServer = new CMyServer();

/*
               char cmd = 0;
               while ( ( cmd != 'q' ) & ( cmd != 'Q' ) )
               {
                   
               }
               */

               MSG msg;
               while (GetMessage(&msg,NULL,0,0))
                    DispatchMessage(&msg);
          }
0
 

Author Comment

by:aegressy
ID: 7090061
Thanks a lot!
You are absolutely totally right :-)

I didn't want to create a GUI interface for this applicatoin and I didn't know, that Console (!) Win32 applications don't have their message pumps. (I thought that one is attached to the window, that the process runs in but I now realize, that this is a nonsence...)

I would like to ask you, if there is another way how to add a message pump and not have to loop in the main function?

Thank you once more.
Regards
Aegressy
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 7090285
you can always spawn it off in it its own thread and control that from the main thread

you can create a GUI-less MFC app by having a hidden main window

an ATL exe server (or service) can have MFC support (there are MS-KB articles on how to do this) and no GUI - if you want to make one from the project wizard
0
 

Author Comment

by:aegressy
ID: 7090488
I've nothing more to say, than: thank you!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

778 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