我已经从 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);
};