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
    #include

    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

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    #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 edits are non-empty, then
        closes this dialog. Pops up a message box if the line edits are
        empty.
    */
    void ConnectionDialog::checkInputAndConnect()
    {
        if (hl->text().isEmpty()) {
            QMessageBox::information(this,
                                     tr("Hostname is empty"),
                                     tr("Please enter the name of the host you wish to connect to."),
                                     QMessageBox::Ok);

        } else if (pl->text().isEmpty()) {
            QMessageBox::information(this,
                                     tr("Port is empty"),
                                     tr("Please enter the port you wish to connect to."),
                                     QMessageBox::Ok);

        } else {
            // If all is ok, close the dialog. The client will use the
            // host and port values and start connecting.
            close();
        }
    }

    /*!
        Returns the value of the "Hostname" line edit.
    */
    QString ConnectionDialog::host() const
    {
        return hl->text();
    }

    /*!
        Returns the value of the "Port" line edit, as an integer.
    */
    int ConnectionDialog::port() const
    {
        return pl->text().toInt();
    }

    /*!
        Clears both line edits and closes the dialog.
    */
    void ConnectionDialog::abortDialog()
    {
        hl->clear();
        pl->clear();
        close();
    }

    /*!
        Constructs an SSLClient.
    */
    SSLClient::SSLClient(QWidget *parent)
        : QWidget(parent)
    {
        setWindowTitle(tr("A simple SSL client"));

        // Create an SSL socket. Do not assign a socket, but rather have
        // it created automatically on demand.
        QtSslSocket *sslsocket = new QtSslSocket(QtSslSocket::Client, this);

        // Notice the platform dependency here; the location of the CA
        // certificate bundle is specific to the OS.
        sslsocket->setPathToCACertDir("/etc/ssl/certs");

        // Build the GUI.
        quitButton = new QPushButton(tr("&Quit"), this);
        userInput = new QLineEdit(this);
        statusLabel = new QLabel(this);
        terminal = new QTextEdit(this);

        terminal->setReadOnly(true);
        quitButton->setDefault(false);
        quitButton->setAutoDefault(false);
        userInput->setReadOnly(true);
        userInput->setFocus();

        QVBoxLayout *vl = new QVBoxLayout(this);
        vl->addWidget(terminal);
        vl->addWidget(userInput);
        vl->addWidget(statusLabel);
        vl->addWidget(quitButton);

        // Connect signals with slots.
        connect(userInput, SIGNAL(returnPressed()), SLOT(sendData()));
        connect(quitButton, SIGNAL(clicked()), SLOT(close()));

        // QtSslSocket's own signals are connected.
        connect(sslsocket, SIGNAL(connected()), SLOT(connectedToHost()));
        connect(sslsocket, SIGNAL(readyRead()), SLOT(readData()));
        connect(sslsocket, SIGNAL(disconnected()), SLOT(connectionClosed()));
        connect(sslsocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(error(QAbstractSocket::SocketError)));
        connect(sslsocket, SIGNAL(connectionVerificationDone(QtSslSocket::VerifyResult, bool, const QString &)),
                SLOT(checkCertError(QtSslSocket::VerifyResult, bool, const QString &)));

        QTimer::singleShot(0, this, SLOT(doConnect()));

        socket = sslsocket;
    }

    /*!
        Initiates connection to the host /a host on port /a port.
    */
    void SSLClient::connectToHost(const QString &host, int port)
    {
        this->host = host;
        statusLabel->setText(QString(tr("Connecting to %1:%2")).arg(host).arg(port));

        // Have the SSL socket initiate a connection to host:port. A
        // socket will be created implicitly. The SSL socket emits
        // connected() when the connection is established.
        socket->connectToHost(host, port);
    }

    /*! /reimp
    */
    QSize SSLClient::sizeHint() const
    {
        return QSize(400, 300);
    }

    /*!
        Pops up a dialog that lets the user type in a hostname and port.
        When the dialog is closed, a connection is initiated if the port
        and hostname fields are non-empty.
     */
    void SSLClient::doConnect()
    {
        ConnectionDialog dialog(this);
        dialog.exec();

        if (dialog.host().isEmpty() || dialog.port() == -1) {
            close();
        } else {
            connectToHost(dialog.host(), dialog.port());
        }
    }

    /*!
        This slot is called when a connection has been established. The
        line edit is enabled, and the status label is updated with a
        confirming message.
    */
    void SSLClient::connectedToHost()
    {
        statusLabel->setText(tr("Connected."));

        // Allow the user to communicate over the secure channel.
        userInput->setReadOnly(false);
    }

    /*!
        Sends the data from the line edit to the remote host.
    */
    void SSLClient::sendData()
    {
        // Get the text that the user wants to submit, and add a
        // linebreak. Then write the data to the SSL socket. The socket
        // will encrypt the data and write it to the internal socket.
        QString s = userInput->text().trimmed() + "/r/n";
        socket->write(s.toLatin1().constData(), s.length());

        // Append the user's text to the textedit display.
        QTextCursor cursor(terminal->textCursor());
        cursor.movePosition(QTextCursor::End);
        cursor.insertText(userInput->text());
        terminal->verticalScrollBar()->setValue(terminal->verticalScrollBar()->maximum());
        // Get ready for more user input.
        userInput->clear();

        // Update the status label.
        QString labelText(tr("Data sent."));
        statusLabel->setText(labelText);
    }

    /*!
        Reads data from the socket and appends it to the text edit.
    */
    void SSLClient::readData()
    {
        // Read all the data from the SSL socket. This is converted to a
        // QString, assuming that the server the user is communicating
        // with uses a text-based protocol.
        QString s(socket->readAll());
        s.replace('/r', "");

        // Append the server's text to the textedit display.
        terminal->append(s);
        terminal->verticalScrollBar()->setValue(terminal->verticalScrollBar()->maximum());
        // Update the status label.
        QString labelText(tr("Response received."));
        statusLabel->setText(labelText);
    }

    /*!
        This slot is called when the connection has been closed.
     */
    void SSLClient::connectionClosed()
    {
        // The connection has been closed. Update the status label and
        // disable user input. Set focus on the quit button, so pressing
        // enter exist the application.
        statusLabel->setText(tr("Connection closed."));
        userInput->setEnabled(false);
        terminal->setEnabled(false);
        quitButton->setFocus();
    }

    /*!
        Displays an error on the status label
    */
    void SSLClient::error(QAbstractSocket::SocketError err)
    {
        // Allow retries when the connection failed.
        if (err == QAbstractSocket::ConnectionRefusedError ||
                        err == QAbstractSocket::HostNotFoundError) {
            QTimer::singleShot(0, this, SLOT(doConnect()));
            return;
        }

        // Any errors reported by QtSslSocket have a human readable
        // representation, available through errorString():
        statusLabel->setText(tr("Error: ") + socket->errorString());
    }

    /*!
        This slot is called if there was an error checking a certificate
        during the SSL handshake phase. It pops up a warning box and asks
        the user to confirm whether or not to continue connecting.
    */
    void SSLClient::checkCertError(QtSslSocket::VerifyResult result,
                                   bool hostNameWrong, const QString &str)
    {
        if (result == QtSslSocket::VerifyOk && !hostNameWrong)
            return;

        QtSslSocket *sslsocket = socket;
        // Display some of the contents of the peer's SSL certificate.
        QStringList certStr = sslsocket->peerCertificate().split('/', QString::SkipEmptyParts);
        QMap cert;
        QStringListIterator it(certStr);
        while (it.hasNext()) {
            QStringList pair = it.next().split('=', QString::SkipEmptyParts);
            cert.insert(pair[0], pair.size() > 1 ? pair[1] : QString());
        }

        QString sInfo = tr("

"
                           ""
                           ""
                           ""
                           ""
                           ""
                           ""
                           ""
                           ""
                           ""
                           "
Organization%1
Unit%2
Common Name%3
Email%4
Country%5
State%6
Location%7
Not valid before%8
Not valid after%9
")
            .arg(cert["O"]).arg(cert["OU"]).arg(cert["CN"]).arg(cert["emailAddress"])
            .arg(cert["C"]).arg(cert["ST"]).arg(cert["L"]).arg(cert["notValidBefore"])
            .arg(cert["notValidAfter"]);

        QString warning = "

"
                          + tr("Could not confirm the identity of the host /"")
                          + host
                          + "/": " + str + "

" + sInfo
                          + tr("

Continue connecting to this server?

");

        // If the user does not wish to connect to the server, close the
        // socket.
        if (QMessageBox::warning(this, tr("Unknown server"), warning, "Abort", "Continue") == 0)
            socket->disconnectFromHost();
    }

    #include "sslclient.moc"
--------------------------------------------------------------------------------

main.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
    #include "sslclient.h"

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);

        SSLClient client;

        client.show();
        return app.exec();
    }
--------------------------------------------------------------------------------

client.pro:

    TEMPLATE = app
    INCLUDEPATH += .

    include(../../src/qtsslsocket.pri)

    HEADERS += sslclient.h
    SOURCES += main.cpp sslclient.cpp

 

 

--------------------------------------------------------------------------------------------------------------///

 

 

sslserver.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 SSLSERVER_H
    #define SSLSERVER_H
    #include
    class QtSslSocket;

    class SSLServerConnection : public QObject
    {
        Q_OBJECT

    public:
        SSLServerConnection(quint16 socket, QObject *parent = 0);
        ~SSLServerConnection();

    public slots:
        void acceptedClient();
        void readData();
        void connectionClosed();
        void error(QAbstractSocket::SocketError err);

    private:
        unsigned int readBytes;
        unsigned int writtenBytes;

        QtSslSocket *socket;
    };

    class SSLServer : public QTcpServer
    {
        Q_OBJECT

    public:
        SSLServer(quint16 port, QObject *parent = 0);

        void incomingConnection(int socket);
    };

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

sslserver.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 "sslserver.h"
    #include
    #include
    #include

    #include

    SSLServer::SSLServer(quint16 port, QObject *parent)
        : QTcpServer(parent)
    {
        listen(QHostAddress::Any, port);
    }

    void SSLServer::incomingConnection(int socket)
    {
        // As soon as a client connects, pass its incoming socket id to a
        // SSLServerConnection child. This child is deleted after the
        // connection is closed (see the connectionClosed() slot).
        new SSLServerConnection(socket, this);
    }

    SSLServerConnection::SSLServerConnection(quint16 socketDescriptor,
                                             QObject *parent)
        : QObject(parent)
    {
        // Create an SSL socket and make its QTcpSocket use our accepted
        // socket, then give it the path to our certificate & private key
        // file. For notes on this file, please check the provided
        // "server.txt".
        socket = new QtSslSocket(QtSslSocket::Server, this);
        socket->socket()->setSocketDescriptor(socketDescriptor);
        socket->setPathToCertificate("sslserver.pem");
        socket->setPathToPrivateKey("sslserver.pem");

        // Notice the platform dependency here; the location of the CA
        // certificate bundle is specific to the OS.
        socket->setPathToCACertDir("/etc/ssl/certs");

        // Connect the SSL socket's signals to our slots.
        connect(socket, SIGNAL(connected()), SLOT(acceptedClient()));
        connect(socket, SIGNAL(disconnected()), SLOT(connectionClosed()));
        connect(socket, SIGNAL(readyRead()), SLOT(readData()));
        connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(error(QAbstractSocket::SocketError)));

        // Call sslAccepted(). After this, when the SSL socket emits
        // accepted(), we are ready to go. We ignore the return value of
        // this function, because it will always fail the first time we
        // call it.
        socket->sslAccept();
    }

    SSLServerConnection::~SSLServerConnection()
    {
        // Report that the connection has closed.
        qDebug("Connection closed.");
    }

    void SSLServerConnection::acceptedClient()
    {
        // Provide feedback to the user about incoming connections. This
        // slot is only called if the connection was established, so all
        // communication is now encrypted.
        qDebug("Accepted new client from %s:%d",
               qPrintable(socket->peerAddress().toString()),
               socket->peerPort());

        // Print a simple DOS-like prompt. Write this to the SSL socket.
        // The SSL socket encrypts the data, and sends it to the client.
        QString s = "Welcome to Fake-DOS 2.11/r/nC://>";
        socket->write(s.toLatin1().constData(), s.length());
    }

    void SSLServerConnection::readData()
    {
        // First, read all incoming data from the client. The SSL socket
        // has already decrypted it for us. We assume that the client uses
        // a plain text protocol, so we convert the data to a QString.
        QString incoming(socket->readAll());

        // This server accepts only the commands "EXIT" and "DIR",
        // although case insensitive. All other commands are rejected with
        // "bad command or file name". Write response back to the client
        // through the SSL socket.
        QString command = incoming.toUpper().trimmed();
        if (command == "EXIT") {
            QString s = "system halted/r/n";
            socket->write(s.toLatin1().constData(), s.length());
            socket->close();
        } else if (command == "DIR") {
            QDir cwd(".");
            const QFileInfoList cwdlist = cwd.entryInfoList();
            if (cwdlist.isEmpty()) {
                QString s = "unable to list directory contents/r/nC://>";
                socket->write(s.toLatin1().constData(), s.length());
            } else {
                QString s = " Volume in drive C has no label./r/n";
                s += " Volume Serial Number is C564-1226/r/n/r/n";
                s += " Directory of C:///r/n/r/n";
                QListIterator it(cwdlist);

                int nfiles = 0;
                int ndirs = 0;
                int tildes = 0;
                while (it.hasNext()) {
                    QFileInfo f = it.next();
                    QDate d = f.created().date();
                    QTime t = f.created().time();
                    QString line;

                    bool dots = f.fileName() == "." || f.fileName() == "..";
                    QString fname = dots ? QString("") : f.baseName().toUpper();
                    QString lname = dots ? f.fileName() : f.completeSuffix().toUpper().left(3);

                    if (fname.length() > 8) {
                        QString tmp;
                        tmp.sprintf("~%i", ++tildes);
                        fname = fname.left(8 - tmp.length()) + tmp;
                    }

                    if (f.isDir()) {
                        line.sprintf("%8s %3s

         %02i-%02i-%02i  %2i:%02i%c/r/n",
                                     qPrintable(fname), qPrintable(lname),
                                     d.day(), d.month(),
                                     (d.year() - 1900) % 100, t.hour() % 12, t.minute(),
                                     t.hour() > 12 ? 'p' : 'a');
                    } else {
                        line.sprintf("%8s %3s       %7d %02i-%02i-%02i  %2i:%02i%c/r/n",
                                     qPrintable(fname), qPrintable(lname),
                                     (int)f.size(), d.day(), d.month(),
                                     (d.year() - 1900) % 100, t.hour() % 12, t.minute(),
                                     t.hour() > 12 ? 'p' : 'a');
                    }

                    s += line;
                    if (f.isDir())
                        ++ndirs;
                    else
                        ++nfiles;
                }

                QString line;
                line.sprintf("%16i File(s)/r/n", nfiles);
                s += line;
                line.sprintf("%16i Dir(s)/r/n", ndirs);
                s += line;
                s += "C://>";
                socket->write(s.toLatin1().constData(), s.length());
            }

        } else {
            QString s = "bad command or file name/r/nC://>";
            socket->write(s.toLatin1().constData(), s.length());
        }
    }

    void SSLServerConnection::connectionClosed()
    {
        // Although the socket may be closing, we must not delete it until
        // the delayed close is done.
        if (socket->socket()->state() == QAbstractSocket::ClosingState) {
            connect(socket->socket(), SIGNAL(disconnected()), SLOT(deleteLater()));
        } else {
            deleteLater();
            return;
        }

        qDebug("Connection closed.");
    }

    void SSLServerConnection::error(QAbstractSocket::SocketError)
    {
        // The SSL socket conveniently provides human readable error
        // messages through the errorString() call. Note that sometimes
        // the errors come directly from the underlying SSL library, and
        // the quality of the text may vary.
        qDebug("Error: %s", qPrintable(socket->errorString()));
    }
--------------------------------------------------------------------------------

main.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
    #include
    #include

    #include "sslserver.h"

    /*
        An SSL server that is started from the console.

        This example demonstrates the use of the QtSSLSocket class in a
        server application.
     */
    int main(int argc, char *argv[])
    {
        QFileInfo cert("sslserver.pem");
        if (!cert.exists()) {
            qDebug("Note: This server requires the file sslserver.pem to exist, "
                   "and to contain the SSL private key and certificate for "
                   "this server, encoded in PEM format. Please read "
                   "server.txt for more information.");
            return 1;
        }

        if (argc < 2) {
            qDebug("usage: %s ", argv[0]);
            qDebug("A simple SSL server.");
            return 1;
        }

        QApplication app(argc, argv, false);

        int port = atoi(argv[1]);

        SSLServer sserver(port);

        qDebug("Listening on port %i. Please press Ctrl-C to exit.", port);

        return app.exec();
    }
--------------------------------------------------------------------------------

server.pro:

    TEMPLATE = app
    INCLUDEPATH += .
    CONFIG += console

    include(../../src/qtsslsocket.pri)

    HEADERS += sslserver.h
    SOURCES += main.cpp sslserver.cpp

你可能感兴趣的:(QT中SOCKET加密SSL)