========================================================================
CONSOLE APPLICATION : ftp_server
========================================================================
AppWizard has created this ftp_server application for you.
This file contains a summary of what you will find in each of the files that
make up your ftp_server application.
ftp_server.dsp
This file (the project file) contains information at the project level and
is used to build a single project or subproject. Other users can share the
project (.dsp) file, but they should export the makefiles locally.
ftp_server.cpp
This is the main application source file.
/////////////////////////////////////////////////////////////////////////////
Other standard files:
StdAfx.h, StdAfx.cpp
These files are used to build a precompiled header (PCH) file
named ftp_server.pch and a precompiled types file named StdAfx.obj.
/////////////////////////////////////////////////////////////////////////////
Other notes:
AppWizard uses "TODO:" to indicate parts of the source code you
should add to or customize.
/////////////////////////////////////////////////////////////////////////////
//===============================================================
BOOL StoreFile(SOCKET s, char *szFile)
{
// char RecFileBuf[RCVBUFLEN];
BOOL endflag=FALSE;
FILE *fp;
int RecLength;
int idx=0;
int filelength;
//filelength=atoi(FileLen);
// filelength=filelength-fposition-2; //减掉命令和\r
if( ( fp= fopen( szFile, "wb" )) == NULL )
{
return false;
}
if(writeflag==TRUE)
{
int sjLen=FirstRecLen-fposition-2;
fwrite(rcmd+fposition+1,sizeof(char),sjLen,fp);
// filelength=filelength-sjLen;
}
while(1)
{
RecLength=MyDataRecv(s, RecBuf, RCVBUFLEN);
if (RecLength==-1)
{
return false;
}
if(RecLength<filelength-idx)
{
endflag=FALSE;
fwrite(RecBuf,sizeof(char),RecLength,fp);
idx=idx+RecLength;
}
else endflag=TRUE;
if (endflag==TRUE)
{
fwrite(RecBuf,sizeof(char),RecLength,fp);
fclose(fp);
break;
}
}
return true;
}
int MyDataRecv(const SOCKET s, char *Buf, const int Buflen)
{
int ret;
int recved = 0;
fd_set fdr;
FD_ZERO( &fdr );
FD_SET( s, &fdr );
struct timeval pTimeout;
pTimeout.tv_sec=TIME_OUT;
pTimeout.tv_usec=0;
ret = select( 0, &fdr, NULL, NULL, &pTimeout );
if ( ret > 0 )
{
if ( FD_ISSET( s, &fdr ) )
{
recved = recv( s, Buf, Buflen, 0 );
if ( recved <= 0 )
{
return SOCKET_ERROR;
}
return recved;
}
}
return ret;
}
/////////////////////////////////////////////////////////////////
DWORD EventCaused = WSAWaitForMultipleEvents(
2,
Handles,
FALSE,
WSA_INFINITE,
FALSE);
if(EventCaused == WSA_WAIT_FAILED || EventCaused == WSA_WAIT_EVENT_0)
{
if(EventCaused == WAIT_FAILED)
pGenericServer->LogMessage(LOGFILENAME, "WaitForMultipleObjects(...) failure", "AcceptThread", GetLastError());
pGenericServer->CleanupThread(Event, pGenericServer->DisconnectEvent, s);
return THREADEXIT_SUCCESS;
}
result = WSAEnumNetworkEvents(
s,
Event,
&NetworkEvents);
if(result == SOCKET_ERROR)
{
pGenericServer->LogMessage(LOGFILENAME, "WSAEnumNetworkEvents(...) failure", "AcceptThread", WSAGetLastError());
pGenericServer->CleanupThread(Event, pGenericServer->DisconnectEvent, s);
return THREADEXIT_SUCCESS;
}
if(NetworkEvents.lNetworkEvents == FD_ACCEPT)
{
if(pGenericServer->bConnected) continue;
//如何区分同一内网中的同名主机?
SOCKET ClientSocket = WSAAccept(s, &ClientAddr, &addrlen, NULL, NULL);
memcpy(&sain, &ClientAddr, addrlen);
sprintf(cAddr, "%d.%d.%d.%d",
sain.sin_addr.S_un.S_un_b.s_b1,
sain.sin_addr.S_un.S_un_b.s_b2,
sain.sin_addr.S_un.S_un_b.s_b3,
sain.sin_addr.S_un.S_un_b.s_b4);
//LPCTSTR ClientIP = inet_ntoa(sain.sin_addr);
//UINT ClientPort = sain.sin_port;
//SOCKADDR_IN ClientAddr; // 定义一个客户端得地址结构作为参数
//int addr_length=sizeof(ClientAddr);
//AcceptSocket = accept(ListenSocket,(SOCKADDR*)&ClientAddr, &addr_length);
//LPCTSTR ClientIP = inet_ntoa(ClientAddr.sin_addr);
//UINT ClientPort = ClientAddr.sin_port;
if(ClientSocket == INVALID_SOCKET)
{
pGenericServer->LogMessage(LOGFILENAME, "WSAAccept(...) failure", "AcceptThread", WSAGetLastError());
// 有一个文件错误
continue;
}
else
{
if(pGenericServer->szMainIP.compare(cAddr))
{
closesocket(ClientSocket);
continue;
}
if(!pGenericServer->Data_AddClient(ClientSocket, cAddr, sain.sin_port))
{
pGenericServer->LogMessage(LOGFILENAME, "AddClient(...) failure", "AcceptThread");
continue; // I think there is no reason to shutdown whole server if just one connection failed
}
}
}
}
FTP服务器和客户端纯C编写
4星 · 超过85%的资源 需积分: 14 136 浏览量
2011-11-11
22:48:47
上传
评论 5
收藏 761KB ZIP 举报
lightd
- 粉丝: 143
- 资源: 42
最新资源
- C语言基础-C语言编程基础之Leetcode编程题解之第39题组合总和.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第38题外观数列.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第37题解数独.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第36题有效的数独.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第35题搜索插入位置.zip
- index.wxml
- C语言基础-C语言编程基础之Leetcode编程题解之第33题搜索旋转排序数组.zip
- 基于Python实现的手写数字识别系统源码.zip
- 从网页提取禁止转载的文字
- C语言基础-C语言编程基础之Leetcode编程题解之第32题最长有效括号.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
前往页