Results 1 to 11 of 11

Thread: My client can't send data

  1. #1
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question My client can't send data

    hi,all.I want to realize the communication between the server and client,but I have tested that the client can't send data,although I have thought for quite a while,I couldn't find the reason,because I have studied to use it for a short time.The send function for client and receive function for server are as following:
    void Client::sendFortune(){
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_2);
    out << (quint16)0;
    out << fortunes.at(qrand() % fortunes.size());
    out.device()->seek(0);
    out << (quint16)(block.size() - sizeof(quint16));
    tcpSocket->write(block);
    }

    void Server::readFortune(){
    QDataStream in(tcpsocket);
    in.setVersion(QDataStream::Qt_4_2);
    if (blockSize == 0) {
    if (tcpsocket->bytesAvailable() < (int)sizeof(quint16))
    return;
    in >> blockSize;
    }
    if (tcpsocket->bytesAvailable() < blockSize)
    return;
    QString nextFortune;
    in >> nextFortune;
    if (nextFortune == currentFortune) {
    QTimer::singleShot(0, this, SLOT(requestNewConnect()));
    return;
    }
    currentFortune = nextFortune;
    }
    Anyone who knows why,please tell me,thank you very much!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: My client can't send data

    How do you know the data is not sent?

  3. #3
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: My client can't send data

    Thank you for your reply!
    In the client's send function I added
    tcpSocket->write(block);
    connect(tcpSocket,SIGNAL(bytesWritten(qint64)),thi s,SLOT(flag()));
    ....
    void Client::flag(){
    QMessageBox::information(this, tr("Fortune Client"),
    tr("data has been sent"));
    }
    but there is no QMessageBox,and in the server's send function I added
    tcpsocket = tcpServer->nextPendingConnection();
    tcpsocket->write(block);
    connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(re adTest()));
    .....
    void Server::readTest(){
    statusLabel->setText("ready read");//statusLabel is a label on the window
    }
    there is also no "ready read".
    Here in the loop,after the server feels connection,it sends data,in the send function's end it reveive data.For the client,when it receives data,in the receive function's end it sends data.Wether I have some misunderstanding TCP/IP protocal?Thank you very much!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: My client can't send data

    Could you use some network sniffer to see if the data is sent and what data is sent? Is the connection open before you try to send data? What does the write() method return?

  5. #5
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: My client can't send data

    Sorry,I don't know how to use network sniffer,here I attach the whole function of server and client.The client can receive data and show them on the window.
    Attached Files Attached Files

  6. #6
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: My client can't send data

    Just now I clicked the getFortuneButton for more than 100 times,about more 40 times the client receives data,after that it shows "data has been sent" for another more than 40 times and during this time the client doesn't receive data,but the server still has no change .Then the client receives data ....
    Why?What's the problem?I am headache now.Please help me.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: My client can't send data

    Where do you connect in the client? I can't see the respective code...

  8. #8
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: My client can't send data

    void Client::requestNewFortune(){
    .....
    tcpSocket->connectToHost(hostLineEdit->text(),
    portLineEdit->text().toInt());
    }
    it can receive the right data from server.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: My client can't send data

    And how are your signals and slots set up? How do you react to a successful connection? Do you have an event loop running in the thread which owns the socket?

  10. #10
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: My client can't send data

    I made some change again.
    For the server:
    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
    in the sendFortune()
    tcpsocket = tcpServer->nextPendingConnection();
    connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(re adFortune()));
    in the readFortune()
    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
    For the client:
    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
    in the readFortune()
    if(tcpSocket->isWritable())
    sendFortune();
    in the sendFortune()
    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
    Wether I made a mistake?Please tell me,thank you!
    Last edited by hiuao; 11th February 2007 at 06:33.

  11. #11
    Join Date
    Feb 2007
    Posts
    47
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: My client can't send data

    I have found the problem but I can't understand.The data sent by server can be received by client,but the client send the some form of data ,the server can't receive.Then I let the client one data which I use actually,then the server can receive.
    Why?Can anyone tell me?

Similar Threads

  1. QSqlQueryModel data update
    By psi in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2012, 04:59
  2. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 13:53
  3. QFtp->List() does not recieve any data
    By nopalot in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2006, 21:42
  4. Replies: 16
    Last Post: 7th March 2006, 16:57
  5. synching client readings to server output
    By OnionRingOfDoom in forum Qt Programming
    Replies: 14
    Last Post: 28th January 2006, 19:15

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.