CAsyncSocket: OnAccept will not be called
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: CAsyncSocket: OnAccept will not be called

  1. #1
    Join Date
    May 2011
    Posts
    3

    CAsyncSocket: OnAccept will not be called

    Hiho,

    I want to make a simple example with CAsyncSocket Obeject. Just a simple Server which accepts a client und disconnect them. By I dont get it work.

    I wrote this little class

    Header
    Code:
    #pragma once
    
    //standard
    #include <list>
    #include <iostream>
    
    //os
    #include <afxsock.h>
    
    //projekt
    #include <EthernalCraft/McClientConnection.h>
    
    class McManager : public CAsyncSocket
    {
    	public:
    		McManager();
    		~McManager();
    
    		void Init(unsigned localPort, char* remoteHost, unsigned remotePort);
    
    		//event notification
    		virtual void OnAccept(int nErrorCode);
    
    	private:
    		unsigned localPort;
    		unsigned remotePort;
    		char* remoteHost;
    
    		std::list<McClientConnection*> connectionList;
    };
    Source
    Code:
    # include <EthernalCraft/McManager.h>
    
    McManager::McManager()
    {
    	
    }
    
    McManager::~McManager()
    {
    
    }
    
    void McManager::Init(unsigned int localPort, char *remoteHost, unsigned int remotePort)
    {
    	this->localPort = localPort;
    	this->remotePort = remotePort;
    	this->remoteHost = strdup(remoteHost);
    	
    	if(!Create(
    			localPort,
    			SOCK_STREAM,
    			FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT
    		))
    	{
    		std::cout << "CAsyncSocket::Create failed" << std::endl;
    	}
    
    	if(!Listen())
    	{
    		std::cout << "CAsyncSocket::Listen failed" << std::endl;
    	}
    
    }
    
    void McManager::OnAccept(int nErrorCode)
    {
    	AfxMessageBox("OnAccept");
    }
    I just want that my OnAccept Method is called, but it wont work and i downt know why

    greetings
    chrisliebaer

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Arrow Re: CAsyncSocket: OnAccept will not be called

    What you ask about is an MFC problem and these are discussed in the Visual C++ section: http://www.codeguru.com/forum/forumdisplay.php?f=7. So please post an inquiry over there (unless one of the moderators sees this first and simply moves the thread).

    Ah, and... Welcome to CodeGuru!
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    May 2011
    Posts
    3

    Re: CAsyncSocket: OnAccept will not be called

    Oh, sorry i clicked the wrong Forum. I wont post it twice, so maybe some mod can move it please.

  4. #4
    Join Date
    Jul 2002
    Posts
    2,521

    Re: CAsyncSocket: OnAccept will not be called

    This is well known CAsyncSocket problem. To call OnAccept, CAsyncSocket internally uses Windows messages, this requires message loop. In UI application, it is easy to create CAsyncSocket in the main application thread, and this thread message loop (supplied in UI thread by default) is used to call OnAccept.
    If you create CAsyncSocket in non-UI thread, add message loop to this thread and ensure that thread is alive and message loop is running all time when you want to work with this socket.

  5. #5
    Join Date
    Jul 2002
    Posts
    2,521

    Re: CAsyncSocket: OnAccept will not be called

    Quote Originally Posted by Eri523 View Post
    Ah, and... Welcome to CodeGuru!
    Code:
    if ( thread.Owner.PostCount <= thread.PostCount(thread.Owner) &&
         thread.PostCount(me) <= 0 ) 
    {
        response.Write("Ah, and... Welcome to CodeGuru!");
    }

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: CAsyncSocket: OnAccept will not be called

    Quote Originally Posted by Alex F View Post
    Code:
    if ( thread.Owner.PostCount <= thread.PostCount(thread.Owner) &&
         thread.PostCount(me) <= 0 ) 
    {
        response.Write("Ah, and... Welcome to CodeGuru!");
    }


    Actually the algorithm rather goes like this:

    Code:
    if ( thread.Owner.PostCount <= 1 &&
         thread.PostCount(me) <= 0 ) 
    {
        response.Write("Ah, and... Welcome to CodeGuru!");
    }
    Do you think there's something wrong (or silly) about that?
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Jul 2002
    Posts
    2,521

    Re: CAsyncSocket: OnAccept will not be called

    Quote Originally Posted by Eri523 View Post
    Do you think there's something wrong (or silly) about that?
    Your algorithm doesn't work for a new user who already posted more than once in the same question. I don't know whether this is wrong or your personal choice

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: CAsyncSocket: OnAccept will not be called

    Quote Originally Posted by Alex F View Post
    Your algorithm doesn't work for a new user who already posted more than once in the same question.
    Oops! In this case I apparently misunderstood the object model. I was assuming thread.Owner.PostCount is the total post count (in any thread) of the user who started the thread. Will hurry to fix the code...

    Or, if you mean that won't post a greeting if the user already did more than one post in his/her initial thread before I post: Yes, this is intentional.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  9. #9
    Join Date
    May 2011
    Posts
    3

    Re: CAsyncSocket: OnAccept will not be called

    Thanks for your help. I will try this.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center