当前位置: 代码网 > it编程>编程语言>C/C++ > 利用Qt实现FTP服务器并支持多客户端登录

利用Qt实现FTP服务器并支持多客户端登录

2024年12月29日 C/C++ 我要评论
一、效果展示二、源码实现由于源码较多,只分享其中一部分ftpserverwidget.h#ifndef ftpserverwidget_h#define ftpserverwidget_h#inclu

一、效果展示

二、源码实现

由于源码较多,只分享其中一部分

ftpserverwidget.h

#ifndef ftpserverwidget_h
#define ftpserverwidget_h

#include <qwidget>
#include <qgridlayout>
#include <qhboxlayout>
#include <qlabel>
#include <qlineedit>
#include <qpushbutton>
#include <qtoolbutton>
#include <qfiledialog>
#include <qregularexpression>
#include <qregularexpressionvalidator>
#include <qmessagebox>
#include <qfileinfo>
#include <qhostinfo>

#include "ftpserver/ftpserver.h"



class ftpserverwidget : public qwidget
{
    q_object
    q_property(qstring ip read get_ip write set_ip)
    q_property(qstring port read get_port write set_port)
    q_property(qstring username read get_user_name write set_user_name)
    q_property(qstring password read get_password write set_password)
    q_property(qstring path read get_path write set_path)
signals:
    void para_changed();
public:
    explicit ftpserverwidget(qwidget *parent = nullptr);
    ~ftpserverwidget();

    bool set_ftp_para(qstring ip,qstring port,qstring username,qstring password,qstring path);

    bool ftp_start_server();
    bool ftp_stop_server();

    qstring get_ip();
    void set_ip(qstring val);
    qstring get_port();
    void set_port(qstring val);
    qstring get_user_name();
    void set_user_name(qstring val);
    qstring get_password();
    void set_password(qstring val);
    qstring get_path();
    void set_path(qstring val);
protected:
    void showevent(qshowevent *event) override;
    void closeevent(qcloseevent *event) override;
private:
    void value_init();
    void control_init();
private slots:
  void btn_click_slot();
  void dir_select_slot(const qstring &directory);

private:
    qgridlayout *gridlayout;
    qhboxlayout *horizontallayout;
    qlabel *label;
    qlineedit *lineeditip;
    qhboxlayout *horizontallayout_2;
    qlabel *label_2;
    qlineedit *lineeditport;
    qhboxlayout *horizontallayout_3;
    qlabel *label_3;
    qlineedit *lineedituser;
    qhboxlayout *horizontallayout_4;
    qlabel *label_4;
    qlineedit *lineeditpassward;
    qhboxlayout *horizontallayout_6;
    qlabel *label_6;
    qlineedit *lineeditpath;
    qpushbutton *btnselectpath;
    qhboxlayout *horizontallayout_7;
    qpushbutton *btncancel;
    qpushbutton *btnconfirm;

    qfiledialog *dirselectdlg;
    qmessagebox *paramessagebox;


    qstring ftpip = nullptr;
    qstring ftpport = nullptr;
    qstring ftpusername = nullptr;
    qstring ftppassword = nullptr;
    qstring ftppath = nullptr;

    ftpserver *ftpserver = nullptr;

};

#endif // ftpserverwidget_h

ftpserverwidget.cpp

#include "ftpserverwidget.h"


ftpserverwidget::ftpserverwidget(qwidget *parent)
    : qwidget{parent}
{
    this->value_init();
    this->control_init();
}

ftpserverwidget::~ftpserverwidget()
{
    this->ftp_stop_server();
}


void ftpserverwidget::value_init()
{
    this->ftpip = "127.0.0.1";
}

void ftpserverwidget::control_init()
{
    this->resize(450, 330);
    this->setminimumsize(qsize(450, 330));
    this->setmaximumsize(qsize(500, 380));
    this->setwindowtitle(tr("ftp服务器参数设置"));

    gridlayout = new qgridlayout();
    gridlayout->setobjectname(qstring::fromutf8("gridlayout"));

    horizontallayout = new qhboxlayout();
    horizontallayout->setobjectname(qstring::fromutf8("horizontallayout"));

    label = new qlabel();
    label->settext(tr("服务器ip:"));
    label->setobjectname(qstring::fromutf8("label"));
    label->setalignment(qt::aligncenter);

    horizontallayout->addwidget(label);

    lineeditip = new qlineedit();
    lineeditip->setobjectname(qstring::fromutf8("lineeditip"));

    horizontallayout->addwidget(lineeditip);

    horizontallayout->setstretch(0, 1);
    horizontallayout->setstretch(1, 4);

    gridlayout->addlayout(horizontallayout, 0, 0, 1, 1);

    horizontallayout_2 = new qhboxlayout();
    horizontallayout_2->setobjectname(qstring::fromutf8("horizontallayout_2"));

    label_2 = new qlabel();
    label_2->settext(tr("服务器端口:"));
    label_2->setobjectname(qstring::fromutf8("label_2"));
    label_2->setalignment(qt::aligncenter);

    horizontallayout_2->addwidget(label_2);

    lineeditport = new qlineedit();
    lineeditport->setobjectname(qstring::fromutf8("lineeditport"));

    horizontallayout_2->addwidget(lineeditport);

    horizontallayout_2->setstretch(0, 1);
    horizontallayout_2->setstretch(1, 4);

    gridlayout->addlayout(horizontallayout_2, 1, 0, 1, 1);

    horizontallayout_3 = new qhboxlayout();
    horizontallayout_3->setobjectname(qstring::fromutf8("horizontallayout_3"));

    label_3 = new qlabel();
    label_3->settext(tr("用户名:"));
    label_3->setobjectname(qstring::fromutf8("label_3"));
    label_3->setalignment(qt::aligncenter);

    horizontallayout_3->addwidget(label_3);

    lineedituser = new qlineedit();
    lineedituser->setobjectname(qstring::fromutf8("lineedituser"));

    horizontallayout_3->addwidget(lineedituser);

    horizontallayout_3->setstretch(0, 1);
    horizontallayout_3->setstretch(1, 4);

    gridlayout->addlayout(horizontallayout_3, 2, 0, 1, 1);

    horizontallayout_4 = new qhboxlayout();
    horizontallayout_4->setobjectname(qstring::fromutf8("horizontallayout_4"));

    label_4 = new qlabel();
    label_4->settext(tr("登录密码:"));
    label_4->setobjectname(qstring::fromutf8("label_4"));
    label_4->setalignment(qt::aligncenter);

    horizontallayout_4->addwidget(label_4);

    lineeditpassward = new qlineedit();
    lineeditpassward->setobjectname(qstring::fromutf8("lineeditpassward"));

    horizontallayout_4->addwidget(lineeditpassward);

    horizontallayout_4->setstretch(0, 1);
    horizontallayout_4->setstretch(1, 4);

    gridlayout->addlayout(horizontallayout_4, 3, 0, 1, 1);

    horizontallayout_6 = new qhboxlayout();
    horizontallayout_6->setobjectname(qstring::fromutf8("horizontallayout_6"));

    label_6 = new qlabel();
    label_6->settext(tr("文件路径:"));
    label_6->setobjectname(qstring::fromutf8("label_6"));
    label_6->setalignment(qt::aligncenter);

    horizontallayout_6->addwidget(label_6);

    lineeditpath = new qlineedit();
    lineeditpath->setobjectname(qstring::fromutf8("lineeditpath"));
    qsizepolicy sizepolicy(qsizepolicy::expanding, qsizepolicy::fixed);
    sizepolicy.sethorizontalstretch(0);
    sizepolicy.setverticalstretch(0);
    sizepolicy.setheightforwidth(lineeditpath->sizepolicy().hasheightforwidth());
    lineeditpath->setsizepolicy(sizepolicy);

    horizontallayout_6->addwidget(lineeditpath);

    btnselectpath = new qpushbutton();
    btnselectpath->settext(tr("..."));
    btnselectpath->setobjectname(qstring::fromutf8("toolbtnselectpath"));
    btnselectpath->setminimumsize(qsize(82, 0));

    horizontallayout_6->addwidget(btnselectpath);

    horizontallayout_6->setstretch(0, 1);
    horizontallayout_6->setstretch(1, 3);
    horizontallayout_6->setstretch(2, 1);

    gridlayout->addlayout(horizontallayout_6, 4, 0, 1, 1);

    horizontallayout_7 = new qhboxlayout();
    horizontallayout_7->setobjectname(qstring::fromutf8("horizontallayout_7"));

    btncancel = new qpushbutton();
    btncancel->settext(tr("取消"));
    btncancel->setobjectname(qstring::fromutf8("btncancel"));
    qsizepolicy sizepolicy1(qsizepolicy::minimum, qsizepolicy::fixed);
    sizepolicy1.sethorizontalstretch(0);
    sizepolicy1.setverticalstretch(0);
    sizepolicy1.setheightforwidth(btncancel->sizepolicy().hasheightforwidth());
    btncancel->setsizepolicy(sizepolicy1);
    btncancel->setminimumsize(qsize(0, 50));

    horizontallayout_7->addwidget(btncancel);

    btnconfirm = new qpushbutton();
    btnconfirm->settext(tr("确定"));
    btnconfirm->setobjectname(qstring::fromutf8("btnconfirm"));
    sizepolicy1.setheightforwidth(btnconfirm->sizepolicy().hasheightforwidth());
    btnconfirm->setsizepolicy(sizepolicy1);
    btnconfirm->setminimumsize(qsize(0, 50));

    horizontallayout_7->addwidget(btnconfirm);


    gridlayout->addlayout(horizontallayout_7, 5, 0, 1, 1);

    this->setlayout(gridlayout);
    //限制lineedit格式输入
    qregularexpression regipstr("(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])");
    qvalidator * validator = new qregularexpressionvalidator(regipstr, this->lineeditip);
    this->lineeditip->setvalidator(validator);

    this->lineeditport->setvalidator(new qintvalidator(0,65535,this->lineeditport));


    qregularexpression reguserstr("[a-za-z0-9]+$");
    validator = new qregularexpressionvalidator(reguserstr, this->lineedituser);
    this->lineedituser->setvalidator(validator);
    this->lineeditpassward->setvalidator(validator);
    this->lineedituser->setmaxlength(8);
    this->lineeditpassward->setmaxlength(8);

    this->lineeditip->setplaceholdertext(tr("请输入服务器ip地址!"));
    this->lineeditport->setplaceholdertext(tr("请输入服务器端口!"));
    this->lineedituser->setplaceholdertext(tr("请输入登录用户名!"));
    this->lineeditpassward->setplaceholdertext(tr("请输入登录密码!"));
    this->lineeditpath->setplaceholdertext(tr("请输入文件路径!"));

    this->lineeditip->setstylesheet("border:2px groove gray;border-radius:8px;padding:2px 4px;border-style: outset;");
    this->lineeditport->setstylesheet("border:2px groove gray;border-radius:8px;padding:2px 4px;border-style: outset;");
    this->lineedituser->setstylesheet("border:2px groove gray;border-radius:8px;padding:2px 4px;border-style: outset;");
    this->lineeditpassward->setstylesheet("border:2px groove gray;border-radius:8px;padding:2px 4px;border-style: outset;");
    this->lineeditpath->setstylesheet("border:2px groove gray;border-radius:8px;padding:2px 4px;border-style: outset;");

    this->lineeditip->setreadonly(true);


    //路径选择dialog初始化
    this->dirselectdlg = new qfiledialog(this);
    this->dirselectdlg->setfilemode(qfiledialog::directory);
    this->dirselectdlg->setwindowtitle(tr("请选择一个文件夹!"));
    this->dirselectdlg->setmodal(false);
    this->dirselectdlg->close();
    connect(this->dirselectdlg,&qfiledialog::fileselected,this,&ftpserverwidget::dir_select_slot);
    //错误提示messagebox初始化
    this->paramessagebox = new qmessagebox(this);
    this->paramessagebox->setmodal(false);
    this->paramessagebox->setwindowtitle(tr("参数错误!"));
    this->paramessagebox->seticon(qmessagebox::warning);
    this->paramessagebox->setminimumsize(500,100);
    this->paramessagebox->close();


    connect(this->btnconfirm,&qpushbutton::clicked,this,&ftpserverwidget::btn_click_slot);
    connect(this->btncancel,&qpushbutton::clicked,this,&ftpserverwidget::btn_click_slot);
    connect(this->btnselectpath,&qpushbutton::clicked,this,&ftpserverwidget::btn_click_slot);

}
bool ftpserverwidget::set_ftp_para(qstring ip, qstring port, qstring username, qstring password, qstring path)
{

    qdebug()<<"ip:"<<ip<<"  port:"<<port<<"  username:"<<username<<"  password:"<<password<<"  path:"<<path;
    if(ip == nullptr)
    {
        this->paramessagebox->settext(tr("ip不能为空!"));
        this->paramessagebox->show();
        return false;
    }
    if(port == nullptr)
    {
        this->paramessagebox->settext(tr("端口设置不能为空!"));
        this->paramessagebox->show();
        return false;
    }
    if(port.touint() >65535)
    {
        this->paramessagebox->settext(tr("端口值设置错误!"));
        this->paramessagebox->show();
        return false;
    }
    if(username == nullptr)
    {
        this->paramessagebox->settext(tr("用户名不能为空!"));
        this->paramessagebox->show();
        return false;
    }
    if(password == nullptr)
    {
        this->paramessagebox->settext(tr("用户密码不能为空!"));
        this->paramessagebox->show();
        return false;
    }
    if(path == nullptr)
    {
        this->paramessagebox->settext(tr("路径不能为空!"));
        this->paramessagebox->show();
        return false;
    }
    qfileinfo info(path);
    if(!info.exists())
    {
        this->paramessagebox->settext(tr("路径不存在!"));
        this->paramessagebox->show();
        return false;
    }


    this->ftpip = ip;
    this->ftpport = port;
    this->ftpusername = username;
    this->ftppassword = password;
    this->ftppath = path;
    return true;
}

bool ftpserverwidget::ftp_start_server()
{
    if(this->ftpip == nullptr)
    {
        return false;
    }
    if(this->ftpport == nullptr)
    {
        return false;
    }
    if(this->ftpport.touint() >65535)
    {
        return false;
    }
    if(this->ftpusername == nullptr)
    {
        return false;
    }
    if(this->ftppassword == nullptr)
    {
        return false;
    }
    if(this->ftppath == nullptr)
    {
        return false;
    }
    qfileinfo info(this->ftppath);
    if(!info.exists())
    {
        return false;
    }

    if (this->ftpserver !=nullptr)
    {
        qdebug() << "服务器已经启动";
        return true;
    }
    this->ftpserver = new ftpserver(this, this->ftppath, this->ftpport.toint(), this->ftpusername, this->ftppassword);
    //connect(m_server, signal(newpeerip(qstring)), this, slot(onnewpeerip(qstring)));

    qdebug() << "服务器已启动";
    return true;
}
bool ftpserverwidget::ftp_stop_server()
{
    if(this->ftpserver != nullptr)
    {
        delete this->ftpserver;
        this->ftpserver = null;
    }
    qdebug() << "服务器已关闭";
    return true;
}

qstring ftpserverwidget::get_ip()
{
    return this->ftpip;
}

void ftpserverwidget::set_ip(qstring val)
{
    this->ftpip = val;
    emit para_changed();
}

qstring ftpserverwidget::get_port()
{
     return this->ftpport;
}

void ftpserverwidget::set_port(qstring val)
{
    this->ftpport = val;
    emit para_changed();

}

qstring ftpserverwidget::get_user_name()
{
    return this->ftpusername;
}

void ftpserverwidget::set_user_name(qstring val)
{
    this->ftpusername = val;
    emit para_changed();
}

qstring ftpserverwidget::get_password()
{
    return this->ftppassword;
}

void ftpserverwidget::set_password(qstring val)
{
    this->ftppassword = val;
    emit para_changed();
}

qstring ftpserverwidget::get_path()
{
    return this->ftppath;
}

void ftpserverwidget::set_path(qstring val)
{
    this->ftppath = val;
    emit para_changed();
}



void ftpserverwidget::btn_click_slot()
{
    qpushbutton *btn = qobject_cast<qpushbutton *>(sender());
    if(btn == this->btnconfirm) //确定
    {
        qstring ipstr = this->lineeditip->text();
        qstring portstr = this->lineeditport->text();
        qstring userstr = this->lineedituser->text();
        qstring passwordstr = this->lineeditpassward->text();
        qstring pathstr = this->lineeditpath->text();


        this->set_ftp_para(ipstr,portstr,userstr,passwordstr,pathstr);
        this->ftp_start_server();
    }
    else if(btn == this->btncancel) //取消
    {
        this->close();
    }
    else if(btn == this->btnselectpath) //选择路径
    {
        this->dirselectdlg->show();
    }
}

void ftpserverwidget::dir_select_slot(const qstring &directory)
{
    this->lineeditpath->settext(directory);
}
void ftpserverwidget::showevent(qshowevent *event)
{
    q_unused(event);
    if(this->ftpip != nullptr)
    {
        this->lineeditip->settext(this->ftpip);
    }
    if(this->ftpport != nullptr)
    {
        this->lineeditport->settext(this->ftpport);
    }
    if(this->ftpusername !=nullptr)
    {
        this->lineedituser->settext(this->ftpusername);
    }
    if(this->ftppassword != nullptr)
    {
        this->lineeditpassward->settext(this->ftppassword);
    }
    if(this->ftppath != nullptr)
    {
        this->lineeditpath->settext(this->ftppath);
    }

}
void ftpserverwidget::closeevent(qcloseevent *event)
{
    q_unused(event);
    this->dirselectdlg->close();
    this->paramessagebox->close();
}

到此这篇关于利用qt实现ftp服务器并支持多客户端登录的文章就介绍到这了,更多相关qt ftp服务器内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com