#include "stdafx.h"
#include "CFtpd.h"
#include "FtpdPi.h"
#define CRLF "\r\n"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
char sadr2[32];
bool canclePasvListenSocket(CFtpPasvSrv* sck)
{
try
{
sck->Close();
delete sck;
return true;
}
catch(...)
{
}
return false;
}
bool deleFtpPI(CFtpdPi* sck)
{
try
{
CFtpd* K = (CFtpd*) sck->pFtpd;
K->RemoveClient(sck);
}
catch(...)
{
}
return true;
}
UINT thFtpCmd( LPVOID pParam )
{
((CFtpdPi*)pParam)->ProcessMsg(((CFtpdPi*)pParam)->bufcmd);
return 1;
}
UINT thSendData( LPVOID pParam)
{
CFtpdPi* pFtpBind = (CFtpdPi*) pParam;
CFile* inFile = NULL;
try
{
char path[256] = "";
strcpy(path,pFtpBind->RemoveLeadingSpace(pFtpBind->curCmdth+5));
char srcPath[300] = "";
if(strcmp(pFtpBind->vCurrentDir,"/")==0)
wsprintf(srcPath,"%s%s%s",pFtpBind->vRealRoot,pFtpBind->vCurrentDir,path);
else
wsprintf(srcPath,"%s%s\\%s",pFtpBind->vRealRoot,pFtpBind->vCurrentDir,path);
pFtpBind->slashFix(srcPath);
if(pFtpBind->isFilePresent(path))
{
bool b_opened = true;
try
{
if(pFtpBind->type == 1)
inFile = new CFile(srcPath,CFile::modeRead|CFile::shareDenyNone|CFile::typeBinary);//CStdioFile(srcPath,CFile::modeRead|CFile::shareDenyNone|CFile::typeText);
else if(pFtpBind->type == 3)
inFile = new CFile(srcPath,CFile::modeRead|CFile::shareDenyNone|CFile::typeBinary);
}
catch(...)
{
b_opened = false;
}
if(b_opened)
{
long flen = inFile->GetLength();
if(pFtpBind->restart_marker > 0)
{
inFile->Seek(pFtpBind->restart_marker,CFile::begin);
pFtpBind->restart_marker = 0;
}
long nCount = 0;
if(pFtpBind->type == 1)
wsprintf(pFtpBind->str,"150 Sending %s (%u bytes). Mode STREAM Type ASCII%s",path,flen,CRLF);
else if(pFtpBind->type == 3)
wsprintf(pFtpBind->str,"150 Sending %s (%u bytes). Mode STREAM Type BINARY%s",path,flen,CRLF);
pFtpBind->Send(pFtpBind->str,strlen(pFtpBind->str),0);
if(flen)
{
long bytesent = 0;
long offset = 0;
DWORD start,end;
do
{
if(flen > pFtpBind->m_buf_length_out)
nCount = inFile->Read(pFtpBind->lpDataBuf_out,pFtpBind->m_buf_length_out);
else
nCount = inFile->Read(pFtpBind->lpDataBuf_out,flen);
flen = flen - nCount;
if(pFtpBind->b_speed_limit_out)
{
bytesent = 0;
offset = 0;
HANDLE h = CreateEvent(NULL,false,false,"noname");
do
{
start = GetTickCount();
if((nCount-offset) > pFtpBind->m_buf_length_out)
bytesent = pFtpBind->dbsocket->Send((char*)pFtpBind->lpDataBuf_out+offset,pFtpBind->m_buf_length_out,60);
else
bytesent = pFtpBind->dbsocket->Send((char*)pFtpBind->lpDataBuf_out+offset,nCount-offset,60);
offset = offset + bytesent;
end = GetTickCount();
if((end-start) < 1000)
{
int time = end-start;
WaitForSingleObject(h,1000-time);
}
}while(offset < nCount);
}
else
{
bytesent = 0;
offset = 0;
do
{
if((nCount-offset) > pFtpBind->m_buf_length_out)
bytesent = pFtpBind->dbsocket->Send((char*)pFtpBind->lpDataBuf_out+offset,pFtpBind->m_buf_length_out,60);
else
bytesent = pFtpBind->dbsocket->Send((char*)pFtpBind->lpDataBuf_out+offset,nCount-offset,60);
offset = offset + bytesent;
}while(offset < nCount);
if(pFtpBind->type == 1)
{
pFtpBind->dbsocket->Send(CRLF,2,60);
}
}
}while(flen != 0);
}
pFtpBind->dbsocket->Close();
if(pFtpBind->b_passive)
pFtpBind->svSocketSrv->Close();
delete inFile;
wsprintf(pFtpBind->str,"226 Transfer finished successfully. Data connection closed.%s",CRLF);
pFtpBind->Send(pFtpBind->str,strlen(pFtpBind->str),0);
SetEvent(pFtpBind->h_inprogress);
return true;
}else
{
pFtpBind->dbsocket->Close();
if(pFtpBind->b_passive)
pFtpBind->svSocketSrv->Close();
if(inFile != NULL)
delete inFile;
wsprintf(pFtpBind->str,"426 Connection closed , file opening failed%s",CRLF);
pFtpBind->Send(pFtpBind->str,strlen(pFtpBind->str),0);
SetEvent(pFtpBind->h_inprogress);
}
}
}catch(...)
{
inFile->Close();
pFtpBind->dbsocket->Close();
canclePasvListenSocket(pFtpBind->svSocketSrv);
delete inFile;
wsprintf(pFtpBind->str,"426 Connection closed; transfer aborted (transfer failed)%s",CRLF);
pFtpBind->Send(pFtpBind->str,strlen(pFtpBind->str),0);
SetEvent(pFtpBind->h_inprogress);
return false;
}
return true;
}
CFtpdPi::CFtpdPi()
{
clientIpCount = 0;
type = 3; //1 = ascii 2 = ebcdic 3 = binary
ipRoller = 0;
b_use_bsock = true;
b_verbose = true;
b_passive = false;
b_bell = false;
b_prompt = true;
b_globbing = true;
b_debugging = false;
b_hashmark = false;
bLOGINOK = false;
bUSER = false;
bPASS = false;
b_speed_limit_out = false;
b_speed_limit_in = false;
restart_marker = 0;
strcpy(vCurrentDir,"/");
strcpy(old_filename,"");
strcpy(new_filename,"");
strcpy(m_group,"nogroup");
m_buf_length_out = 32768;
m_buf_length_in = 32768;
lpDataBuf_out = (unsigned char*) malloc(m_buf_length_out);
memset(lpDataBuf_out,0,m_buf_length_out);
lpDataBuf_in = (unsigned char*) malloc(m_buf_length_in);
memset(lpDataBuf_in,0,m_buf_length_in);
strcpy(monthStr[1],"Jan");
strcpy(monthStr[2],"Feb");
strcpy(monthStr[3],"Mar");
strcpy(monthStr[4],"Apr");
strcpy(monthStr[5],"May");
strcpy(monthStr[6],"Jun");
strcpy(monthStr[7],"Jul");
strcpy(monthStr[8],"Aug");
strcpy(monthStr[9],"Sep");
strcpy(monthStr[10],"Oct");
strcpy(monthStr[11],"Dec");
strcpy(monthStr[12],"Nov");
sl = sizeof(SOCKADDR_IN);
GetLocalTime(&MyTime);
if(FileExists(ipfile))
LoadIpRange();
h_inprogress = CreateEvent(NULL,false,true,"in_progress");
h_dbsocket_ready_to_send = CreateEvent(NULL,false,false,"in_progress");
dbsocket = NULL;
svSocketSrv = NULL;
}
CFtpdPi::~CFtpdPi()
{
free(lpDataBuf_in);
free(lpDataBuf_out);
if(dbsocket != NULL)
{
dbsocket->Close();
delete dbsocket;
}
if(svSocketSrv != NULL)
{
svSocketSrv->Close();
delete svSocketSrv;
}
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CFtpdPi, CSocket)
//{{AFX_MSG_MAP(CFtpdPi)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
void CFtpdPi::OnReceive(int nErrorCode)
{
unsigned char* bufnow = (unsigned char*)malloc(256);
try
{
memset(bufnow,NULL,256);
result = Receive(bufnow,256,0);
bool cont = true;
int currentPos = 0;
if(*(bufnow+1)==0xf2)
currentPos = 1;
for(int i=0;i<result;i++)
{
if(*(bufnow+i)==0x0d&&*(bufnow+i+1)==0x0a)
{
strncpy(bufcmd,(char*)bufnow+currentPos,i-currentPos);
*(bufcmd+i-currentPos) = NULL;
strcpy(bufcmd_history,bufcmd);
AfxBeginThread(&thFtpCmd,this,THREAD_PRIORITY_NORMAL,0,0,NULL);
currentPos = i+2;
i = i + 1;
}
}
free(bufnow);
}
catch (...)
{
delete bufnow;
char lpMsgBuf[128]="";
DWORD eRRR = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,NULL,eRRR,0,lpMsgBuf,128,NULL);
char ecp[256]="";
wsprintf(ecp,"%s\\core.ftpd",AppDir);
CStdioFile ofs((LPCTSTR) ecp ,CFile::modeWrite|CFile::modeCreate);
ofs.WriteString("*---------------------------------------------------------------------------\n");
ofs.WriteString("Exception occurs while processing socket msg\n");
GetLocalTime(&MyTime);
wsprintf(ecp,"Thread crashed at %.4d-%.2d-%.2d-%.2d-%.2d\n",MyTime.wYear,MyTime.wMonth,MyTime.wDay,MyTime.wHour,MyTime.wMinute);
ofs.WriteString(ecp);
ofs.WriteString(lpMsgBuf);
ofs
没有合适的资源?快使用搜索试试~ 我知道了~
FTP-Server-Code.rar_C ftp server_VC FTP_c++网络高并发_ftp_ftp 服务器
共29个文件
h:9个
cpp:8个
clw:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 111 浏览量
2022-09-23
08:06:02
上传
评论
收藏 108KB RAR 举报
温馨提示
FTP服务器源代码,用VC写的,通过分析它的源代码,可以掌握C网络编程以及高并发服务器的设计
资源详情
资源评论
资源推荐
收起资源包目录
FTP-Server-Code.rar (29个子文件)
FTP Server Code
FTPD.ncb 65KB
FtpdPi.cpp 36KB
Resource.h 1KB
FtpdPasvSrv.h 1KB
blocksock.cpp 9KB
FTPD.DSP 4KB
CSTRUTIL.CPP 7KB
FTPD.CPP 1KB
ftpd.rc 7KB
CSTRUTIL.H 683B
RES
FTPD.RC2 396B
FS.BMP 82KB
FTPD.ICO 1KB
Thumbs.db 8KB
fs.bmp.psd 69KB
CFtpd.h 2KB
FtpdPasvSrv.cpp 733B
ftpdDlg.cpp 8KB
StdAfx.cpp 206B
FTPD.DSW 531B
FTPD.OPT 50KB
CFtpd.cpp 6KB
StdAfx.h 1KB
blocksock.h 4KB
ftpdDlg.h 2KB
FtpdPi.h 3KB
FTPD.H 1KB
FTPD.CLW 4KB
fsftpd.user 32B
共 29 条
- 1
御道御小黑
- 粉丝: 58
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0