其实网上都有现成的代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define COMMAND_SIZE 100
#define BUF_SIZE 1000
#define END_SIGN "OK "
#define READY_SIGN "OK "
#define FILE_NAME_SIZE 100
#define PORT 8888
int readen(int fd, char *buf, int size){
int size1 = size;
while(1){
int num = read(fd, buf, size);
//buf[num] = 0;
size -= num;
buf += num;
if(0 == size){
break;
}
if(0 == strncmp(buf-strlen(END_SIGN), END_SIGN, strlen(END_SIGN))){
*(buf-strlen(END_SIGN)) = 0;
size += 2;
break;
}
}
return (size1-size);
}
void getFileName(char *cmd, char *fileName){
char *p = strstr(cmd, " ");
char *p1 = strstr(cmd, "\n ");
while( ' ' == *p)
p++;
strncpy(fileName, p, p1-p);
fileName[p1-p] = 0;
}
int initfd(char **argv){
int fd;
if((fd=socket(AF_INET,SOCK_STREAM,0))==-1){
printf( "socket error!\n ");
exit(1);
}
struct sockaddr_in server;
bzero(&server, sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
in_addr temp;
inet_aton(argv[1], &temp);
server.sin_addr = temp;
if(connect(fd, (struct sockaddr *)&server, sizeof(struct sockaddr))==-1){
perror( "connect error!\n ");
exit(1);
}
return fd;
}
void localcommand(char *cmd){
char buf[BUF_SIZE];
FILE *fp = popen(cmd, "r ");
if(NULL == fp){
printf( "exec %s error!\n ", cmd);
return;
}
while(fgets(buf, BUF_SIZE, fp)){
printf(buf);
}
pclose(fp);
}
void getcommand(int fd, char *fileName){
char buf[BUF_SIZE];
FILE *fp = fopen(fileName, "w+b ");
if(NULL == fp){
printf( "open %s failed!\n ", fileName);
return;
}
while(1){
int num = readen(fd, buf, BUF_SIZE);
fwrite(buf, num, 1, fp);
printf( "the num is %d\n ", num);
if(BUF_SIZE != num)
break;
}
fclose(fp);
printf( "get finished\n ");
}
void putcommand(int fd, char *fileName){
char buf[BUF_SIZE];
//receive "OK "
int num = recv(fd, buf, strlen(END_SIGN), 0);
buf[num] = 0;
if(0 == strncmp(buf, "OK ", strlen( "OK "))){
FILE *fp = fopen(fileName, "rb ");
if(NULL == fp){
printf( "open %s failed!\n ", fileName);
return;
}
while(1){
long offset1 = ftell(fp);
fread(buf, BUF_SIZE, 1, fp);
long offset2 = ftell(fp);
int num = offset2 - offset1;
send(fd, buf, num, 0);
printf( "the num is %d\n ", num);
if(BUF_SIZE != num)
break;
}
send(fd, END_SIGN, strlen(END_SIGN), 0);
fclose(fp);
printf( "put finished\n ");
}
else{
printf( "server is not ready!\n ");
}
}
void lpcFtpCommand(int fd){
char buf[BUF_SIZE];
while(1){
int num = readen(fd, buf, BUF_SIZE);
printf( "%s\n ", buf);
if(BUF_SIZE != num)
break;
}
}
void requestftp(int fd){
char cmd[COMMAND_SIZE];
char buf[BUF_SIZE];
char fileName[FILE_NAME_SIZE];
while(1){
//input the command
printf( "myftp> ");
fgets(cmd, COMMAND_SIZE, stdin);
printf( "%s ", cmd);
//quit command
if(0 == strncmp(cmd, "bye\n ", strlen( "bye\n "))){
send(fd, cmd, strlen(cmd), 0);
printf( "connection closing!\n ");
break;
}
//ftp command
if(0 == strncmp(cmd, "!ls\n ", strlen( "!ls\n "))){
localcommand(cmd+1);
}
else if(0 == strncmp(cmd, "!pwd\n ", strlen( "!pwd\n "))){
localcommand(cmd+1);
}
else if(0 == strncmp(cmd, "!cd ", strlen( "!cd "))){
getFileName(cmd, fileName);
chdir(fileName);
localcommand( "ls\n ");
}
else if(0 == strncmp(cmd, "get ", strlen( "get "))){ //notice space
send(fd, cmd, strlen(cmd), 0);
getFileName(cmd, fileName);
getcommand(fd, fileName);
}
else if(0 == strncmp(cmd, "put ", strlen( "put "))){ //notice space
send(fd, cmd, strlen(cmd), 0);
getFileName(cmd, fileName);
putcommand(fd, fileName);
}
else if(0 == strncmp(cmd, "ls\n ", strlen( "ls\n "))){
send(fd, cmd, strlen(cmd), 0);
lpcFtpCommand(fd);
}
else if(0 == strncmp(cmd, "pwd\n ", strlen( "pwd\n "))){
send(fd, cmd, strlen(cmd), 0);
lpcFtpCommand(fd);
}
else if(0 == strncmp(cmd, "cd ", strlen( "cd "))){
send(fd, cmd, strlen(cmd), 0);
lpcFtpCommand(fd);
}
else{
printf( "?invalid command!\n ");
}
}
close(fd);
}
int main(int argc, char **argv){
//check the param
if(2 != argc){
printf( "myftp command is like: myftp 127.0.0.1\n ");
exit(0);
}
//create and connect socket
int fd = initfd(argv);
//deal with the comand, the endpoint is "bye "
requestftp(fd);
return 0;
}
基本功能给你了,其它自己做
73.FTP下载
CFtpGet::CFtpGet()
{
// get the name of the app
strAppName.LoadString(AFX_IDS_APP_TITLE);
// create an internet session
pInternetSession = new CInternetSession(strAppName,
INTERNET_OPEN_TYPE_PRECONFIG);
// if Not good, show message + return
// should never failed anyway
if(!pInternetSession)
{
AfxMessageBox("Can't start internet session");
return;
}
}
CFtpGet::~CFtpGet()
{
// close the internet session
pInternetSession->Close();
// delete the session
if(pInternetSession != NULL)
delete pInternetSession;
}
// function, in logical order
bool CFtpGet::SetAccessRight(CString userName,
CString userPass)
{
// simply get username and password
strPass = userPass;
strUser = userName;
if( (strPass == "") || (strUser == ""))
return 0;
return 1;
}
bool CFtpGet::OpenConnection(CString server)
{
if(server == "")
return 0;
// put the server name in the CFtpGet class
strServerName = server;
try {
// try to connect to a ftp server
pFtpConnection = pInternetSession->GetFtpConnection(strServerName,
strUser,
strPass);
} catch (CInternetException* pEx)
{
// if failed, just show the error
// Oops! We failed to connect!
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
TRACE(szErr);
AfxMessageBox(szErr);
pEx->Delete();
return 0;// return 1 but previous error box have been showed
}
return 1;
}
bool CFtpGet::GetFile(CString remoteFile,
CString localFile)
{
// Try to get the file
BOOL bGotFile = pFtpConnection->GetFile(remoteFile,
localFile,
FALSE,
FILE_ATTRIBUTE_NORMAL,
FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE);
return bGotFile ? 1 : 0 ;
// if bGotFile is 0 ( FALSE ), return 0
// if bGotFile is 1 ( TRUE ), return 1
}
int CFtpGet::GetMultipleFile(CStringArray *remoteArray,
CStringArray *localArray,
int number_file)
{
// init some var
BOOL goodfile;
int x=0;
int nb_lost_file =0;
// while loop to transfer every file in the array
while(x
// try to get file
goodfile = pFtpConnection->GetFile(remoteArray->GetAt(x),
localArray->GetAt(x),
FALSE,
FILE_ATTRIBUTE_NORMAL,
FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE);
missed[x] = goodfile ? 0 : 1;
// if failed, missed[x] become 1
// if good, missed become 0
if(missed[x])
nb_lost_file++;
// if the file was missed, increase the number of
// missing file.
// increase to the next file
x++;
}
//return the number of missing file, if any.
return nb_lost_file;
}
bool CFtpGet::CloseConnection()
{
// close the connection to server, you can reconnect latter
if(pFtpConnection == NULL)
return 0;
try{
pFtpConnection->Close();
}catch(...)
{
return 0;
}
if(pFtpConnection != NULL)
delete pFtpConnection;
return 1;
}
int CAddFile::DoModal()
{
// TODO: Add your specialized code here and/or call the base class
return CDialog::DoModal();
}
void CAddFile::OnCancel()
{
// TODO: Add extra cleanup here
strNewFile = "";
CDialog::OnCancel();
}
void CAddFile::OnOK()
{
// TODO: Add extra validation here
m_new_file.GetWindowText(strNewFile);
CDialog::OnOK();
}
void CAddFile::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
OnOK();
}
m_folder.SetWindowText("c:\\download\\");
void CHttpDlg::OnNewVer() //Get These file button
{
CString folder;
CString user;
CString pass;
CString server;
// CString program_name;
CString tempRemote;
CString tempLocal;
// error
CString Error;
// get folder
m_folder.GetWindowText(folder);
// Get username, password, server
m_user.GetWindowText(user);
m_pass.GetWindowText(pass);
m_server.GetWindowText(server);
// get every file name in the box
// make 2 CStringArray, one for remote, the other for local
CStringArray remote;
CStringArray local;
for(int x=0;x
m_file.GetText(x,tempRemote);
remote.Add(tempRemote);
tempLocal = folder + tempRemote;
local.Add(tempLocal);
}
// open CFtpGet class
CFtpGet ftpget;
// SetRight
ftpget.SetAccessRight(user,pass);
// open server
bool conectOK;
conectOK = ftpget.OpenConnection(server);
// transfer multiple file
if(conectOK)
{
ftpget.GetMultipleFile(&remote,&local,m_file.GetCount());
}
else
memset(ftpget.missed,0,sizeof(bool[100]));
// verify which file, if any, have not been transfered
for(int test=0;test
if(ftpget.missed[test])
{
m_file.GetText(test,Error);
Error = "The file " + Error;
Error += " is missing!";
AfxMessageBox(Error);
}
}
// close connection
ftpget.CloseConnection();
// quit this function
}
void CHttpDlg::OnAdd() // Add button
{
// TODO: Add your control notification handler code here
CAddFile add;
int ret = add.DoModal();
int lng = 0;
CString newfile;
if(ret == IDOK)
{
// get m_new_file lenght
lng = add.strNewFile.GetLength();
// if OK put in list
if(lng == 0 )// no input
return;
m_file.AddString(add.strNewFile);
}
}
void CHttpDlg::OnDel() // delete button
{
// TODO: Add your control notification handler code here
int selection=0;
selection = m_file.GetCurSel();
// now i get the good one
if(selection <0)
{
AfxMessageBox("You have to select a value in the file list");
return;
}
m_file.DeleteString(selection);
// no more in the dialog
}
void CHttpDlg::OnAnon() // anonymous check button
{
// TODO: Add your control notification handler code here
int check = m_anon.GetCheck();
if(check == 1)
{
// store some info in mem
m_user.GetWindowText(cOldUser,99);
m_pass.GetWindowText(cOldPass,99);
// set new value
m_user.SetWindowText("anonymous");
m_pass.SetWindowText("your@email.com");
m_pass_email.SetWindowText("Your email");
// you can't edit anonymous
m_user.SetReadOnly(TRUE);
}else
{
// set the old value ( if any )
m_user.SetWindowText(cOldUser);
m_pass.SetWindowText(cOldPass);
m_pass_email.SetWindowText("Password");
// you have to edit the user name.
m_user.SetReadOnly(FALSE);
}
}
如果采用Borland C++ Builder系列软件的话,是很easy的。
其自带FTP相关的组件,简单的拖拽到节目上,再编写少量的代码就ok了。
试一试吧。
额..建议你参考孙鑫vc++的第十七课...
这个比较麻烦啊..