操作系统:Ubuntu 12.04 LTS

开发工具:GNU4.6.3,C/C++标准库,Qt4,Qt Creator Documentation 2.4.1

码云:传送门,GitHub:传送门

相关知识点参考:

网络通信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客户端(有无界面版)的更多相关文章

  1. ClassicFTP for Mac(FTP 客户端)破解版安装

    1.软件简介    ClassicFTP 是 macOS 系统上一款易于使用的 FTP 客户端,让您能够从远程服务器(网站)或网络查看,编辑,上传,下载和删除文件的免费的软件.Mac 下的一款使用 F ...

  2. 【RL-TCPnet网络教程】第37章 RL-TCPnet之FTP客户端

    第37章      RL-TCPnet之FTP客户端 本章节为大家讲解RL-TCPnet的FTP客户端应用,学习本章节前,务必要优先学习第35章的FTP基础知识.有了这些基础知识之后,再搞本章节会有事 ...

  3. Jenkins结合.net平台之ftp客户端

    上一节我们讲解了如何配置ftp服务端,本节我们讲解如何使用winscp搭建ftp客户端,为什么使用winscp而不是filezilla客户端版,前面我们简单说过,这里不再赘述. 下载winscp以后我 ...

  4. Linux FTP客户端

    1.File Zilla File Zilla是一个开源的,跨平台的Linux FTP客户端.File Zilla有一个标签式的用户界面,允许用户查看正在传输的文件的所有细节.File Zilla是通 ...

  5. 使用 Socket 通信实现 FTP 客户端程序(来自IBM)

    FTP 客户端如 FlashFXP,File Zilla 被广泛应用,原理上都是用底层的 Socket 来实现.FTP 客户端与服务器端进行数据交换必须建立两个套接字,一个作为命令通道,一个作为数据通 ...

  6. Socket网络编程--FTP客户端

    Socket网络编程--FTP客户端(1)(Windows) 已经好久没有写过博客进行分享了.具体原因,在以后说. 这几天在了解FTP协议,准备任务是写一个FTP客户端程序.直接上干货了. 0.了解F ...

  7. ftp客户端命令使用简记

    OS:windows8.1评估版 程序和功能 tftp客户端勾选上 Win+R:运行,键入cmd,键入ftp -help 如下图: 使用ftp客户端可以做的事:将文件传送到运行FTP服务器服务(经常称 ...

  8. Socket网络编程--FTP客户端(1)(Windows)

    已经好久没有写过博客进行分享了.具体原因,在以后说. 这几天在了解FTP协议,准备任务是写一个FTP客户端程序.直接上干货了. 0.了解FTP作用 就是一个提供一个文件的共享协议. 1.了解FTP协议 ...

  9. 用edtftpj实现Java FTP客户端工具

    edtftpj是一个java FTP工具包,使用非常方便,感觉比Apache的好用,但Apache更灵活.edtftpj有多种版本,分别是java..net和js版本.对于Java版的有一个免费版本. ...

随机推荐

  1. promethues exporter+ grafana 监控pg+mysql

    这篇文章本来是打算使用pmm 进行数据库监控的,但是居然参考官方文档使用docker 运行起来有点问题,所以直接改用 exporter 进行处理,但是比pmm 弱好多 pmm 的参考架构 说明,以上图 ...

  2. 3.学习SpringMVC注解深入

    一.SpringMVC注解: 1.RequestParam注解: 其中required属性默认为true(必须得传而且传的名字一样),为false时可以不传. 编写jsp代码: <a href= ...

  3. AGC010

    AGC010 A [过水已隐藏] B 这题推完了还是不会/kk真的毒瘤 考虑每次会减少的总和是\(n(n+1)/2\),用原来的和除以这个可以得到操作次数\(m\)(不是整数无解) 再考虑相邻两个数\ ...

  4. 【Beta】Scrum meeting 6

    目录 写在前面 进度情况 任务进度表 Beta-1阶段燃尽图 遇到的困难 照片 commit记录截图 小程序前端仓库 后端代码仓库 技术博客 写在前面 例会时间:5.10 22:30-22:50 例会 ...

  5. 日常开发中的shell小技巧

    工具推荐 命令行中很方便的代码统计工具---cloc 强大的分屏工具---tmux 最舒服的markdown书写工具---typora markdown图床推荐--七牛云 模拟生成熵(避免暴力手搓键盘 ...

  6. Druid: A Real-time Analytical Data Store

    Druid一种实时数仓,针对的场景和目的,如下比较明确 Druid was originally designed to solve problems around ingesting and exp ...

  7. 坐标转换7参数计算工具——arcgis 地理处理工具案例教程

    坐标转换7参数计算工具--arcgis 地理处理工具案例教程 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 不接受个人免费咨询. 提供API,独 ...

  8. centos6.9安装python3.6.9独立的virtualenv环境,并且能正确引入ssl

    centos6.9安装python3.6.9独立的virtualenv环境,并且能正确引入ssl 1.编译安装python3.6环境# 安装依赖yum -y install zlib-devel bz ...

  9. java 迭代的陷阱

    /** * 关于迭代器,有一种常见的误用,就是在迭代的中间调用容器的删除方法. * 但运行时会抛出异常:java.util.ConcurrentModificationException * * 发生 ...

  10. EasyNVR摄像机网页无插件直播方案H5前端构建之:bootstrap弹窗功能的实现方案与代码

    前言介绍 在web前端的网页设计中,为了展示出简洁的网页风格和美观的效果,往往会使用弹窗效果在EasyNVR前端页面录像检索功能时,必然会播放录像,如果单独为播放录像文件排一个界面,用户在使用上会更加 ...