0

我已经从 CAsyncSocket 实现了我的 ClientSocket 类:

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);
 }

我没有将任何 GUI 与此相关联,因为我认为它不会产生任何开销。我不需要它。我还在 main 中完成了 AfxSocketIback() ,并从一个线程开始了另一个 ListeningSocket 。

netstat -a 显示在 ListeningSocket 端口正确绑定,状态为 Listening

// ListeningSocket 继承公共 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);
 }

在 ListenSocket::OnAccept 中设置刹车点时,它永远不会出现。

编辑:

 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;
 }

我没有让它像 MFC 向导解决方案一样。我有简单的项目和 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);
    };
4

2 回答 2

2

我也遇到了同样的问题——有人重新实现了名为 CAsyncSocketEx 的 CAsyncSocket——构建为 CAsyncSocket 的函数替代品。

就在这周,我想看看我是否可以再次使用这段代码,但我遇到了同样的问题。由于异步窗口不是在具有消息循环的线程内创建的,因此事件没有从 WSAAsyncSelect() 触发...

看到这个: http: //support.microsoft.com/kb/90975

于 2013-01-31T01:39:37.917 回答
2

CAsyncSocket 类在内部使用 Windows 消息来触发事件。您需要在带有消息循环的线程中创建 CAsyncSocket 派生类。在这种情况下,将调用事件。伪代码:

// 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); 
        }
    }
}

要停止此线程,请使用 PostQuitMessage 函数。

编辑. 假设您熟悉它们,我没有发布所有多线程细节。通常,CreateThread 需要全局函数作为参数(或类静态函数)。要调用常规类方法,请使用“this”作为 CreateThread 参数,该参数作为 void* 传递给全局线程函数。将其转换回类指针并调用常规类方法。

于 2012-08-16T06:33:38.813 回答