ftpClient.h
#pragma
#include<winsock.h>
class ftpClient
{
private:
enum {
SERVER_PORT = 9999,
BUFFER_SIZE = 4096
};
sockaddr_in serverChannel;
char buffer[BUFFER_SIZE];
int serverSocket;
int clientSocket;
bool isConnect;
char name[50];
bool getFile();
bool putFile();
bool acknowledge();
bool sendRequest(char* instruction);
bool connect2Host(const char* hostName);
bool getWorkDir();
public:
ftpClient();
~ftpClient();
void start();
};
ftpClient.cpp
#define _CRT_SECURE_NO_WARNINGS
#include"ftpClient.h"
#include<cstdio>
#include<io.h>
#include<cstring>
#include<fstream>
ftpClient::ftpClient()
{
WORD wVersionRequested;
WSADATA wsaData;
int ret;
//WinSock初始化:
wVersionRequested = MAKEWORD(2, 2);//希望使用的WinSock DLL的版本
ret = WSAStartup(wVersionRequested, &wsaData);
if (ret != 0)
{
printf("WSAStartup() failed!n");
}
//确认WinSock DLL支持版本2.2:
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
WSACleanup();
printf("Invalid Winsock version!n");
}
isConnect = false;
}
void ftpClient::start()
{
char c[100];
char d[100];
printf("这里是FTP客户端,您可以输入help查看操作方法,输入quit退出客户端n");
while (1) {
scanf("%s", c);
if (strcmp(c, "help") == 0) {
printf("get [fileName] -- 下载文件n"
"put [fileName] -- 上传文件n"
"ftp [ip] -- 登录FTPn"
"pwd -- 显示服务器当前工作文件夹n"
"cd [dirName] -- 更改当前文件夹n"
"close -- 关闭与当前ftp的连接n"
"quit -- 退出客户端n"
);
}
else if (strcmp(c, "get") == 0) {
scanf("%s", d);
strcat(c, " ");
strcat(c, d);
if (!isConnect) {
printf("you haven't connected to any server!n");
}
else sendRequest(c);
}
else if (strcmp(c, "put") == 0) {
scanf("%s", d);
strcat(c, " ");
strcat(c, d);
if (!isConnect) {
printf("you haven't connected to any server!n");
}
else sendRequest(c);
}
else if (strcmp(c, "ftp") == 0) {
scanf("%s", d);
if (!isConnect&&connect2Host(d)) {
isConnect = true;
}
else if(isConnect){
printf("you have already connected to servern"
"please close the connection before connect to a new servern");
}
}
else if (strcmp(c, "pwd") == 0) {
if (!isConnect) {
printf("you haven't connected to any server!n");
}
else sendRequest(c);
}
else if (strcmp(c, "cd") == 0) {
scanf("%s", d);
strcat(c, " ");
strcat(c, d);
if (!isConnect) {
printf("you haven't connected to any server!n");
}
else sendRequest(c);
}
else if (strcmp(c, "quit") == 0) {
if (isConnect) {
strcpy(c, "close");
isConnect = false;
send(clientSocket, c, strlen(c) + 1, 0);
closesocket(clientSocket);
}
break;
}
else if (strcmp(c, "close") == 0) {
if (isConnect) {
isConnect = false;
send(clientSocket, c, strlen(c) + 1, 0);
closesocket(clientSocket);
}
}
else {
printf("syntex errorn");
}
}
}
bool ftpClient::connect2Host(const char* hostName)
{
//创建socket
clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket < 0) {
printf("cannot create socketn");
return false;
}
else printf("successfully create socketn");
memset(&serverChannel, 0, sizeof(serverChannel));//初始化为0
serverChannel.sin_family = AF_INET;//channel协议家族AF_INET
serverChannel.sin_addr.S_un.S_addr = inet_addr(hostName);//地址
serverChannel.sin_port = htons(SERVER_PORT);//服务器端口
//建立连接
serverSocket = connect(clientSocket, (sockaddr*)&serverChannel, sizeof(serverChannel));
if (serverSocket < 0) {
printf("cannot connect to the hostn");
return false;
}
else {
printf("successfully connect to the hostn");
return true;
}
}
bool ftpClient::sendRequest(char* instruction)
{
int r = send(clientSocket, instruction, strlen(instruction) + 1, 0);
if (r == SOCKET_ERROR) {
printf("request failedn");
return false;
}
else {
printf("request successn");
char opt[5];
int i = 0, j = 0;
while (instruction[i] != ' '&&instruction[i] != '