#pragma comment(lib, "WS2_32")
#include <WinSock2.h>
#include <stdio.h>
#include <assert.h>
#include "common.h"
#include "Client_Socket.h"
#include "Client.h"
FTP_Client::FTP_Client()
{
i= 0;
pFile = NULL;
g_lLength = 0;
g_pBuff = NULL;
g_sClient = INVALID_SOCKET;
memset(g_sdir, 0 ,sizeof(g_sdir));
strcpy(g_sdir,"c:\\");
memset(g_szBuff,0,sizeof(g_szBuff));
memset(g_szFileName,0,sizeof(g_szFileName));
}
FTP_Client::~FTP_Client()
{
CloseSocket();
}
bool FTP_Client::CloseSocket()
{
// 关闭套接字
closesocket(g_sClient);
// 释放winsock库
WSACleanup();
return true;
}
int main(char argv[])
{
ClientSocket Csocket;
Csocket.InitSocket();
Csocket.ConectToServer();
return 0;
}
// 处理server端传送过来的消息
bool FTP_Client::ProcessMsg()//存在父类调用子类函数的问题导致错误
{
CCSDef::TMSG_HEADER *pMsgHeader = (CCSDef::TMSG_HEADER*)g_szBuff;
int nRecv = ::recv(g_sClient, g_szBuff, MAX_PACKET_SIZE + 1, 0);
switch (pMsgHeader->cMsgID)
{
case MSG_OPENFILE_ERROR: // 打开文件错误
{
OpenFileError(pMsgHeader);
}
break;
case MSG_FILELENGTH: // 文件的长度
{
if (0 == g_lLength)
{
CCSDef::TMSG_FILELENGTH* pRequestFilenameMsg = (CCSDef::TMSG_FILELENGTH*)pMsgHeader;
g_lLength = pRequestFilenameMsg->lLength;
printf("File Length: %d\n", g_lLength);
}
}
break;
case MSG_FILENAME:
{
if(AllocateMemoryForFile(pMsgHeader) == false)
{
memset(g_sdir,0,sizeof(g_sdir));
memset(g_sdir,0,sizeof(g_sdir));
//memset(g_szBuff,0,sizeof(g_szBuff));
memset(g_szFileName,0,sizeof(g_szFileName));
g_lLength = 0;
g_pBuff = NULL;
if(Client_File_Assist()==0)
{
ProcessMsg();
}
}
return true;
}
break;
case MSG_OPENDIR_ERROR: // 直接输入路径下载时出错进入
{
printf("\n\nCannot find file!Now Jump Into FindSystem!\n\n\n");
memset(g_sdir,0,sizeof(g_sdir));
//memset(g_szBuff,0,sizeof(g_szBuff));
memset(g_szFileName,0,sizeof(g_szFileName));
g_lLength = 0;
g_pBuff = NULL;
Client_File_Assist();
}
break;
case MSG_OPENDIR_OK:
{
printf("ftp command successful\n");
Client_File_Assist();
}
break;
case MSG_OPENDIRFILE_ERROR:
{
int j;
printf("No such file or directory.\n");
for (j=80; j>=0; j--)//太大了无法实现
{
if (g_sdir[j] == '\\')
{
g_sdir[j]=0;
break;
}
g_sdir[j]=0;
}
Client_File_Assist();
//ProcessMsg();
}
break;
case MSG_DIR_BACK:
{
CCSDef::TMSG_FINDBACK* pPrintFilenameMsg = (CCSDef::TMSG_FINDBACK*)pMsgHeader;
printf("**************File Name**************\n%s\n", pPrintFilenameMsg->szFileBack);
memset(pPrintFilenameMsg->szFileBack,0,sizeof(2000));
Client_File_Assist();
}
break;
case MSG_FILE: // 传送文件,写入文件成功之后退出这个函数
{
while(WriteToFile())
{
}
return false;
}
break;
}
return true;
}
// 打开文件失败
bool FTP_Client::OpenFileError(CCSDef::TMSG_HEADER *pMsgHeader)
{
if (NULL != g_pBuff)
return true;
assert(NULL != pMsgHeader);
printf("Cannot find file!Please input again!\n");
// 重新输入文件名称
//SendFileNameToServer();//...................................
return true;
}
// 查找是否已经存在了要保存的文件,同时分配缓冲区保存文件
bool FTP_Client::AllocateMemoryForFile(CCSDef::TMSG_HEADER *pMsgHeader)
{
CCSDef::TMSG_FILENAME* pRequestFilenameMsg = (CCSDef::TMSG_FILENAME*)pMsgHeader;
printf("File Name: %s\n", pRequestFilenameMsg->szFileName);
// 把文件的路径设置为C盘根目录下
strcpy(g_szFileName, "c:\\SocketDownloadData\\");
strcat(g_szFileName, pRequestFilenameMsg->szFileName);
// 查找相同文件名的文件是否已经存在,如果存在报错退出
FILE* pFile;
if (NULL != (pFile = fopen(g_szFileName, "r")))
{
// 文件已经存在,要求重新输入一个文件
printf("The file already exist!\n");
CCSDef::TMSG_ERROR_MSG tMsgErrorMsg(MSG_FILEALREADYEXIT_ERROR);
::send(g_sClient, (char*)(&tMsgErrorMsg), sizeof(CCSDef::TMSG_ERROR_MSG), 0);
fclose(pFile);
return false;
}
// 分配缓冲区开始接收文件,如果分配成功就给server端发送开始传送文件的要求
printf("Now ready to get the file %s!\n", pRequestFilenameMsg->szFileName);
CCSDef::TMSG_CLIENT_READY tMsgClientReady;
tMsgClientReady.sendlength = g_lLength;
strcpy(tMsgClientReady.szFileName,g_sdir); //12.31 15:48
if (SOCKET_ERROR == ::send(g_sClient, (char*)(&tMsgClientReady), sizeof(CCSDef::TMSG_CLIENT_READY), 0))
{
printf("Send Error!\n");
exit(-1);
}
return true;
}
// 写入文件
bool FTP_Client::WriteToFile()
{
CCSDef::TMSG_HEADER *pMsgHeader = (CCSDef::TMSG_HEADER*)g_szBuff;
int nRecv = ::recv(g_sClient, g_szBuff, MAX_PACKET_SIZE + 1, 0);
if (pMsgHeader->cMsgID == MSG_FILE_DATA)
{
CCSDef::TMSG_FILE_DATA* pMsgFile = (CCSDef::TMSG_FILE_DATA*)pMsgHeader;
int nStart = pMsgFile->tFile.nStart;
int nSize = pMsgFile->tFile.nSize;
if (nStart==0)
{
pFile = fopen(g_szFileName, "w+b");
}
printf("start = %d, size = %d\n", nStart, nSize);
printf("FileLength = %d\n",g_lLength);
//printf("Writing to disk....\n");
//printf("%d\n",++i);
// 写入文件
fwrite(pMsgFile->tFile.szBuff, pMsgFile->tFile.nSize, 1, pFile);
if (nStart + nSize >= g_lLength)
{
fclose(pFile);
// 保存文件成功传送消息给server退出server
CCSDef::TMSG_SENDFILESUCCESS tMsgSendFileSuccess;
while (SOCKET_ERROR == ::send(g_sClient, (char*)(&tMsgSendFileSuccess), sizeof(CCSDef::TMSG_SENDFILESUCCESS), 0));
printf("Save the file %s success!\n", g_szFileName);
return false;
}
}
return true;
}
//查找服务器文件助手
bool FTP_Client::Client_File_Assist()
{
int i,j;
char filenameget[25];
while (1)
{
//printf("****Welcome To Server Find System****\n");
printf("ftp> ");
TCHAR fdir[MAX_PATH];
TCHAR filedir[MAX_PATH];
fgets(fdir, MAX_PATH, stdin);
//去除最后一位回车
for (i=0; i<=MAX_PATH; i++)
{
if (fdir[i]=='\n')
{
fdir[i]=0;
break;
}
}
//退出判断
if (strcmp(fdir,"exit") == 0)
{
printf("Exit Success!\n");
CCSDef::TMSG_ERROR_MSG tMsgErrorMsg(MSG_CLIENT_EXIT);
::send(g_sClient, (char*)(&tMsgErrorMsg), sizeof(CCSDef::TMSG_ERROR_MSG), 0);
CloseSocket();
exit(-1);
}
//路径查询
if (strcmp(fdir,"pwd") == 0)
{
printf("Now Your Position: %s\n",g_sdir);
}
//文件打印
if (strcmp(fdir,"ls") == 0)
{
CCSDef::TMSG_FINDFILE tMsgFilePath;
tMsgFilePath.findflag=1;
lstrcpy(tMsgFilePath.szDIR, g_sdir);
if (SOCKET_ERROR == ::send(g_sClient, (char*)(&tMsgFilePath), sizeof(CCSDef::TMSG_FINDFILE), 0))
{
//printf("%d\n",WSAGetLastError);
printf("Send Find Path Error!\n");
exit(-1);
}
return 0;
}
//获取判断
if (fdir[0] == 'g' && fdir[1] == 'e' &&
fdir[2] == 't' && fdir[3] == ' ')
{
memset(filenameget, 0, sizeof(filenameget));
for (i=0; i<=30; i++)
{
filenameget[i]=fdir[i+4];
if (fdir[i]=='\0')
{
filenameget[i]='\0';
break;
}
}
lstrcat(g_sdir,"\\");
lstrcat(g_sdir, filenameget);
printf("------------------%s\n",g_sdir);
CCSDef::TMSG_FILENAME tMsgRequestFileName;
strcpy(tMsgRequestFileName.szFileName, g_sdir);
printf("------------------%s\n",tMsgRequestFileName.szFileName);
if (SOCKET_ERROR == ::send(g_sClient, (char*)(&tMsgRequestFileName), sizeof(CCSDef::TMSG_FILENAME), 0))
{
printf("Send File Name Error!\n");
exit(-1);
}
return 0;
}
// 路径切换
if (fdir[0] == 'c' && fdir[1] == 'd' && fdir[2]== ' ')
{
if (fdir[3] == '.')
{
for (j=80; j>=0; j--)//太大了无法实现
{
if (g_sdir[j] == '\\')
{
g_sdir[j]=0;
break;
}
g_sdir[j]=0;
}
}
else
{
memset(filedir, 0, sizeof(filedir));
for (i=0;i<MAX_PATH;i++)
{
if (fdir[i+3]=='\0')
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
SocketS&C.rar (61个子文件)
SocketS&C
Client
Client_Socket.h 562B
Client.h 702B
Client.cpp 8KB
common.h 5KB
Client.opt 50KB
Client.ncb 161KB
Client_Socket.cpp 4KB
Client_login.h 278B
Client.positions 692B
Client.plg 1KB
Client.dsp 4KB
Debug
Client.exe 200KB
Client.pch 3.51MB
vc60.pdb 84KB
Client.pdb 529KB
vc60.idb 161KB
Client.ilk 296KB
Client.obj 35KB
Client_login.obj 7KB
Client_Socket.obj 29KB
CommonDef.h 3KB
Client.dsw 537B
Client_login.cpp 887B
Server
StdAfx.h 1KB
Server.opt 54KB
Thread.h 386B
Socket.cpp 2KB
common.h 5KB
Thread_Client.h 847B
Server.dsw 537B
Thread_Client.cpp 7KB
Server.suo 4KB
samprotocolapi.h 2KB
DbOci.cpp 4KB
Thread.cpp 1015B
Socket.h 364B
Debug
vc60.pdb 164KB
Server.ilk 643KB
Server.obj 25KB
Server.suo 3KB
Server_File.obj 22KB
vc60.idb 257KB
Thread.obj 13KB
DbRead.obj 24KB
Server.pdb 833KB
Thread_Client.obj 41KB
Server.exe 264KB
Server.pch 3.42MB
Socket.obj 30KB
DbOci.obj 25KB
DbSend.obj 54KB
Server.dsp 4KB
Server.cpp 238B
SamRtbPacket.H 7KB
Server.positions 2KB
Server_File.h 478B
Server.ncb 73KB
datainterface.h 2KB
Server_File.cpp 4KB
DbOci.h 557B
Server.plg 1KB
共 61 条
- 1
资源评论
- zqchap20122014-03-07貌似用不了 但还是有帮助的
- shicharming2015-04-22从中学习到了很多
Leech_jiaojiao
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功