Question

I have a client program and a server program that communicate using QSslSockets. In the server program I'm sending a string "Snameadded" if the client successfully connected,I also want to send it a list of the users on the server, but as a different message, I make two different blocks and write them separately but they are being read by the client as one

This is the code on the server:

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


}

heres the code on my client side: secureSocket->write(block);

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;
}
Was it helpful?

Solution

I fixed it I just did QSslSocket::waitForBytesWritten() so it would send the first byte array then the second one seperately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top