⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datasocket.cpp

📁 这时一个ftp服务器,可以根据这个小的服务器原型做出更大的服务器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
					Close();
					m_pConnectSocket->SendResponse("450 can't access file.");
					// destroy this socket
					AfxGetThread()->PostThreadMessage(WM_THREADMSG, 0, 0);
					// upload failed
					((CConnectThread *)AfxGetThread())->UpdateStatistic(FTPSTAT_UPLOADFAILED);
				}
				break;
		}
	}
	CAsyncSocket::OnConnect(nErrorCode);
}


/********************************************************************/
/*																	*/
/* Function name : CDataSocket::OnClose								*/
/* Description   : Called by the framework to notify this socket	*/
/*				   that the connected socket is closed.				*/
/*																	*/
/********************************************************************/
void CDataSocket::OnClose(int nErrorCode) 
{
	TRACE("CDataSocket() OnClose()\n");
	if (m_pConnectSocket)
	{
		// shutdown sends
		ShutDown(1);

		if (m_nStatus == XFERMODE_RECEIVE)
		{
			while(Receive() != 0)
			{
				// receive remaining data				
			}
		}
		else
		{
			m_pConnectSocket->SendResponse("426 Connection closed; transfer aborted.");
			// destroy this socket
			AfxGetThread()->PostThreadMessage(WM_THREADMSG, 0, 0);
			// upload failed
			((CConnectThread *)AfxGetThread())->UpdateStatistic(FTPSTAT_UPLOADFAILED);
		}
	}
	m_nStatus = XFERMODE_IDLE;	
	m_bConnected = FALSE;
	CAsyncSocket::OnClose(nErrorCode);
}


/********************************************************************/
/*																	*/
/* Function name : CDataSocket::OnAccept							*/
/* Description   : Notify this listening socket that it can accept	*/
/*				   pending connection requests.						*/
/*																	*/
/********************************************************************/
void CDataSocket::OnAccept(int nErrorCode) 
{
	CAsyncSocket socky;
	Accept(socky);
	
	SOCKET socket = socky.Detach();
	Close();

	Attach(socket);

	m_bConnected = TRUE;
	m_pConnectSocket->SendResponse("150 Connection accepted");

	CAsyncSocket::OnAccept(nErrorCode);

	m_bPassiveMode = TRUE;
}


/********************************************************************/
/*																	*/
/* Function name : CDataSocket::GetStatus							*/
/* Description   : Get socket status.								*/
/*																	*/
/********************************************************************/
int CDataSocket::GetStatus()
{
	return m_nStatus;
}


/********************************************************************/
/*																	*/
/* Function name : CDataSocket::PrepareSendFile						*/
/* Description   : Prepare socket to send a file.					*/
/*																	*/
/********************************************************************/
BOOL CDataSocket::PrepareSendFile(LPCTSTR lpszFilename)
{
	// close file if it's already open
	if (m_File.m_hFile != NULL)
	{
		m_File.Close();
	}

	// open source file
	if (!m_File.Open(lpszFilename, CFile::modeRead | CFile::typeBinary))
	{
		return FALSE;
	}
	m_nTotalBytesSend = m_File.GetLength();
	m_nTotalBytesTransfered = 0;

	return TRUE;
}


/********************************************************************/
/*																	*/
/* Function name : CDataSocket::PrepareReceiveFile					*/
/* Description   : Prepare socket to receive a file.				*/
/*																	*/
/********************************************************************/
BOOL CDataSocket::PrepareReceiveFile(LPCTSTR lpszFilename)
{
	// close file if it's already open
	if (m_File.m_hFile != NULL)
	{
		m_File.Close();
	}

	// open destination file
	if (!m_File.Open(lpszFilename, CFile::modeWrite | CFile::modeCreate | CFile::modeNoTruncate | CFile::shareDenyWrite))
	{
		return FALSE;
	}
	m_nTotalBytesReceive = 0;
	m_nTotalBytesTransfered = 0;
	return TRUE;
}


/********************************************************************/
/*																	*/
/* Function name : CDataSocket::OnReceive							*/
/* Description   : Called by the framework to notify this socket	*/
/*				   that there is data in the buffer that can be		*/
/*				   retrieved by calling the Receive member function.*/
/*																	*/
/********************************************************************/
void CDataSocket::OnReceive(int nErrorCode) 
{
	CAsyncSocket::OnReceive(nErrorCode);
	
	Receive();
}


/********************************************************************/
/*																	*/
/* Function name : CDataSocket::Receive								*/
/* Description   : Receive data from a socket						*/
/*																	*/
/********************************************************************/
int CDataSocket::Receive()
{
	int nRead = 0;
	
	if (m_nStatus == XFERMODE_RECEIVE)
	{
		if (m_File.m_hFile == NULL)
			return 0;

		byte data[PACKET_SIZE];
		nRead = CAsyncSocket::Receive(data, PACKET_SIZE);

		switch(nRead)
		{
			case 0:
			{
				m_File.Close();
				m_File.m_hFile = NULL;
				Close();
				// tell the client the transfer is complete.
				m_pConnectSocket->SendResponse("226 Transfer complete");
				// destroy this socket
				AfxGetThread()->PostThreadMessage(WM_THREADMSG, 0, 0);
				// upload succesfull
				((CConnectThread *)AfxGetThread())->UpdateStatistic(FTPSTAT_UPLOADSUCCEEDED);
				break;
			}
			case SOCKET_ERROR:
			{
				if (GetLastError() != WSAEWOULDBLOCK)
				{
					m_File.Close();
					m_File.m_hFile = NULL;
					Close();
					m_pConnectSocket->SendResponse("426 Connection closed; transfer aborted.");
					// destroy this socket
					AfxGetThread()->PostThreadMessage(WM_THREADMSG, 0, 0);
					// upload failed
					((CConnectThread *)AfxGetThread())->UpdateStatistic(FTPSTAT_UPLOADFAILED);

				}
				break;
			}
			default:
			{
				((CConnectThread *)AfxGetThread())->IncReceivedBytes(nRead);

				TRY
				{
					m_File.Write(data, nRead);
				}
				CATCH_ALL(e)
				{
					m_File.Close();
					m_File.m_hFile = NULL;
					Close();
					m_pConnectSocket->SendResponse("450 can't access file.");
					// destroy this socket
					AfxGetThread()->PostThreadMessage(WM_THREADMSG, 0, 0);
					// upload failed
					((CConnectThread *)AfxGetThread())->UpdateStatistic(FTPSTAT_UPLOADFAILED);
					return 0;
				}
				END_CATCH_ALL;
				break;
			}
		}
	}
	return nRead;
}


/********************************************************************/
/*																	*/
/* Function name : CDataSocket::SetData								*/
/* Description   : Set data that is initially send to client.		*/
/*																	*/
/********************************************************************/
void CDataSocket::SetData(LPCTSTR lpszData)
{
	m_strData = lpszData;
	m_nTotalBytesSend = m_strData.GetLength();
	m_nTotalBytesTransfered = 0;
}


/********************************************************************/
/*																	*/
/* Function name : CDataSocket::SetTransferType						*/
/* Description   : Set transfer type:								*/
/*				   0 = LIST DIR, 1 = SEND FILE, 2 = RECEIVE FILE	*/
/*																	*/
/********************************************************************/
void CDataSocket::SetTransferType(int nType)
{
	m_nTransferType = nType; 

	switch(m_nTransferType)
	{
		case 0:	// List Directory
			m_nStatus = XFERMODE_LIST;
			OnSend(0);
			break;
		case 1:	// Send File
			if (PrepareSendFile(m_strData))
			{
				m_nStatus = XFERMODE_SEND;
				m_bConnected = TRUE;
				OnSend(0);
			}
			else
			{
				Close();
			}
			break;
		case 2:	// Receive File
			if (PrepareReceiveFile(m_strData))
			{
				m_nStatus = XFERMODE_RECEIVE;
				m_bConnected = TRUE;
				OnSend(0);
			}
			else
			{
				Close();
				m_pConnectSocket->SendResponse("450 can't access file.");
				// destroy this socket
				AfxGetThread()->PostThreadMessage(WM_THREADMSG, 0, 0);
				// upload failed
				((CConnectThread *)AfxGetThread())->UpdateStatistic(FTPSTAT_UPLOADFAILED);
			}
			break;
		default:
			break;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -