在TCP连接,QT期间出现奇怪的引号

时间:2018-01-20 17:17:00

标签: c++ string qt tcp quotes

我在客户端/服务器应用中通过TCP发送字符串。在客户端,获取字符串看起来像:

void MainWindow::on_btn_login_clicked()
{
    QByteArray login = (ui->login_input->text()).toUtf8();
    QByteArray password = (ui->password_input->text()).toUtf8();
    connection->ConnectAndSendData(login, password);
}

void TcpConnect::ConnectAndSendData(const QByteArray login, const QByteArray password)
{
    socket = new QTcpSocket(this);
    int port = 1234;
    socket->connectToHost("localhost", port);

    if(socket->waitForConnected(3000))
    {
        qDebug() << "connected to s. localhost at port " << port;
        //socket->flush();
        socket->write(MESSAGE_WANTLOGIN);
        qDebug() << login<< "\n";

        socket->waitForReadyRead(1000);
        QByteArray response = socket->readAll();
        qDebug() << "got response " << response;
        if(response == MESSAGE_LOGINRQ)
        {
            socket->write(login);
            qDebug() << "i wrote " << login;
        }
        else
            qDebug() << "not LOGINRQ";

        socket->waitForReadyRead(1000);
        response = socket->readAll();
        qDebug() << "got response " << response;
        if(response == MESSAGE_PASSRQ)
        {
            socket->write(password);
            qDebug() << "i wrote " << password;
        }
        else
            qDebug() << "not PASSRQ" << response;

        socket->waitForReadyRead(1000);
        response = socket->readAll();
        qDebug() << "got username: " << response;
        QByteArray username = response;
        socket->flush();
        socket->close();
    }
    else
    {
        emit changeInfoLabel(QString("Serwer nie odpowiada. Sprawdz swoje polaczenie sieciowe."));
        qDebug() << "unable to connect to server";
    }
}

接收消息的服务器如下:

void Thread::readyRead()
{
    socket->flush();
    QByteArray request, login, password;
    request = socket->readAll();

    qDebug() << "data received: " << request;
    if(request == MESSAGE_WANTLOGIN)
    {
        socket->write(MESSAGE_LOGINRQ);
        qDebug() << "Login request: " << MESSAGE_LOGINRQ;
        qDebug() << "\"";
        socket->waitForReadyRead(1000);
        login = socket->readAll();
        qDebug() << "Login readed: " << login;
        socket->write(MESSAGE_PASSRQ);
        qDebug() << "password request: " << MESSAGE_PASSRQ;
        socket->waitForReadyRead(1000);
        password = socket->readAll();
        qDebug() << "password readed: " << password;

        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");

            db.setHostName("localhost");
            db.setDatabaseName("gamebase");
            db.setUserName("root");
            db.setPassword("");

            if(!db.open())
                qDebug() << "cant connect to database";
            else
            {
                QStringList tables = db.tables();
                for(int i=0;i<tables.size();i++)
                    qDebug() << tables.at(i);

                QSqlQuery pobieranie;
                QByteArray query = "SELECT * FROM user WHERE login=\""+login+"\" AND password=\""+password+"\"";
                qDebug() << "zapytanie: " << query;
                qDebug() <<  " natomiast login " << login << " haslo " << password;
                if(!pobieranie.exec(query))
                {
                    qDebug() << "No records";
                } else
                {
                    socket->write(pobieranie.value("username").toString().toUtf8());
                }

                db.close();
            }
    }
    else
    {
        qDebug() << "error not messageloginrq";
    }
}

并且一切工作通常都很好,但看起来,在代码中的某个地方或TCP流中的某处都添加了发送字符串的引号,因此发送数据而不是:登录和密码是:&#34; login&#34;和#34;密码&#34;。由于这个查询&#34; SELECT * FROM用户WHERE login = \&#34;&#34; + login +&#34; \&#34; AND密码= \&#34;&#34; +密码+&#34; \&#34;&#34 ;;完全搞砸了,不能正确执行。为什么这段代码的行为如此?

0 个答案:

没有答案