QT中SOCKET加密SSL

sslclient.h:

    /****************************************************************************
    **
    ** Copyright (C) 2003-2006 Trolltech ASA. All rights reserved.
    **
    ** This file is part of a Qt Solutions component.
    **
    ** Licensees holding valid Qt Solutions licenses may use this file in
    ** accordance with the Qt Solutions License Agreement provided with the
    ** Software.
    **
    ** See http://www.trolltech.com/products/qt/addon/solutions/
    ** or email [email protected] for information about Qt Solutions
    ** License Agreements.
    **
    ** Contact [email protected] if any conditions of this licensing are
    ** not clear to you.
    **
    ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    **
    ****************************************************************************/
    #ifndef SSLCLIENT_H
    #define SSLCLIENT_H
    #include <qwidget.h>
    #include <qtsslsocket.h>

    class QPushButton;
    class QTextEdit;
    class QLabel;
    class QLineEdit;
    class QtSslSocket;
    class QSocket;

    class SSLClient : public QWidget
    {
        Q_OBJECT

    public:
        SSLClient(QWidget *parent = 0);

        QSize sizeHint() const;

    private slots:
        void doConnect();
        void readData();
        void sendData();
        void connectionClosed();
        void error(QAbstractSocket::SocketError err);
        void connectToHost(const QString &host, int port);
        void connectedToHost();
        void checkCertError(QtSslSocket::VerifyResult, bool, const QString &str);

    private:
        QString host;
        int port;
        QPushButton *quitButton;
        QTextEdit *terminal;
        QLabel *statusLabel;
        QLineEdit *userInput;
        //QtSslSocket *sslsocket;
        QtSslSocket *socket;
    };

    #endif
--------------------------------------------------------------------------------

sslclient.cpp:

    /****************************************************************************
    **
    ** Copyright (C) 2003-2006 Trolltech ASA. All rights reserved.
    **
    ** This file is part of a Qt Solutions component.
    **
    ** Licensees holding valid Qt Solutions licenses may use this file in
    ** accordance with the Qt Solutions License Agreement provided with the
    ** Software.
    **
    ** See http://www.trolltech.com/products/qt/addon/solutions/
    ** or email [email protected] for information about Qt Solutions
    ** License Agreements.
    **
    ** Contact [email protected] if any conditions of this licensing are
    ** not clear to you.
    **
    ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    **
    ****************************************************************************/
    #include <qtsslsocket.h>

    #include <qdialog.h>
    #include <qlabel.h>
    #include <qlayout.h>
    #include <qlineedit.h>
    #include <qmap.h>
    #include <qmessagebox.h>
    #include <qpushbutton.h>
    #include <qregexp.h>
    #include <qtabwidget.h>
    #include <qtextedit.h>
    #include <qtimer.h>
    #include <qtextcursor.h>
    #include <qscrollbar.h>

    #include "sslclient.h"

    /*
        A simple SSL client example.

        This example demostrates the use of QtSslSocket in a client
        application. The application connects to an SSL service on any
        given host and port. If the connection succeeds, the user is given
        a line edit for entering commands, and a text edit which displays
        the response from the server.

        Try this example by connection to your IMAP4 or POP3 server.
        Chances are that these services are running SSL. The SSL service
        IMAP4 runs on port 993, and POP3 is on port 995.

        When communicating with an IMAP4 server, the command "X LOGOUT"
        will close the connection. With POP3, use the command "QUIT".
    */

    /*
        This class provides a QDialog popup that is shown by SSLClient. It
        has line edits for hostname and port number.
    */
    class ConnectionDialog : public QDialog
    {
        Q_OBJECT
    public:
        ConnectionDialog(QWidget *parent = 0);

        QString host() const;
        int port() const;

    private slots:
        void abortDialog();
        void checkInputAndConnect();

    private:
        QLineEdit *hl;
        QLineEdit *pl;
        QPushButton *conn;
        QPushButton *abrt;
    };

    /*!
        Constructs a ConnectionDialog, which is the initial dialog
        that pops up when the user starts this program.

        Its /a parent and /a name arguments are passed to QDialog's
        constructor.
    */
    ConnectionDialog::ConnectionDialog(QWidget *parent)
        : QDialog(parent)
    {
        setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);

        setWindowTitle(tr("Connect to host"));
        QGridLayout *layout = new QGridLayout(this);

        // Create two labels
        layout->addWidget(new QLabel(tr("Hostname"), this), 0, 1);
        layout->addWidget(new QLabel(tr("Port"), this), 1, 1);

        // Create the hostname and port line edits. Find an appropriate
        // input mask for the port input.
        hl = new QLineEdit(this);
        pl = new QLineEdit(this);
        pl->setInputMask("99999");

        layout->addWidget(hl, 0, 2);
        layout->addWidget(pl, 1, 2);

        // Add two buttons: "Abort" and "Connect"
        abrt = new QPushButton(tr("&Abort"), this);
        conn = new QPushButton(tr("&Connect"), this);
        conn->setDefault(true);

        layout->addWidget(abrt, 2, 1);
        layout->addWidget(conn, 2, 2);

        connect(abrt, SIGNAL(clicked()), SLOT(abortDialog()));
        connect(conn, SIGNAL(clicked()), SLOT(checkInputAndConnect()));
    }

    /*!
        Checks that the hostname and port line edi

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值