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
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! :)
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.
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.
Re: CAsyncSocket: OnAccept will not be called
Quote:
Originally Posted by
Eri523
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!");
}
Re: CAsyncSocket: OnAccept will not be called
Quote:
Originally Posted by
Alex F
Code:
if ( thread.Owner.PostCount <= thread.PostCount(thread.Owner) &&
thread.PostCount(me) <= 0 )
{
response.Write("Ah, and... Welcome to CodeGuru!");
}
:lol:
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?
Re: CAsyncSocket: OnAccept will not be called
Quote:
Originally Posted by
Eri523
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 :)
Re: CAsyncSocket: OnAccept will not be called
Quote:
Originally Posted by
Alex F
Your algorithm doesn't work for a new user who already posted more than once in the same question.
Oops! :o 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.
Re: CAsyncSocket: OnAccept will not be called
Thanks for your help. I will try this.