C/C++、Qt4实现FTP客户端(有无界面版)

操作系统:Ubuntu 12.04 LTS

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

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

相关知识点参考:

网络通信TCP/UDP——学习笔记,Qt相关知识(二)——学习笔记

C语言无界面版本:

直接上效果

C/C++、Qt4实现FTP客户端(有无界面版)_第1张图片

接下来操作和ftp基本操作一致

我一共分了3个文件

我们简单看下代码

ftpclient.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "network.h"

NetWork* nw;
NetWork* data_nw;
int list_len = 0;
char buf[256] = {};

typedef struct List
{
	char filename[40];
}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[40] = {};
	strcpy(c_ip,argv[1]);

	nw = open_network('c',SOCK_STREAM,c_ip,21);
	if(NULL == nw)
	{
		printf("open network socket null!\n");
		return -1;
	}

	printf("Connected to %s.\n",c_ip);

	nrecv(nw,buf,sizeof(buf));
	printf("%s",buf);//220

	for(;;)
	{
		char user[20] = {};
		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);//331

		char pw[20] = {};
		printf("Password:");
		struct termios old, new; 
		tcgetattr(0, &old);  // 获取终端属性
		new = old;	
		new.c_lflag &= ~(ECHO | ICANON);// 不使用标准的输出,不显示字符。 
 		tcsetattr(0, TCSANOW, &new);// 设置终端新的属性
		gets(pw);								
		tcsetattr(0, 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);//230
		if(strstr(buf,"530") == NULL)
		{
			break;
		}
	}

	printf("Remote system type is UNIX.\n");
	printf("Using binary mode to transfer files.\n");
	
	ex();
	must();
	char cmd[40] = {};
	while(1)
	{

		printf("ftp> ");
		gets(cmd);
		if(strcmp(cmd,"bye")==0)
		{
			break;
		}
		if(strcmp(cmd,"ls")==0)
		{
			ls();
		}

		char *cmd1 = malloc(20);
		char *path = malloc(100);
		sscanf(cmd,"%s %s",cmd1,path);
		if(strcmp(cmd1,"cd") == 0)
		{
			cd_to(path);
		}

		if(strcmp(cmd1,"get") == 0)
		{
			download(path);
		}

		if(strcmp(cmd1,"put") == 0)
		{
			upload(path);
		}
		
		//must();
	}

	printf("221 Goodbye.\n");//221


}

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);//257

	sprintf(buf,"PASV\n");
	nsend(nw,buf,strlen(buf));

	bzero(buf,sizeof(buf));
	nrecv(nw,buf,sizeof(buf));
	//puts(buf);//227

	unsigned char ip1,ip2,ip3,ip4,port1,port2;
	sscanf(strchr(buf,'(')+1,"%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*256+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);//150
	
	int ret = 0;
	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);//226
}

void ls(void)
{
	sprintf(buf,"PWD\n");
	nsend(nw,buf,strlen(buf));

	bzero(buf,sizeof(buf));
	nrecv(nw,buf,sizeof(buf));
	//puts(buf);//257

	sprintf(buf,"PASV\n");
	nsend(nw,buf,strlen(buf));

	bzero(buf,sizeof(buf));
	nrecv(nw,buf,sizeof(buf));
	//puts(buf);//227

	unsigned char ip1,ip2,ip3,ip4,port1,port2;
	sscanf(strchr(buf,'(')+1,"%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*256+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);//150
	
	int ret = 0;
	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);//226
}

void cd_to(char* cd)
{
	char *dir = cd;
	if(strcmp(dir,"..")==0)
	{
		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);//250
}

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,'(')+1,"%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*256+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,0644);
	if(0 > fd)
	{
		perror("open");
		return;
	}
	int ret = 0;
	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,'(')+1,"%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*256+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,0644);
	if(0 > fd)
	{
		perror("open");
		return;
	}
	int ret = 0;
	bzero(buf,sizeof(buf));
	while(read(fd,buf,1))
	{
		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 
#include 

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 
#include 
#include 
#include 
#include 
#include 
#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,0);
	if(0 > 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,50))
		{
			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(0 > 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,0);
	}
	else if(SOCK_DGRAM == nw->type)
	{
		return sendto(nw->fd,buf,len,0,(SP)&nw->addr,nw->len);
	}
	return -1;
}

// 接收数据
int nrecv(NetWork* nw,void* buf,uint32_t len)
{
	if(SOCK_STREAM == nw->type)
	{
		return recv(nw->fd,buf,len,0);
	}
	else if(SOCK_DGRAM == nw->type)
	{
		return recvfrom(nw->fd,buf,len,0,(SP)&nw->addr,&nw->len);
	}
	return -1;
}

// 关闭网络连接
void close_network(NetWork* nw)
{
	if(close(nw->fd))
	{
		perror("network close");
	}
	free(nw);
}

---------------------------------------------------------------------------------------------------------------------------------------------

Qt版(有界面)

内容比较多,可以直接看完整的项目

下面我们看看运行效果

C/C++、Qt4实现FTP客户端(有无界面版)_第2张图片

完整的文件如下

C/C++、Qt4实现FTP客户端(有无界面版)_第3张图片

在贴一个关键的代码好了

widget.cpp

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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[1024];
    char* temp = getcwd(cur_dir, sizeof(cur_dir)-1); // 获取当前路径
    if(temp == NULL)
    {
        qDebug("获取当前路径失败");
    }

    qDebug("%s",cur_dir);
    ui->clientdir->setText(cur_dir);    // 设置clientdir的text


    int i = 0;
    DIR* dp = opendir(clientdir);
    for(struct dirent* file = readdir(dp); file!=NULL; file = readdir(dp))
    {
        client_filename[i++] = file->d_name;

        char img[256] = {};

        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[20] = {};
    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[60] = {};
    sprintf(username,"USER %s\n",ui->username->text().toStdString().c_str());
    tcpSocket->write(username,strlen(username));    // 发送命令
    tcpSocket->waitForBytesWritten();

    //std::cout << "--" <password->text().toStdString().c_str());
    tcpSocket->write(password,strlen(password));
    tcpSocket->waitForBytesWritten();

    char syst[20] = {};
    sprintf(syst,"SYST\n");
    tcpSocket->write(syst,strlen(syst));
    tcpSocket->waitForBytesWritten();

    char opts[20] = {};
    sprintf(opts,"OPTS UTF8 ON\n");
    tcpSocket->write(opts,strlen(opts));
    tcpSocket->waitForBytesWritten();

    char pwd[20] = {};
    sprintf(pwd,"PWD\n");
    tcpSocket->write(pwd,strlen(pwd));
    tcpSocket->waitForBytesWritten();

    char pasv[20] = {};
    sprintf(pasv,"PASV\n");
    tcpSocket->write(pasv,strlen(pasv));
    tcpSocket->waitForBytesWritten();


    char list[20] = {};
    sprintf(list,"LIST -al\n");
    tcpSocket->write(list,strlen(list));
    tcpSocket->waitForBytesWritten();

    ui->connect->setText("断开");
}

// 初始端口
void Widget::recv_msg()
{
    char buf1[1024] = {};
    tcpSocket->read(buf1,sizeof(buf1));   // 接收数据
    qDebug("%s",buf1);
    bzero(buf,sizeof(buf));
    strcpy(buf,buf1);
    //qDebug("%s",buf);

    // PASV 227
    if(strstr(buf1,"227") != NULL)
    {
        int ip1,ip2,ip3,ip4,port1,port2;    // 解析第二个端口号
        sscanf(strchr(buf,'(')+1,"%d,%d,%d,%d,%d,%d",&ip1,&ip2,&ip3,&ip4,&port1,&port2);

        //std::cout << "port1:" << port1 << ",port2:"<< port2 << endl;

        int port3 = port1*256+port2;
        QString _ip = ui->ip->text();
        const char* ip = _ip.toStdString().c_str();

        tcpSocket2->connectToHost(ip,port3);    // 连接
        //std::cout << "port:"<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[10] = {};
        while(read(fd,buf3,1) != 0) // 一次上传1字节
        {
            //qDebug("%s",buf3);
            tcpSocket2->write(buf3,strlen(buf3));
            bzero(buf3,sizeof(buf3));
        }

        qDebug("上传完毕");
        upload = false;
        tcpSocket2->close();
    }
    // 226
    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 = 0;
    while(1)
    {
        char buf2[1024]={};
        tcpSocket2->readLine(buf2,sizeof(buf2));   // 接收数据
        //qDebug("%s",buf2);

        if(strlen(buf2) <= 0)
        {
            break;
        }
        // 读取命令LIST -al返回的信息
        if(buf2[1] == 'r' && (buf2[0] == '-'||buf2[0] == 'd'))
        {
            char img[256] = {};

            strcpy(img,":/icon/");
            if(buf2[0] == 'd')  // 判断是否是目录文件
            {
                strcat(img,"dir.png");
                //qDebug("dir");
            }
            else
            {
                strcat(img,"file.png");
            }
            QIcon icon(img);

            char* str = (strrchr(buf2,' ')+1);  // 获取最后的字符串
            str[strlen(str)-2] = '\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,0666);
            //qDebug("%s",buf2);
            int ret = write(fd,buf2,strlen(buf2));
            if(ret == 0)
            {
                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() == 0)
    {
        str = file->d_name;
    }
    for(int i=0; id_name;
        }
    }

    QByteArray byte = str.toAscii();
    char* filename =  byte.data();   // 文件名
    qDebug("%s",filename);

    int ret = chdir(filename);    // 修改工作目录
    if(ret == -1)
    {
        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[40] = {};
    sprintf(cwd,"CWD %s\n",filename);
    tcpSocket->write(cwd,strlen(cwd));
    tcpSocket->waitForBytesWritten();

    char pwd[20] = {};
    sprintf(pwd,"PWD\n");
    tcpSocket->write(pwd,strlen(pwd));
    tcpSocket->waitForBytesWritten();


    char pasv[20] = {};
    sprintf(pasv,"PASV\n");
    tcpSocket->write(pasv,strlen(pasv));
    tcpSocket->waitForBytesWritten();

    char list[20] = {};
    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[60] = {};
    strcpy(file_name,temp); // 复制给全局变量
    strcpy(filename,temp);
    qDebug("%s",filename);

    char typea[40] = {};
    sprintf(typea,"TYPE A\n");
    tcpSocket->write(typea,strlen(typea));
    tcpSocket->waitForBytesWritten();

    char size[60] = {};
    sprintf(size,"SIZE %s\n",filename);
    tcpSocket->write(size,strlen(size));
    tcpSocket->waitForBytesWritten();

    char mdtm[60] = {};
    sprintf(mdtm,"MDTM %s\n",filename);
    tcpSocket->write(mdtm,strlen(mdtm));
    tcpSocket->waitForBytesWritten();

    char pasv[20] = {};
    sprintf(pasv,"PASV\n");
    tcpSocket->write(pasv,strlen(pasv));
    tcpSocket->waitForBytesWritten();


    char retr[60] = {};
    sprintf(retr,"RETR %s\n",filename);
    tcpSocket->write(retr,strlen(retr));
    tcpSocket->waitForBytesWritten();

    char pwd[20] = {};
    sprintf(pwd,"PWD\n");
    tcpSocket->write(pwd,strlen(pwd));
    tcpSocket->waitForBytesWritten();

    char pasv1[20] = {};
    sprintf(pasv1,"PASV\n");
    tcpSocket->write(pasv1,strlen(pasv1));
    tcpSocket->waitForBytesWritten();

    char list[20] = {};
    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[60] = {};
    strcpy(file_name,temp); // 复制给全局变量
    strcpy(filename,temp);
    qDebug("%s",filename);

    int fd = open(filename,O_RDONLY,0666);
    if(fd < 0)
    {
        qDebug("不是文件,上传失败");
        upload = false;
        return;
    }

    char typea[60] = {};
    sprintf(typea,"TYPE A\n");
    tcpSocket->write(typea,strlen(typea));
    tcpSocket->waitForBytesWritten();

    char size[60] = {};
    sprintf(size,"SIZE %s\n",filename);
    tcpSocket->write(size,strlen(size));
    tcpSocket->waitForBytesWritten();

    char pasv[20] = {};
    sprintf(pasv,"PASV\n");
    tcpSocket->write(pasv,strlen(pasv));
    tcpSocket->waitForBytesWritten();

    char stor[60] = {};
    sprintf(stor,"STOR %s\n",filename);
    tcpSocket->write(stor,strlen(stor));
    tcpSocket->waitForBytesWritten();

    while(upload == true)
    {
        usleep(1000);
        qDebug("while...");
    }

    char mdtm[60] = {};
    sprintf(mdtm,"MDTM %s\n",filename);
    tcpSocket->write(mdtm,strlen(mdtm));
    tcpSocket->waitForBytesWritten();

    char pwd[20] = {};
    sprintf(pwd,"PWD\n");
    tcpSocket->write(pwd,strlen(pwd));
    tcpSocket->waitForBytesWritten();

    char pasv1[20] = {};
    sprintf(pasv1,"PASV\n");
    tcpSocket->write(pasv1,strlen(pasv1));
    tcpSocket->waitForBytesWritten();

    char list[20] = {};
    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 == -1)
    {
        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[40] = {};
    sprintf(cwd,"CWD %s\n",filename);
    tcpSocket->write(cwd,strlen(cwd));
    tcpSocket->waitForBytesWritten();

    char pwd[20] = {};
    sprintf(pwd,"PWD\n");
    tcpSocket->write(pwd,strlen(pwd));
    tcpSocket->waitForBytesWritten();


    char pasv[20] = {};
    sprintf(pasv,"PASV\n");
    tcpSocket->write(pasv,strlen(pasv));
    tcpSocket->waitForBytesWritten();

    char list[20] = {};
    sprintf(list,"LIST -al\n");
    tcpSocket->write(list,strlen(list));
    tcpSocket->waitForBytesWritten();
}

 

你可能感兴趣的:(C,C/C++程序,Qt)