-
May 12th, 2011, 04:27 PM
#1
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
-
May 12th, 2011, 05:54 PM
#2
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.
-
May 13th, 2011, 01:27 AM
#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.
-
May 13th, 2011, 01:28 AM
#4
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.
-
May 13th, 2011, 01:35 AM
#5
Re: CAsyncSocket: OnAccept will not be called
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!");
}
-
May 13th, 2011, 02:59 AM
#6
Re: CAsyncSocket: OnAccept will not be called
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!");
}
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.
-
May 13th, 2011, 03:43 AM
#7
Re: CAsyncSocket: OnAccept will not be called
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
-
May 13th, 2011, 09:29 AM
#8
Re: CAsyncSocket: OnAccept will not be called
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! 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.
-
May 13th, 2011, 03:47 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|