如何通过QSslSocket单独发送两个字节?

时间:2012-12-09 23:07:36

标签: c++ qt ssl qtnetwork

我有一个客户端程序和一个使用QSslSockets进行通信的服务器程序。在服务器程序中,如果客户端成功连接,我发送一个字符串"Snameadded",我也想在服务器上发送一个用户列表,但作为一个不同的消息,我制作两个不同的块并写下它们分开但客户正在将它们视为一个

这是服务器上的代码:

void server::processMess(QString message)
{

    //Message type legend
    // [number]*** [name]*** [message]
    // number = 1, joining server for first time, rest is name
    // number = 2, split by "***", retrive name, receiving name, and rest is message
    // number = 3, disconnecting from server, resy is name


    bool successfullyConnected = false;
    QStringList temp = message.split("***");
    int num = temp.at(0).toInt();

    qDebug() << num;
    qDebug() << "size of socket list:" << myClientSockets.size();
    QString name = temp.at(1);
    if(num == 1)
    {
        QString name = temp.at(1);
        if(clientList.contains(name))
        {
            QByteArray block;
            block.append("Enameinvalid");

            QSslSocket *clientConnection = myClientSockets.at(myClientSockets.size() - 1);
            myClientSockets.pop_back();
            clientConnection->write(block);
            \

        }
        else
        {
            clientList.append(name);
            QByteArray block;
            block.append("Snameadded");
            successfullyConnected = true;


            QSslSocket *clientConnection = myClientSockets.at(myClientSockets.size() - 1);
            qDebug() << "Message created to send back to client: " << QString(block);
            clientConnection->write(block);

           /*
            ClientThread *thisClientThread = new ClientThread();
            thisClientThread->setClientConnection(clientConnection);
            myClientThreads.push_back(thisClientThread);
            connect(thisClientThread, SIGNAL(messageReceived(QString)), this, SLOT(processMess(QString)));
            thisClientThread->start();*/

        }
    }
    else if(num == 2)
    {
        //Direct message correctly
        QString receiver = temp.at(2);
        QSslSocket* receiverConnection = myClientSockets.at(clientList.indexOf(receiver));
        QByteArray block;
        block.append(message);
        receiverConnection->write(block);

    }
    else if(num == 3)
    {
        //Disconnect client
        updateServer(name + "disconnected");
        int position = clientList.indexOf(name);
        clientList.removeAt(position);
        myClientSockets.removeAt(position);
    }
    else
    {
        qDebug() << "Should never reach here";
    }

    if(successfullyConnected)
    {

        QByteArray block;
        block.append("UsersOnServer:");
        for(int i = 0; i < clientList.size(); ++i)
        {
            block.append(clientList.at(i));
            if(i < clientList.size() - 1)
            {
                block.append("***");
            }
        }
        for(int i = 0; i < myClientSockets.size(); ++i)
        {
            QSslSocket *receiverConnection = myClientSockets.at(i);
            receiverConnection->write(block);
        }
    }

    qDebug() << "Messaged received from client: " + message;
    updateServer("Messaged received from client: " + message);


    /*QSslSocket* thisConnection = myClientSockets.pop_back();

    QByteArray incoming = thisConnection->readAll();

    QString message = QString(incoming);

    qDebug() << "Message received from client: " + message;

    return;*/

    /*QByteArray in = tempSocket->readAll();

    QString message = QString(in);

    qDebug() << "Messaged received from client: " + message;
    updateServer(message);
*/


}

继承我客户端的代码: secureSocket-→写(块);

if(secureSocket->waitForReadyRead(1000))
{
    QByteArray in = secureSocket->readAll();

    QString serverMess = QString(in);
    if(QString::compare(serverMess, "Snameadded", Qt::CaseSensitive) == 0)
    {
        returnVal = true;
        qDebug() << "added to server";
        connect(secureSocket, SIGNAL(readyRead()), this, SLOT(receiveMess()));
    }
    else if(QString::compare(serverMess, "Enameinvalid", Qt::CaseSensitive) == 0)
    {
        returnVal = false;
        qDebug() << "username already taken";
    }
    qDebug() << "*************Message from server:************** 1" << serverMess;
}

1 个答案:

答案 0 :(得分:0)

我修复了它我只是做了QSslSocket :: waitForBytesWritten()所以它会发送第一个字节数组,然后分别发送第二个数组。