Results 1 to 7 of 7

Thread: QtcpSocket readyRead Problem with Java Client

  1. #1
    Join Date
    Jan 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QtcpSocket readyRead Problem with Java Client

    Hi!

    AT the moment i'm trying to develop a server with Qt and a client in java. But thing don't work as i like. I'm even having troubles receiving a string with my Qt server.
    Here is a little bit of code:

    The important part of my java client:
    Qt Code:
    1. public boolean sendString(){
    2.  
    3. try{
    4. final PrintWriter pw = new PrintWriter(mSocket.getOutputStream(), true);
    5. pw.println("Test String");
    6. return true;
    7. } catch(Exception e){
    8. // to be coded
    9. return false;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    and here my qt part:
    Qt Code:
    1. [...]
    2. QTcpSocket clientConnection = tcpServer->nextPendingConnection();
    3. connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readClient()));
    4. [...]
    5.  
    6. void Server::readClient(void)
    7. {
    8. ui.clientLabel->setText(tr("entered readClient"));
    9. }
    To copy to clipboard, switch view to plain text mode 

    Now, my problem is, that readClient() isn't even called. So I think the SIGNAL(readyRead()) is never executed.
    I suspect that I can't send a String like this. But what do I do wrong?

    Thanks,
    Bernie

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QtcpSocket readyRead Problem with Java Client

    Qt Code:
    1. QTcpSocket clientConnection = tcpServer->nextPendingConnection();
    To copy to clipboard, switch view to plain text mode 

    That's not correct.

  3. #3
    Join Date
    Jan 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtcpSocket readyRead Problem with Java Client

    Thanks for your reply. But I made a copy paste mistake. QTcpSocket is a Pointer. (tcpServer as well)

    Qt Code:
    1. QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    To copy to clipboard, switch view to plain text mode 

    or is nextPendingConnection(); wrong?

  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: QtcpSocket readyRead Problem with Java Client

    How do you know readClient() is never called? Can you show us the header for your Server class?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtcpSocket readyRead Problem with Java Client

    Here is the Header File:

    Qt Code:
    1. #include <QtGui/QMainWindow>
    2. #include <QTcpServer>
    3. #include <QTcpSocket>
    4. #include <QNetworkInterface>
    5. #include "ui_server.h"
    6.  
    7. QT_BEGIN_NAMESPACE
    8. class QLabel;
    9. class QTcpServer;
    10. class QNetworkSession;
    11. QT_END_NAMESPACE
    12.  
    13. class Server : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. Server(QWidget *parent = 0, Qt::WFlags flags = 0);
    19. ~Server();
    20.  
    21. private:
    22. Ui::ServerClass ui;
    23.  
    24. QTcpServer *tcpServer;
    25. QTcpSocket *clientConnection;
    26.  
    27. private slots:
    28. void doMagic(void);
    29. void sendTestString(void);
    30. void openImage(void);
    31. void startServer(void);
    32. void stopServer(void);
    33. void readClient(void);
    34. };
    To copy to clipboard, switch view to plain text mode 

    and the cpp looks like this:

    Qt Code:
    1. #include <QtGui>
    2. #include <QtNetwork>
    3.  
    4.  
    5. #include "server.h"
    6.  
    7. Server::Server(QWidget *parent, Qt::WFlags flags)
    8. : QMainWindow(parent, flags)
    9. {
    10. ui.setupUi(this);
    11.  
    12. ui.statusLabel->setText(tr("Server is not running"));
    13.  
    14. connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(doMagic()));
    15. connect(ui.quitButton, SIGNAL(clicked()), this, SLOT(close()));
    16. connect(ui.imageButton, SIGNAL(clicked()), this, SLOT(openImage()));
    17. connect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(startServer()));
    18.  
    19.  
    20. }
    21.  
    22. Server::~Server()
    23. {
    24.  
    25. }
    26.  
    27. void Server::doMagic(void)
    28. {
    29. ui.textEdit->append("Blaaa");
    30. }
    31.  
    32. void Server::startServer(void)
    33. {
    34.  
    35. tcpServer = new QTcpServer(this);
    36. tcpServer->listen(QHostAddress::Any,8001);
    37. if (!tcpServer->isListening()) {
    38. QMessageBox::critical(this, tr("Server"), tr("Unable to start server: %1.").arg(tcpServer->errorString()));
    39. close();
    40. return;
    41. }
    42.  
    43. ui.startstopButton->setText(tr("Stop Server"));
    44. ui.statusLabel->setText(tr("The server is running on port %1.\n").arg(tcpServer->serverPort()));
    45.  
    46. connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendTestString()));
    47. disconnect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(startServer()));
    48. connect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(stopServer()));
    49.  
    50. }
    51.  
    52. void Server::stopServer(void)
    53. {
    54. if(tcpServer->isListening()){
    55.  
    56. tcpServer->close();
    57. }
    58. ui.statusLabel->setText(tr("Server stopped!"));
    59. disconnect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(stopServer()));
    60. connect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(startServer()));
    61. ui.startstopButton->setText(tr("Start Server"));
    62.  
    63. }
    64.  
    65. void Server::readClient(void)
    66. {
    67. ui.clientLabel->setText(tr("readClient()"));
    68. }
    69.  
    70. void Server::sendTestString(void)
    71. {
    72. //ui.clientLabel->setText(tr("String sent"));
    73.  
    74. QByteArray block;
    75. QDataStream out(&block, QIODevice::WriteOnly);
    76. out.setVersion(QDataStream::Qt_4_0);
    77.  
    78. out << (quint16)0;
    79. out << QString("Test").toUtf8();
    80. out.device()->seek(0);
    81. out << (quint16)(block.size() - sizeof(quint16));
    82.  
    83. clientConnection = tcpServer->nextPendingConnection();
    84. connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
    85. connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readClient()));
    86.  
    87.  
    88. clientConnection->write(block);
    89. clientConnection->disconnectFromHost();
    90.  
    91. }
    92. void Server::openImage(void)
    93. {
    94. QString fileName = QFileDialog::getOpenFileName(this,
    95. tr("Open File"), QDir::currentPath());
    96. if (!fileName.isEmpty()) {
    97. QImage image(fileName);
    98. if (image.isNull()) {
    99. QMessageBox::information(this, tr("Image Viewer"),
    100. tr("Cannot load %1.").arg(fileName));
    101. return;
    102. }
    103. }
    104. }
    To copy to clipboard, switch view to plain text mode 

    I know that readClient(/) is never called because otherwise my UI would have shown "readClient()".

  6. #6
    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: QtcpSocket readyRead Problem with Java Client

    I know that readClient(/) is never called because otherwise my UI would have shown "readClient()".
    No, not really. But it is true that readClient() is never called. Right after you establish the connection you call disconnectFromHost() (which in turn deletes the socket) which makes it impossible to receive any data from the client.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtcpSocket readyRead Problem with Java Client

    Ohhh,
    sorry for my late reply. But thank you.
    It now works.

Similar Threads

  1. QTcpSocket and readyRead and QTimer
    By cafu in forum Qt Programming
    Replies: 2
    Last Post: 18th December 2009, 13:36
  2. Qt as Client and Java as a Server. It's that possible ?
    By eroskoller in forum Qt Programming
    Replies: 6
    Last Post: 29th May 2009, 19:19
  3. Question in readyRead(QTCPSOCKET)
    By morgana in forum Qt Programming
    Replies: 2
    Last Post: 24th July 2008, 18:11
  4. QTcpSocket readyRead and buffer size
    By pdoria in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2008, 10:11
  5. Replies: 1
    Last Post: 1st November 2007, 14:09

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.