Solved

CAsyncSocket - OnAccept method NOT CALLED

Posted on 2002-06-17
7
1,113 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

789 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