📄 cnserversocketconnect.cpp
字号:
//---------------------------------------------------------------------------
#include "CNServerSocketConnect.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
// The CNSockConnect class implementation
// Method implementations
BOOL CNSockConnectThread::Init(VSocket *socket, CNServer *server)
{
// Save the server pointer
m_server = server;
// Save the socket pointer
m_socket = socket;
// Start the thread
m_shutdown = FALSE;
start_undetached();
return TRUE;
}
// Code to be executed by the thread
void *CNSockConnectThread::run_undetached(void * arg)
{
// Go into a loop, listening for connections on the given socket
while (!m_shutdown)
{
// Accept an incoming connection
VSocket *new_socket = m_socket->Accept();
if (new_socket == NULL)
break;
// Successful accept - start the client unauthenticated
m_server->AddClient(new_socket);
}
return NULL;
}
// The CNSockConnect class implementation
CNSockConnect::CNSockConnect()
{
m_thread = NULL;
}
CNSockConnect::~CNSockConnect()
{
m_socket.Shutdown();
// Join with our lovely thread
if (m_thread != NULL)
{
// *** This is a hack to force the listen thread out of the accept call,
// because Winsock accept semantics are broken
((CNSockConnectThread *)m_thread)->m_shutdown = TRUE;
VSocket socket;
socket.Create();
socket.Bind(0);
socket.Connect("localhost", m_port);
socket.Close();
void *returnval;
m_thread->join(&returnval);
m_thread = NULL;
m_socket.Close();
}
}
BOOL CNSockConnect::Init(CNServer *server, UINT port)
{
// Save the port id
m_port = port;
// Create the listening socket
if (!m_socket.Create())
return FALSE;
// Bind it
if (!m_socket.Bind(m_port, server->LoopbackOnly()))
return FALSE;
// Set it to listen
if (!m_socket.Listen())
return FALSE;
// Create the new thread
m_thread = new CNSockConnectThread;
if (m_thread == NULL)
return FALSE;
// And start it running
return ((CNSockConnectThread *)m_thread)->Init(&m_socket, server);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -