Solved

CAsyncSocket - OnAccept method NOT CALLED

Posted on 2002-06-17
7
1,085 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

16 Experts available now in Live!

Get 1:1 Help Now