C/C++、Qt4实现FTP客户端(有无界面版)
操作系统:Ubuntu 12.04 LTS
开发工具:GNU4.6.3,C/C++标准库,Qt4,Qt Creator Documentation 2.4.1
相关知识点参考:
网络通信TCP/UDP——学习笔记,Qt相关知识(二)——学习笔记
C语言无界面版本:
直接上效果
接下来操作和ftp基本操作一致
我一共分了3个文件
我们简单看下代码
ftpclient.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "network.h" NetWork* nw;
NetWork* data_nw;
int list_len = ;
char buf[] = {}; typedef struct List
{
char filename[];
}List; void ex(void);
void must(void);
void ls(void);
void cd_to(char* cd);
void download(char* get);
void upload(char* put); int main(int argc,char* argv[])
{ char c_ip[] = {};
strcpy(c_ip,argv[]); nw = open_network('c',SOCK_STREAM,c_ip,);
if(NULL == nw)
{
printf("open network socket null!\n");
return -;
} printf("Connected to %s.\n",c_ip); nrecv(nw,buf,sizeof(buf));
printf("%s",buf);// for(;;)
{
char user[] = {};
printf("Name (%s:zhizhen):",c_ip);
gets(user); sprintf(buf,"USER %s\n",user);
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
printf("%s",buf);// char pw[] = {};
printf("Password:");
struct termios old, new;
tcgetattr(, &old); // 获取终端属性
new = old;
new.c_lflag &= ~(ECHO | ICANON);// 不使用标准的输出,不显示字符。
tcsetattr(, TCSANOW, &new);// 设置终端新的属性
gets(pw);
tcsetattr(, TCSANOW, &old); sprintf(buf,"PASS %s\n",pw);
nsend(nw,buf,strlen(buf));//pw bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
printf("\n%s",buf);//
if(strstr(buf,"") == NULL)
{
break;
}
} printf("Remote system type is UNIX.\n");
printf("Using binary mode to transfer files.\n"); ex();
must();
char cmd[] = {};
while()
{ printf("ftp> ");
gets(cmd);
if(strcmp(cmd,"bye")==)
{
break;
}
if(strcmp(cmd,"ls")==)
{
ls();
} char *cmd1 = malloc();
char *path = malloc();
sscanf(cmd,"%s %s",cmd1,path);
if(strcmp(cmd1,"cd") == )
{
cd_to(path);
} if(strcmp(cmd1,"get") == )
{
download(path);
} if(strcmp(cmd1,"put") == )
{
upload(path);
} //must();
} printf("221 Goodbye.\n");// } void ex(void)
{
sprintf(buf,"SYST\n");
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
//puts(buf); sprintf(buf,"OPTS UTF8 ON\n");
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
//puts(buf); } void must(void)
{
sprintf(buf,"PWD\n");
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
//puts(buf);// sprintf(buf,"PASV\n");
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
//puts(buf);// unsigned char ip1,ip2,ip3,ip4,port1,port2;
sscanf(strchr(buf,'(')+,"%hhu,%hhu,%hhu,%hhu,%hhu,%hhu",&ip1,&ip2,&ip3,&ip4,&port1,&port2);
sprintf(buf,"%hhu.%hhu.%hhu.%hhu",ip1,ip2,ip3,ip4); NetWork* data_nw = open_network('c',SOCK_STREAM,buf,port1*+port2);
//printf("connect success fd = %d\n",data_nw->fd); sprintf(buf,"LIST -al\n");
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
//printf("%s",buf);// int ret = ;
bzero(buf,sizeof(buf));
while(ret = nrecv(data_nw,buf,sizeof(buf)))
{
//printf("%s",buf);
bzero(buf,sizeof(buf));
}
close_network(data_nw); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
//printf("%s",buf);//
} void ls(void)
{
sprintf(buf,"PWD\n");
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
//puts(buf);// sprintf(buf,"PASV\n");
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
//puts(buf);// unsigned char ip1,ip2,ip3,ip4,port1,port2;
sscanf(strchr(buf,'(')+,"%hhu,%hhu,%hhu,%hhu,%hhu,%hhu",&ip1,&ip2,&ip3,&ip4,&port1,&port2);
sprintf(buf,"%hhu.%hhu.%hhu.%hhu",ip1,ip2,ip3,ip4); NetWork* data_nw = open_network('c',SOCK_STREAM,buf,port1*+port2);
//printf("connect success fd = %d\n",data_nw->fd); sprintf(buf,"LIST -al\n");
nsend(nw,buf,strlen(buf)); printf("200 PORT command successful. Consider using PASV.\n"); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
printf("%s",buf);// int ret = ;
bzero(buf,sizeof(buf));
while(ret = nrecv(data_nw,buf,sizeof(buf)))
{
printf("%s",buf);
bzero(buf,sizeof(buf));
}
close_network(data_nw); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
printf("%s",buf);//
} void cd_to(char* cd)
{
char *dir = cd;
if(strcmp(dir,"..")==)
{
sprintf(buf,"CDUP %s\n",dir);
}
else
{
sprintf(buf,"CWD %s\n",dir);
}
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
printf("%s",buf);//
} void download(char* get)
{
char *filename = get;
sprintf(buf,"TYPE A\n");
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
puts(buf); sprintf(buf,"SIZE %s\n",filename);
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
puts(buf); sprintf(buf,"MDTM %s\n",filename);
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
puts(buf); sprintf(buf,"PASV\n");
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
puts(buf); unsigned char ip1,ip2,ip3,ip4,port1,port2;
sscanf(strchr(buf,'(')+,"%hhu,%hhu,%hhu,%hhu,%hhu,%hhu",&ip1,&ip2,&ip3,&ip4,&port1,&port2);
sprintf(buf,"%hhu.%hhu.%hhu.%hhu",ip1,ip2,ip3,ip4); data_nw = open_network('c',SOCK_STREAM,buf,port1*+port2);
printf("connect success fd = %d\n",data_nw->fd); sprintf(buf,"RETR %s\n",filename);
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
puts(buf); int fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,);
if( > fd)
{
perror("open");
return;
}
int ret = ;
while(ret = nrecv(data_nw,buf,sizeof(buf)))
{
write(fd,buf,ret);
}
close(fd);
} void upload(char* put)
{
char *filename = put; sprintf(buf,"TYPE A\n");
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
puts(buf); sprintf(buf,"SIZE %s\n",filename);
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
puts(buf); sprintf(buf,"PASV\n");
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
puts(buf); unsigned char ip1,ip2,ip3,ip4,port1,port2;
sscanf(strchr(buf,'(')+,"%hhu,%hhu,%hhu,%hhu,%hhu,%hhu",&ip1,&ip2,&ip3,&ip4,&port1,&port2);
sprintf(buf,"%hhu.%hhu.%hhu.%hhu",ip1,ip2,ip3,ip4); data_nw = open_network('c',SOCK_STREAM,buf,port1*+port2);
printf("connect success fd = %d\n",data_nw->fd); sprintf(buf,"STOR %s\n",filename);
nsend(nw,buf,strlen(buf)); int fd = open(filename,O_RDONLY,);
if( > fd)
{
perror("open");
return;
}
int ret = ;
bzero(buf,sizeof(buf));
while(read(fd,buf,))
{
nsend(data_nw,buf,strlen(buf));
bzero(buf,sizeof(buf));
} close_network(data_nw); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
printf("%s",buf);// 150-226 sprintf(buf,"MDTM %s\n",filename);
nsend(nw,buf,strlen(buf)); bzero(buf,sizeof(buf));
nrecv(nw,buf,sizeof(buf));
puts(buf); }
network.h
#ifndef NETWORK_H
#define NETWORK_H
#include <netinet/in.h>
#include <stdint.h> typedef struct sockaddr* SP; typedef struct NetWork
{
int fd; // socket描述符
int type; // 协议类型 SOCK_STREAM/SOCK_DGRAM
socklen_t len; // 地址长度
struct sockaddr_in addr; // 通信地址
}NetWork; // 创建网络连接
NetWork* open_network(char c_or_s,int type,char* ip,uint16_t port); // TCP的server专用
NetWork* accept_network(NetWork* nw); // 发送数据
int nsend(NetWork* nw,void* buf,uint32_t len); // 接收数据
int nrecv(NetWork* nw,void* buf,uint32_t len); // 关闭网络连接
void close_network(NetWork* nw); #endif//NETWORK_H
network.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "network.h" // 创建网络连接
NetWork* open_network(char c_or_s,int type,char* ip,uint16_t port)
{
// 在堆上创建NetWork结构
NetWork* nw = malloc(sizeof(NetWork));
if(NULL == nw)
{
perror("network malloc");
return NULL;
} // 创建socket对象
nw->fd = socket(AF_INET,type,);
if( > nw->fd)
{
perror("network socket");
free(nw);
return NULL;
} // 准备通信地址
nw->addr.sin_family = AF_INET;
nw->addr.sin_port = htons(port);
nw->addr.sin_addr.s_addr = inet_addr(ip);
nw->len = sizeof(nw->addr);
nw->type = type; if('s' == c_or_s)
{
if(bind(nw->fd,(SP)&nw->addr,nw->len))
{
perror("network bind");
free(nw);
return NULL;
} if(SOCK_STREAM == type && listen(nw->fd,))
{
perror("network listen");
free(nw);
return NULL;
}
}
else if(SOCK_STREAM == type)
{
if(connect(nw->fd,(SP)&nw->addr,nw->len))
{
perror("network connect");
free(nw);
return NULL;
}
} return nw;
} // TCP的server专用
NetWork* accept_network(NetWork* nw)
{
if(SOCK_STREAM != nw->type)
{
printf("network accept socket type error!\n");
return NULL;
} NetWork* clinw = malloc(sizeof(NetWork));
if(NULL == clinw)
{
perror("network accept malloc");
return NULL;
} clinw->type = nw->type;
clinw->len = sizeof(clinw->addr);
clinw->fd = accept(nw->fd,(SP)&clinw->addr,&clinw->len);
if( > clinw->fd)
{
perror("network accept");
free(clinw);
return NULL;
} return clinw;
} // 发送数据
int nsend(NetWork* nw,void* buf,uint32_t len)
{
if(SOCK_STREAM == nw->type)
{
return send(nw->fd,buf,len,);
}
else if(SOCK_DGRAM == nw->type)
{
return sendto(nw->fd,buf,len,,(SP)&nw->addr,nw->len);
}
return -;
} // 接收数据
int nrecv(NetWork* nw,void* buf,uint32_t len)
{
if(SOCK_STREAM == nw->type)
{
return recv(nw->fd,buf,len,);
}
else if(SOCK_DGRAM == nw->type)
{
return recvfrom(nw->fd,buf,len,,(SP)&nw->addr,&nw->len);
}
return -;
} // 关闭网络连接
void close_network(NetWork* nw)
{
if(close(nw->fd))
{
perror("network close");
}
free(nw);
}
---------------------------------------------------------------------------------------------------------------------------------------------
Qt版(有界面)
内容比较多,可以直接看完整的项目
下面我们看看运行效果
完整的文件如下
在贴一个关键的代码好了
widget.cpp
#include <iostream>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "widget.h"
#include "ui_widget.h" using namespace std; Widget::Widget(QWidget *parent) : QWidget(parent),ui(new Ui::Widget)
{
ui->setupUi(this); QPalette pal = this->palette(); // 设置背景图片
pal.setBrush(QPalette::Background,QBrush(QPixmap(":/icon/bg.png")));
setPalette(pal); download = false;
upload = false; tcpSocket = new QTcpSocket;
tcpSocket2 = new QTcpSocket;
connect(tcpSocket,SIGNAL(connected()),this,SLOT(connect_success())); // 关联接收连接信号与槽函数
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(recv_msg()));
connect(tcpSocket2,SIGNAL(connected()),this,SLOT(connect_success2()));
connect(tcpSocket2,SIGNAL(readyRead()),this,SLOT(recv_msg2())); strcpy(clientdir,".");
strcpy(serverdir,".");
show_clientdir(); // 刚开始就显示客户端目录
} Widget::~Widget()
{
delete ui;
} // 显示客户端的所有文件
void Widget::show_clientdir()
{
ui->clientdir->clear(); // 清空clientdir
char cur_dir[];
char* temp = getcwd(cur_dir, sizeof(cur_dir)-); // 获取当前路径
if(temp == NULL)
{
qDebug("获取当前路径失败");
} qDebug("%s",cur_dir);
ui->clientdir->setText(cur_dir); // 设置clientdir的text int i = ;
DIR* dp = opendir(clientdir);
for(struct dirent* file = readdir(dp); file!=NULL; file = readdir(dp))
{
client_filename[i++] = file->d_name; char img[] = {}; strcpy(img,":/icon/");
if(file->d_type == DT_DIR) // 判断是否是目录文件
{
strcat(img,"dir.png");
//qDebug("dir");
}
else
{
strcat(img,"file.png");
}
QIcon icon(img);
QListWidgetItem* item = new QListWidgetItem(icon,file->d_name);
ui->listWidget_c->addItem(item); // 添加字段
}
} // 显示服务端的所有文件
void Widget::show_serverdir()
{
char list[] = {};
sprintf(list,"LIST -al\n");
tcpSocket->write(list,strlen(list));
tcpSocket->waitForBytesWritten();
} // 点击connect按钮
void Widget::on_connect_clicked()
{
QString _connect = ui->connect->text();
if(_connect != "连接")
{
tcpSocket->close();
ui->connect->setText("连接");
ui->serverdir->clear();
ui->listWidget_s->clear();
return;
} QString _ip = ui->ip->text(); // 获取控件的text
QString _port = ui->port->text(); const char* ip = _ip.toStdString().c_str();
short port = _port.toShort(); tcpSocket->connectToHost(ip,port); // 连接 char username[] = {};
sprintf(username,"USER %s\n",ui->username->text().toStdString().c_str());
tcpSocket->write(username,strlen(username)); // 发送命令
tcpSocket->waitForBytesWritten(); //std::cout << "--" <<username <<"--" << endl; char password[] = {};
sprintf(password,"PASS %s\n",ui->password->text().toStdString().c_str());
tcpSocket->write(password,strlen(password));
tcpSocket->waitForBytesWritten(); char syst[] = {};
sprintf(syst,"SYST\n");
tcpSocket->write(syst,strlen(syst));
tcpSocket->waitForBytesWritten(); char opts[] = {};
sprintf(opts,"OPTS UTF8 ON\n");
tcpSocket->write(opts,strlen(opts));
tcpSocket->waitForBytesWritten(); char pwd[] = {};
sprintf(pwd,"PWD\n");
tcpSocket->write(pwd,strlen(pwd));
tcpSocket->waitForBytesWritten(); char pasv[] = {};
sprintf(pasv,"PASV\n");
tcpSocket->write(pasv,strlen(pasv));
tcpSocket->waitForBytesWritten(); char list[] = {};
sprintf(list,"LIST -al\n");
tcpSocket->write(list,strlen(list));
tcpSocket->waitForBytesWritten(); ui->connect->setText("断开");
} // 初始端口
void Widget::recv_msg()
{
char buf1[] = {};
tcpSocket->read(buf1,sizeof(buf1)); // 接收数据
qDebug("%s",buf1);
bzero(buf,sizeof(buf));
strcpy(buf,buf1);
//qDebug("%s",buf); // PASV 227
if(strstr(buf1,"") != NULL)
{
int ip1,ip2,ip3,ip4,port1,port2; // 解析第二个端口号
sscanf(strchr(buf,'(')+,"%d,%d,%d,%d,%d,%d",&ip1,&ip2,&ip3,&ip4,&port1,&port2); //std::cout << "port1:" << port1 << ",port2:"<< port2 << endl; int port3 = port1*+port2;
QString _ip = ui->ip->text();
const char* ip = _ip.toStdString().c_str(); tcpSocket2->connectToHost(ip,port3); // 连接
//std::cout << "port:"<<port3<<",socket2 connect" << endl;
} // PWD 257
if(strstr(buf1,"") != NULL)
{
char* str_pwd = (strpbrk(buf1,"\"")+); // 获取最后的字符串 char* str1 = (strchr(str_pwd,'"')); // 获取最后的字符串
str1[] = '\0'; str_pwd[strlen(str_pwd)] = '\0'; // 用\0替换\n
QByteArray byte(str_pwd); // 解决中文乱码问题
QString dir(byte);
ui->serverdir->setText(dir);
} // 150 down
if(strstr(buf1,"150 Opening BINARY mode data connection for") != NULL)
{
download = true;
}
// 150 up
if(strstr(buf1,"150 Ok to send data.") != NULL)
{
qDebug("收到150");
upload = true; int fd = open(file_name,O_RDONLY); qDebug("开始上传");
char buf3[] = {};
while(read(fd,buf3,) != ) // 一次上传1字节
{
//qDebug("%s",buf3);
tcpSocket2->write(buf3,strlen(buf3));
bzero(buf3,sizeof(buf3));
} qDebug("上传完毕");
upload = false;
tcpSocket2->close();
}
//
if(strstr(buf1,"226 Transfer complete.") != NULL)
{
download = false;
upload = false;
ui->listWidget_c->clear(); // 清空listWidget_c
show_clientdir();
} bzero(buf1,sizeof(buf1)); } // socket1连接成功
void Widget::connect_success()
{
qDebug("connected()");
} // tcpSocket2接收到消息
void Widget::recv_msg2()
{
ui->listWidget_s->clear(); // 清空listWidget_s
int i = ;
while()
{
char buf2[]={};
tcpSocket2->readLine(buf2,sizeof(buf2)); // 接收数据
//qDebug("%s",buf2); if(strlen(buf2) <= )
{
break;
}
// 读取命令LIST -al返回的信息
if(buf2[] == 'r' && (buf2[] == '-'||buf2[] == 'd'))
{
char img[] = {}; strcpy(img,":/icon/");
if(buf2[] == 'd') // 判断是否是目录文件
{
strcat(img,"dir.png");
//qDebug("dir");
}
else
{
strcat(img,"file.png");
}
QIcon icon(img); char* str = (strrchr(buf2,' ')+); // 获取最后的字符串
str[strlen(str)-] = '\0'; // 用\0替换\n
QByteArray byte(str); // 解决中文乱码问题
QString filename(byte);
server_filename[i++] = filename;
QListWidgetItem* item = new QListWidgetItem(icon,filename);
ui->listWidget_s->addItem(item); // 添加字段
} if(download == true)
{
// 读取下载的信息,写入文件
int fd = open(file_name,O_WRONLY|O_CREAT|O_APPEND,);
//qDebug("%s",buf2);
int ret = write(fd,buf2,strlen(buf2));
if(ret == )
{
qDebug("写入完毕");
}
} }
tcpSocket2->close();
} // socket2连接成功
void Widget::connect_success2()
{
qDebug("connected()");
} // client的listWidget双击
void Widget::on_listWidget_c_doubleClicked(const QModelIndex &index)
{
qDebug("%d",index.row()); // 双击的index DIR* dp = opendir(".");
struct dirent* file = readdir(dp);
QString str; // 存储文件名
if(index.row() == )
{
str = file->d_name;
}
for(int i=; i<index.row(); i++)
{
file = readdir(dp);
if(i == index.row()-)
{
str = file->d_name;
}
} QByteArray byte = str.toAscii();
char* filename = byte.data(); // 文件名
qDebug("%s",filename); int ret = chdir(filename); // 修改工作目录
if(ret == -)
{
qDebug("chdir失败");
} ui->listWidget_c->clear(); // 清空
show_clientdir(); } // server的listWidget双击
void Widget::on_listWidget_s_doubleClicked(const QModelIndex &index)
{
qDebug("%d",index.row()); // 双击的index QString str; // 存储文件名
str = server_filename[index.row()];
QByteArray byte = str.toAscii();
char* filename = byte.data(); // 文件名
qDebug("%s",filename); char cwd[] = {};
sprintf(cwd,"CWD %s\n",filename);
tcpSocket->write(cwd,strlen(cwd));
tcpSocket->waitForBytesWritten(); char pwd[] = {};
sprintf(pwd,"PWD\n");
tcpSocket->write(pwd,strlen(pwd));
tcpSocket->waitForBytesWritten(); char pasv[] = {};
sprintf(pasv,"PASV\n");
tcpSocket->write(pasv,strlen(pasv));
tcpSocket->waitForBytesWritten(); char list[] = {};
sprintf(list,"LIST -al\n");
tcpSocket->write(list,strlen(list));
tcpSocket->waitForBytesWritten(); } // 点击 << 下载按钮
void Widget::on_left_clicked()
{
download = true; QString str1 = server_filename[ui->listWidget_s->currentRow()]; // 获取选中的文件名
QByteArray byte = str1.toAscii();
char* temp = byte.data();
char filename[] = {};
strcpy(file_name,temp); // 复制给全局变量
strcpy(filename,temp);
qDebug("%s",filename); char typea[] = {};
sprintf(typea,"TYPE A\n");
tcpSocket->write(typea,strlen(typea));
tcpSocket->waitForBytesWritten(); char size[] = {};
sprintf(size,"SIZE %s\n",filename);
tcpSocket->write(size,strlen(size));
tcpSocket->waitForBytesWritten(); char mdtm[] = {};
sprintf(mdtm,"MDTM %s\n",filename);
tcpSocket->write(mdtm,strlen(mdtm));
tcpSocket->waitForBytesWritten(); char pasv[] = {};
sprintf(pasv,"PASV\n");
tcpSocket->write(pasv,strlen(pasv));
tcpSocket->waitForBytesWritten(); char retr[] = {};
sprintf(retr,"RETR %s\n",filename);
tcpSocket->write(retr,strlen(retr));
tcpSocket->waitForBytesWritten(); char pwd[] = {};
sprintf(pwd,"PWD\n");
tcpSocket->write(pwd,strlen(pwd));
tcpSocket->waitForBytesWritten(); char pasv1[] = {};
sprintf(pasv1,"PASV\n");
tcpSocket->write(pasv1,strlen(pasv1));
tcpSocket->waitForBytesWritten(); char list[] = {};
sprintf(list,"LIST -al\n");
tcpSocket->write(list,strlen(list));
tcpSocket->waitForBytesWritten(); } // 点击 >> 上传按钮
void Widget::on_right_clicked()
{
QString str1 = client_filename[ui->listWidget_c->currentRow()]; // 获取选中的文件名
QByteArray byte = str1.toAscii();
char* temp = byte.data();
char filename[] = {};
strcpy(file_name,temp); // 复制给全局变量
strcpy(filename,temp);
qDebug("%s",filename); int fd = open(filename,O_RDONLY,);
if(fd < )
{
qDebug("不是文件,上传失败");
upload = false;
return;
} char typea[] = {};
sprintf(typea,"TYPE A\n");
tcpSocket->write(typea,strlen(typea));
tcpSocket->waitForBytesWritten(); char size[] = {};
sprintf(size,"SIZE %s\n",filename);
tcpSocket->write(size,strlen(size));
tcpSocket->waitForBytesWritten(); char pasv[] = {};
sprintf(pasv,"PASV\n");
tcpSocket->write(pasv,strlen(pasv));
tcpSocket->waitForBytesWritten(); char stor[] = {};
sprintf(stor,"STOR %s\n",filename);
tcpSocket->write(stor,strlen(stor));
tcpSocket->waitForBytesWritten(); while(upload == true)
{
usleep();
qDebug("while...");
} char mdtm[] = {};
sprintf(mdtm,"MDTM %s\n",filename);
tcpSocket->write(mdtm,strlen(mdtm));
tcpSocket->waitForBytesWritten(); char pwd[] = {};
sprintf(pwd,"PWD\n");
tcpSocket->write(pwd,strlen(pwd));
tcpSocket->waitForBytesWritten(); char pasv1[] = {};
sprintf(pasv1,"PASV\n");
tcpSocket->write(pasv1,strlen(pasv1));
tcpSocket->waitForBytesWritten(); char list[] = {};
sprintf(list,"LIST -al\n");
tcpSocket->write(list,strlen(list));
tcpSocket->waitForBytesWritten(); } // 客户端目录回车
void Widget::on_clientdir_returnPressed()
{
QString str = ui->clientdir->text(); // 存储文件名
QByteArray byte = str.toAscii();
char* filename = byte.data(); // 文件名
qDebug("%s",filename); int ret = chdir(filename); // 修改工作目录
if(ret == -)
{
qDebug("chdir失败");
} ui->listWidget_c->clear(); // 清空
show_clientdir();
} // 服务端目录回车
void Widget::on_serverdir_returnPressed()
{
QString str = ui->serverdir->text(); // 存储文件名
QByteArray byte = str.toAscii();
char* filename = byte.data(); // 文件名
qDebug("%s",filename); char cwd[] = {};
sprintf(cwd,"CWD %s\n",filename);
tcpSocket->write(cwd,strlen(cwd));
tcpSocket->waitForBytesWritten(); char pwd[] = {};
sprintf(pwd,"PWD\n");
tcpSocket->write(pwd,strlen(pwd));
tcpSocket->waitForBytesWritten(); char pasv[] = {};
sprintf(pasv,"PASV\n");
tcpSocket->write(pasv,strlen(pasv));
tcpSocket->waitForBytesWritten(); char list[] = {};
sprintf(list,"LIST -al\n");
tcpSocket->write(list,strlen(list));
tcpSocket->waitForBytesWritten();
}
C/C++、Qt4实现FTP客户端(有无界面版)的更多相关文章
- ClassicFTP for Mac(FTP 客户端)破解版安装
1.软件简介 ClassicFTP 是 macOS 系统上一款易于使用的 FTP 客户端,让您能够从远程服务器(网站)或网络查看,编辑,上传,下载和删除文件的免费的软件.Mac 下的一款使用 F ...
- 【RL-TCPnet网络教程】第37章 RL-TCPnet之FTP客户端
第37章 RL-TCPnet之FTP客户端 本章节为大家讲解RL-TCPnet的FTP客户端应用,学习本章节前,务必要优先学习第35章的FTP基础知识.有了这些基础知识之后,再搞本章节会有事 ...
- Jenkins结合.net平台之ftp客户端
上一节我们讲解了如何配置ftp服务端,本节我们讲解如何使用winscp搭建ftp客户端,为什么使用winscp而不是filezilla客户端版,前面我们简单说过,这里不再赘述. 下载winscp以后我 ...
- Linux FTP客户端
1.File Zilla File Zilla是一个开源的,跨平台的Linux FTP客户端.File Zilla有一个标签式的用户界面,允许用户查看正在传输的文件的所有细节.File Zilla是通 ...
- 使用 Socket 通信实现 FTP 客户端程序(来自IBM)
FTP 客户端如 FlashFXP,File Zilla 被广泛应用,原理上都是用底层的 Socket 来实现.FTP 客户端与服务器端进行数据交换必须建立两个套接字,一个作为命令通道,一个作为数据通 ...
- Socket网络编程--FTP客户端
Socket网络编程--FTP客户端(1)(Windows) 已经好久没有写过博客进行分享了.具体原因,在以后说. 这几天在了解FTP协议,准备任务是写一个FTP客户端程序.直接上干货了. 0.了解F ...
- ftp客户端命令使用简记
OS:windows8.1评估版 程序和功能 tftp客户端勾选上 Win+R:运行,键入cmd,键入ftp -help 如下图: 使用ftp客户端可以做的事:将文件传送到运行FTP服务器服务(经常称 ...
- Socket网络编程--FTP客户端(1)(Windows)
已经好久没有写过博客进行分享了.具体原因,在以后说. 这几天在了解FTP协议,准备任务是写一个FTP客户端程序.直接上干货了. 0.了解FTP作用 就是一个提供一个文件的共享协议. 1.了解FTP协议 ...
- 用edtftpj实现Java FTP客户端工具
edtftpj是一个java FTP工具包,使用非常方便,感觉比Apache的好用,但Apache更灵活.edtftpj有多种版本,分别是java..net和js版本.对于Java版的有一个免费版本. ...
随机推荐
- Nuxt + Vue 全家桶
引子 情由无中有,一旦有了,便万劫不复 简介 “简单却不失优雅,小巧而不乏大匠”. Vue.js 是一个JavaScriptMVVM库,是一套构建用户界面的渐进式框架.它是以数据驱动和组件化的思想构建 ...
- December Challenge 2019 Division 1 题解
传送门 当我打开比赛界面的时候所有题目都已经被一血了-- BINXOR 直接把异或之后二进制最多和最少能有多少个\(1\)算出来,在这个范围内枚举,组合数算一下就行了.注意\(1\)的个数是\(2\) ...
- IIS 7中添加匿名访问FTP站点
1. 开启FTP和IIS服务: 2.打开IIS 管理器: 我电脑上是IIS 7.5 ,所以选择第一个并点击打开哦. 如果你想知道自己IIS的版本,打开帮助菜单: 3. 新建FTP站点: 4. 填写站点 ...
- Android 自己实现更新下载自动安装
1.一些公司开发完一款App之后可能并不会去上架App商店,但事后期也需要定时进行维护更新,所以会选择把打包好的apk 发布到自己的服务器,然后在数据库建一个版本号的表,然后剩下的就交给你androi ...
- 【Node.js】Node.js的调试
目录结构: contents structure [-] 使用console.log() 使用Chrome DevTools 使用Visual Studio Code 与JavaScript运行在浏览 ...
- [译]在Python中,如何拆分字符串并保留分隔符?
原文来源:https://stackoverflow.com/questions/2136556/in-python-how-do-i-split-a-string-and-keep-the-sepa ...
- windows系统下设置mtu值的方法
说起 MTU 值,可能许多朋友连听都没听说过.简单来说,我们上网时所进行的操作,都是通过传输一个又一个“数据包”来实现的,而 MTU 值就是用来设定可传输数据包的最大尺寸的.很显然,MTU 值设置得过 ...
- SpringMVC基本
SpringMVC和Struts2的区别 SpringMVC与Struts2区别 对比项目 SrpingMVC Struts2 优势 国内市场情况 有大量用户,一般新项目启动都会选用springmvc ...
- Nginx学习之入门
1. 概念 (1) 什么是nginx? Nginx (engine x) 是一款轻量级的Web 服务器 .反向代理服务器及电子邮件(IMAP/POP3)代理服务器. (2) 什么是反向代 ...
- Java 动态代理与AOP
动态代理与AOP 代理模式 代理模式给某一个目标对象(target)提供代理对象(proxy),并由代理对象控制对target对象的引用. 模式图: 代理模式中的角色有: 抽象对象角色(Abstrac ...