操作系统: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();
}

最新文章

  1. pgsql 建数据库注意事项
  2. JQuery随笔
  3. 自定义TextField清除按钮
  4. AngularJS之Provider, Value, Constant, Service, Factory, Decorator的区别与详解
  5. HDU1019
  6. linux samba 服务器 简单配置
  7. C#委托之泛型
  8. 关于WebView的内存泄露问题
  9. APT攻击将向云计算平台聚焦
  10. MR 的 mapper 数量问题
  11. wine下汉字方块解决
  12. gpexpand error:Do not have enough valid segments to start the array.
  13. [国嵌攻略][048][MMU配置与使用]
  14. APK Multi-Tool强大的APK反编译工具终极教程
  15. [COGS 2287][HZOI 2015]疯狂的机器人
  16. Linux 常用性能分析命令
  17. 集合之HashMap(含JDK1.8源码分析)
  18. OracleDBconsoleorcl服务无法启动的原因及解决思路
  19. UI5-学习篇-17-云端WEB IDE开发
  20. Intellij IDEA调试功能总结

热门文章

  1. 全部文章&gt; Maven
  2. 一起学Makefile(六)
  3. shell 求数组的平均值,求和,最大值,最小值
  4. SpringBoot:缓存注解@Cacheable详解
  5. SpringBoot:使用Jenkins自动部署SpringBoot项目(一)环境准备
  6. Cisco路由器用SSH替代Telnet连接
  7. win10 关闭开机密码
  8. Zookeeper请求处理原理分析
  9. web程序设计关于我们
  10. CentOS7安装及配置vsftpd (FTP服务器FTP账号创建以及权限设置)