📄 listensocket.cpp
字号:
/****************************************************************/
/* */
/* LISTENSOCKET.CPP */
/* */
/* Implementation of the Listen Socket. */
/* The server listens for connections. When a new connection */
/* is requested, the server accepts the connection and then */
/* creates a connect thread to handle the connection. */
/* */
/* This class is a part of the Mini ASP Web Server */
/* */
/* Programmed by Pablo van der Meer */
/* Copyright Pablo Software Solutions 2003 */
/* This code is stolen from: http://www.pablovandermeer.nl */
/* */
/* Last updated: July 4, 2003 */
/* */
/****************************************************************/
#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"
#include "ListenSocket.h"
#include "ClientThread.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CListenSocket::CListenSocket()
{
m_pWndDlg = NULL;
}
CListenSocket::~CListenSocket()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CListenSocket, CAsyncSocket)
//{{AFX_MSG_MAP(CListenSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/********************************************************************/
/* */
/* Function name : OnAccept */
/* Description : Called by the framework to notify this listening */
/* socket that it can accept pending connection */
/* requests by calling the Accept member function. */
/* */
/********************************************************************/
void CListenSocket::OnAccept(int nErrorCode)
{
// New connection is being established
CSocket sock;
// Accept the connection using a temp CSocket object.
Accept(sock);
// Create a thread to handle the connection. The thread is created suspended so that we can
// set variables in CConnectThread before it starts executing.
CClientThread* pThread = (CClientThread*)AfxBeginThread(RUNTIME_CLASS(CClientThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
if (!pThread)
{
sock.Close();
TRACE("Could not create thread\n");
return;
}
CServerDlg* pDlg = (CServerDlg*) m_pWndDlg;
// set members of CClientThread.m_socket
pThread->m_ClientSocket.m_pCriticalSection = &(pDlg->m_CriticalSection);
pThread->m_ClientSocket.m_strHomeDir = pDlg->m_strWebPages;
pThread->m_ClientSocket.m_strCurrentDir = pDlg->m_strWebPages;
pThread->m_ClientSocket.m_strDefaultPage = pDlg->m_strDefaultPage;
pThread->m_hWndOwner = m_pWndDlg->GetSafeHwnd();
pDlg->m_CriticalSection.Lock();
// since everything is successful, add the thread to our list
pDlg->m_ThreadList.AddTail(pThread);
pDlg->m_CriticalSection.Unlock();
// Pass the socket to the thread by passing the socket handle. You cannot pass
// a CSocket object across threads.
pThread->m_hSocket = sock.Detach();
// Now start the thread.
pThread->ResumeThread();
CAsyncSocket::OnAccept(nErrorCode);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -