Updating a textbox with a text sent from client



  • Hello,
    I am trying to create a simple chat between client and server using socket programming.
    I want to update the text in server dialog box to a message sent from client.
    I am not able to figure out how the message should go to the textbox in server dialog box.
    Here is my function to send data from client : .cpp

    void Client::senddata() //----------------------------------------------
    {

    send->setText("clicked");
    
    textinput = messageedit->text();
    
    if(!textinput.isEmpty())
    

    {

    tcpSocket->write(QString(textinput).toUtf8());
      //  tcpSocket->write((textinput).toUtf8());
        tcpSocket->flush();
    

    }
    } #

    and server function to read data :

    void Server::on_readdata()
    {

    //while(tcpSocket->canReadLine())
    

    // {

     getbutton->setText("recieved");
     QByteArray ba = tcpSocket->readLine();
    
     //QString textinput = ba;
     QString textinput(ba);
    /* if(strcmp(ba.constData(), "!exit\n") == 0)
     {
         tcpSocket->disconnectFromHost();
         break;
     } */
    
     message->setText((const QString)textinput);
    

    } #

    Thanks a lot in advance,
    Mansi



  • client code :

    @void Client::senddata() //———————————————————————
    {
    send->setText(“clicked”); textinput = messageedit->text(); if(!textinput.isEmpty()) { tcpSocket->write(QString(textinput).toUtf8()); // tcpSocket->write((textinput).toUtf8()); tcpSocket->flush(); } } #

    and server function to read data :@

    server code
    @void Server::on_readdata() {
    //while(tcpSocket->canReadLine()) // { getbutton->setText(“recieved”); QByteArray ba = tcpSocket->readLine(); //QString textinput = ba; QString textinput(ba); /* if(strcmp(ba.constData(), “!exit\n”) == 0) { tcpSocket->disconnectFromHost(); break; } */ message->setText((const QString)textinput); }@


  • Moderators

    check out the "Fortune example":http://qt-project.org/doc/qt-4.8/network-fortuneserver.html , it should clear things up for you.
    In the example the clients reads the data from the server.

    Basically you need to listen to the readyRead() signal which gets triggered every time new data is available. Until there is no more data which was sent.

    I don't know how/when you trigger on_readdata(), but i think you call it too early and thus it wont do anything at the end.



  • thanks a lot raven :) ... with little changes in the code , able to send messages bi directionally :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.