Solved

CAsyncSocket - OnAccept method NOT CALLED

Posted on 2002-06-17
7
1,098 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now