📄 listensocket.cpp
字号:
/****************************************************************/
/* */
/* LISTSOCKET.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. */
/* */
/* Programmed by Pablo van der Meer */
/* Copyright Pablo Software Solutions 2002 */
/* http://www.pablovandermeer.nl */
/* */
/* Last updated: 10 july 2002 */
/* */
/****************************************************************/
//在ListenSocket.cpp文件中实现侦听套接字的创建,该类用来侦听连接请求,
//当一个新的连接被请求,服务器将接受这个这个请求并为其创建一个连接线程用来处理此连接
#include "stdafx.h"
#include "FTPServerApp.h"
#include "FTPServer.h"
#include "ApplicationDlg.h"
#include "ListenSocket.h"
#include "ConnectThread.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CListenSocket::CListenSocket()
{
m_pWndServer = 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)
{
// 新的连接建立
CSocket sockit;
// 用一个临时的CSocket对象接收连接
Accept(sockit);
// 创建一个新的线程来处理连接
CConnectThread* pThread = (CConnectThread*)AfxBeginThread(RUNTIME_CLASS(CConnectThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
if (!pThread)
{
sockit.Close();
TRACE("Could not create thread\n");
return;
}
CFTPServer *pWnd = (CFTPServer *)m_pWndServer;
// 添加这个线程到列表中
pWnd->m_CriticalSection.Lock();
pWnd->m_ThreadList.AddTail(pThread);
pWnd->m_CriticalSection.Unlock();
// 保存指针
pThread->m_pWndServer = m_pWndServer;
// 把套接字句柄传送给这个线程
pThread->m_hSocket = sockit.Detach();
// 启动线程
pThread->ResumeThread();
CAsyncSocket::OnAccept(nErrorCode);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -