Results 1 to 3 of 3

Thread: Server sends me empty message

  1. #1
    Join Date
    May 2013
    Location
    Georgia,Tbilisi
    Posts
    32
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question Server sends me empty message

    Hi all.
    I'm writting client-server chat application. Client have disconnect button,when I disconnect connection between client and server and then I again click connect button everything works well but when I sends server message to client,server message sends 2 message:

    1. actual message which I want to send
    2. empty message

    I ca't figure out what can be wrong with my code

    picture of both
    Capture.jpg

    server code(important functions only)
    Qt Code:
    1. void Server::listenTo()
    2. {
    3. bool result = server->listen(QHostAddress(ui->lineEdit_ipaddress->text()),ui->lineEdit_port->text().toInt());
    4. if(!result){
    5. labelText(ui->label_status,"Can't listen",Qt::red);
    6. return;
    7. }
    8.  
    9. labelText(ui->label_status,"Listening...",Qt::green);
    10. changeButtonStates(false,false,false);
    11. }
    12. void Server::disconnectFromClient()
    13. {
    14. if(socket != nullptr && socket->isOpen())
    15. socket->close();
    16. labelText(ui->label_status,"Listening...",Qt::green);
    17. changeButtonStates(false,false,false);
    18. }
    19. void Server::sendMessage()
    20. {
    21. if(socket != nullptr && socket->isOpen()){
    22. socket->write(qPrintable(ui->textEdit_msgArea->toPlainText()));
    23.  
    24. ui->textEdit_chat->append("<Server>: " + ui->textEdit_msgArea->toPlainText());
    25. ui->textEdit_msgArea->clear();
    26. }
    27. }
    28. void Server::newClient()
    29. {
    30. socket = server->nextPendingConnection();
    31. labelText(ui->label_status,"Connected to client",Qt::green);
    32. changeButtonStates(false,true,true);
    33.  
    34. connect(socket,SIGNAL(readyRead()),this,SLOT(readFromClient()));
    35. connect(socket,SIGNAL(disconnected()),this,SLOT(closeSocket()));
    36. }
    37. void Server::readFromClient()
    38. {
    39. QString clientMessage = socket->readAll();
    40. ui->textEdit_chat->append("<Client>: " + clientMessage);
    41. }
    42. void Server::closeSocket()
    43. {
    44. socket->close();
    45. changeButtonStates(false,false,false);
    46. labelText(ui->label_status,"Listening...",Qt::green);
    47. }
    To copy to clipboard, switch view to plain text mode 

    Client code:
    Qt Code:
    1. m_socket = new QTcpSocket(this);
    2. connect(m_socket,SIGNAL(connected()),this,SLOT(msgBoxConnected()));
    3. connect(m_socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(socketError(QAbstractSocket::SocketError)));
    4. connect(ui->pushButton_connect,SIGNAL(clicked()),this,SLOT(connectToServer()));
    5. connect(ui->pushButton_disconnect,SIGNAL(clicked()),this,SLOT(disconnectFromServer()));
    6. connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMessage()));
    7.  
    8. void Client::connectToServer()
    9. {
    10. m_socket->connectToHost(QHostAddress(ui->lineEdit_ipaddress->text()),ui->lineEdit_port->text().toInt());
    11. }
    12.  
    13. void Client::disconnectFromServer()
    14. {
    15. m_socket->close();
    16. changeButtonsState(true,false,false);
    17. labelText(ui->label_status,"status",Qt::black);
    18.  
    19.  
    20. }
    21.  
    22. void Client::sendMessage()
    23. {
    24. ui->txtEdit_chat->append("<Client>: " + ui->textEdit_msgArea->toPlainText());
    25.  
    26. m_socket->write(qPrintable(ui->textEdit_msgArea->toPlainText()));
    27. ui->textEdit_msgArea->clear();
    28. }
    29. void Client::msgBoxConnected()
    30. {
    31. connect(m_socket,SIGNAL(readyRead()),this,SLOT(readData()));
    32. labelText(ui->label_status,"connected",Qt::green);
    33.  
    34. changeButtonsState(false,true,true);
    35. }
    36. void Client::readData()
    37. {
    38. msgbox("readData called");
    39. ui->txtEdit_chat->append("<Server>: " + m_socket->readAll());
    40. }
    To copy to clipboard, switch view to plain text mode 

    Have any idea?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Server sends me empty message

    Where You disconnect readyRead() signal from readData() slot after socket closing ?

  3. The following user says thank you to Lesiok for this useful post:

    Higgs (21st July 2014)

  4. #3
    Join Date
    May 2013
    Location
    Georgia,Tbilisi
    Posts
    32
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Server sends me empty message

    thank you so much
    I didn't call disconnect function after socket closing.

Similar Threads

  1. Server sent message not reaching client
    By tiredtyrant in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2014, 13:12
  2. how to know which is the class that sends the signal in qt ?
    By sliverTwist in forum Qt Programming
    Replies: 4
    Last Post: 23rd April 2013, 12:22
  3. Fancy Browser, Windows Server 2008 and empty webpage
    By drewpt in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2011, 00:35
  4. Replies: 3
    Last Post: 1st February 2007, 20:00
  5. remove directory empty or not empty
    By raphaelf in forum Newbie
    Replies: 12
    Last Post: 27th October 2006, 07:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.