Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8401987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:56:30+00:00 2026-06-09T21:56:30+00:00

i have implemented my ClientSocket class from CAsyncSocket: class ClientSocket : public CAsyncSocket {

  • 0

i have implemented my ClientSocket class from CAsyncSocket:

class ClientSocket : public CAsyncSocket
{
    // this socket sends data back to "backSocket" which points to this only for 
    // testing but it can send data to other sockets like that too.
ClientSocket * backSocket;

    // store some data in backupData untill connection is established.
    StringBuilder * backupData;

public:

virtual void OnClose(int);
virtual void OnReceive(int);
ClientSocket(void);
bool ConnectToBACK();
virtual ~ClientSocket(void);
};

ClientSocket::ClientSocket(void)
{
// DONOT run to back !!! recursive calls otherwise.
backSocket = NULL;
backupData = NULL;
}

bool ClientSocket::ConnectToBACK()
{
if(this->backSocket != NULL)
    return true;

// just for debugging :)
this->backSocket = this;
return true;
}

ClientSocket::~ClientSocket(void)
{
this->Close();
if(this->backSocket)
{
    this->backSocket->Close();
    delete this->backSocket;
    this->backSocket = NULL;
}
}

void ClientSocket::OnClose(int nErrorCode)
{
if(this->backSocket != NULL)
{
    this->backSocket->Close();
}

CAsyncSocket::OnClose(nErrorCode);
}

void ClientSocket::OnReceive(int nErrorCode)
{
if(nErrorCode == 0)
{
    char *buffer = new char[2049];
    int bufLen = sizeof(buffer)/sizeof(buffer[0]);

    int received = this->Receive(buffer, bufLen-1, 0);
    if(received == SOCKET_ERROR)
    {
        return ;
    }

    if(this->ConnectToback())
    {
        if(backupData)
        {
            int backupLen;
            char *backup = backupData->ToString(&backupLen);
            this->backSocket->Send(backup, backupLen);
            delete backupData;
            delete [] backup;
            backupData = NULL;
        }

        this->backSocket->Send(buffer, received);
        delete buffer;
    }
    else
    {
        if(backupData == NULL)
        {
            backupData = new StringBuilder();
        }
        backupData->Insert(buffer, received);
    }
}

CAsyncSocket::OnReceive(nErrorCode);
 }

I have not associated any GUI to this as i thought that it would be good for no overheads.
I donot require it. I have also done AfxSocketIback() in main and from a thread started another ListeningSocket .

netstat -a shows proper binding at the port of ListeningSocket and status as Listening

// ListeningSocket inherits public CAsyncSocket

 void ListeningSocket::OnAccept(int nErrorCode)
 {
 #ifdef DEBUG
std::cout << "\nOnAccepting Proxy Server :)";
 #endif
if(nErrorCode == 0)
{
    ClientSocket *FromCliet = new ClientSocket();
    FromCliet->value = 100;
    if(this->Accept(*FromCliet, NULL, NULL))
    {
                    // Connection just has ClientSocket * client
        Connection * connection = new Connection(FromCliet);
        // a list<Connection *> is stored in ListeningSocket
                    this->clients.push_front(connection);
    }
    else
    {
        std::cerr << "\nFailed to accept connection from Client";
    }
}

CAsyncSocket::OnAccept(nErrorCode);
 }

When putting brakepoints in ListenSocket::OnAccept, it never comes here.

EDIT:

 static DWORD WINAPI StartListening(LPVOID param)
 {
       ListeningSocket *app = (ListeningSocket *)param;
   if(false == app->Create(7897, SOCK_STREAM, 31, "127.0.0.1"))
   {
     std::cerr << "\nCould not create\bind to port";
     delete app;
     return -1;
   }
   if(false == app->Listen())
   {
          std::cerr << "\nCould not listen";
      app->Close();
      delete app;
      return -1;
   }
   return 0;
 }

 int ListeningSocket::Start()
 {
      if(NULL == CreateThread(NULL,0, StartListening, (LPVOID)this,0, NULL))
   {
    return -1;
   }

  return 0;
 }

I have NOT made it like MFC Wizard solution. I have simple project and main().

 My ListeningSocket Class is Singletone Class:  
 class ListeningSocket : public CAsyncSocket
 {

    private:
static ListeningSocket * ListeningSocket;
std::list<Connection *> clients;
ListeningSocket(void);

    public:
// overrides
virtual void OnAccept(int);
virtual void OnClose(int);
static ListeningSocket * GetListeningSocket();
virtual ~ListeningSocket(void);
virtual void Close();
int Start(void);
    };
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:56:31+00:00Added an answer on June 9, 2026 at 9:56 pm

    CAsyncSocket class internally uses Windows messages for firing events. You need to create CAsyncSocket-derived class in a thread with message loop. In this case events will be called. Pseudo-code:

    // This function runs in the context of worker thread
    void MyClass::ThreadFunction()
    {
        mySocket.Create(...);    // creating CAsyncSocket-derived class
    
        // Run message loop.
        BOOL bRes = FALSE;
        MSG msg;
    
        while((bRes = GetMessage( &msg, NULL, 0, 0 )) != 0)
        { 
            if (bRes == -1)
            {
                break;
            }
            else
            {
                TranslateMessage(&msg); 
                DispatchMessage(&msg); 
            }
        }
    }
    

    To stop this thread, use PostQuitMessage function.

    Edit.
    I didn’t post all multi-threading details, assuming that you are familiar with them. Generally, CreateThread requires global function as parameter (or class static function). To call regular class method, use “this” as CreateThread parameter, which is passed as void* to global thread function. Cast it back to the class pointer and call regular class method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a jquery ui autocomplete with data from database. Right now what
I have implemented INotifyPropertyChanged to the following class public class FactoryItems : INotifyPropertyChanged {
I have implemented correctly bump's api, and added this code: - (void) configureBump {
I have implemented pagination to my data, but the problem is I only have
i have implemented galleryview in one screen named display_image.xml in which i have shown
I have these three classes: Command: package pack; public abstract class Command impements java.io.Serializable
I have implemented a simple linked list class and I would now like to
I have created this asynchronous client socket that connects to a server and keeps
I have implemented SSO authentication using the sourceforge spnego project . This is my
I have implemented one application in android which uses epublib to view .epub files.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.