Results 1 to 15 of 15

Thread: QProcess+TcpSocket

  1. #1
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default QProcess+TcpSocket

    my code:

    Qt Code:
    1. Connection::Connection(QObject *parent)
    2. : QTcpSocket(parent)
    3. {
    4. QObject::connect(this, SIGNAL(readyRead()), this, SLOT(processReadyRead()));
    5. QObject::connect(this, SIGNAL(disconnected()), this, SLOT(Disconnected()));
    6. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Connection::processReadyRead()
    2. {
    3.  
    4. char *szData;
    5. QProcess process;
    6. qint16 nOutState;
    7.  
    8. QByteArray block;
    9. QDataStream out(&block, QIODevice::WriteOnly);
    10. out.setVersion(QDataStream::Qt_4_0);
    11.  
    12.  
    13. if( this->isReadable())
    14. {
    15. qint64 qnSize = this->bytesAvailable();
    16. if( qnSize > 0)
    17. {
    18. szData = new char [qnSize+sizeof(szData)];
    19. qint64 qnReadSize = this->read(szData, qnSize);
    20. szData[qnReadSize]=NULL;
    21. }
    22. }
    23.  
    24. QString cData(szData);
    25. QStringList cListCmd;
    26. QString cmd;
    27.  
    28. cListCmd = cData.split(QRegExp("\\s+"));
    29. //For example:
    30. process.start("NOTEPAD.EXE");
    31. nOutState = process.waitForFinished(-1);
    32. delete[] szData;
    33.  
    34.  
    35. block = NULL;
    36. block.append(QString::number(nOutState));
    37. this->write(block);
    38.  
    39.  
    40. this->disconnectFromHost();
    41. if(this->state() == QAbstractSocket::ConnectedState)
    42. this->waitForDisconnected(5000);
    43. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void Connection::Disconnected()
    2. {
    3.  
    4. ::_beep(1000,100);
    5. ::_beep(1000,100);
    6. ::_beep(1000,100);
    7. ::_beep(1000,100);
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 



    QProcess starts the program and should expect its end. The client at this time holds connection and expects end of the program. But at switching-off of the client - I should catch that connection is lost and it is necessary to finish the carried out program. But at me slot Disconnected () works only after closing the program even if the client was disconnected. How to catch the message of separation of the client?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QProcess+TcpSocket

    Quote Originally Posted by Fastman View Post
    my code:

    Qt Code:
    1. this->disconnectFromHost();
    2. if(this->state() == QAbstractSocket::ConnectedState)
    3. this->waitForDisconnected(5000);
    4. }
    To copy to clipboard, switch view to plain text mode 
    I would expect the socket more likely to be in QAbstractSocket::ClosingState..
    J-P Nurmi

  3. #3
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess+TcpSocket

    Thx. replase this line. But the problem has remained.

  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: QProcess+TcpSocket

    Why don't you try the event oriented approach? Connect appropriate signals to slots and wait for them to be fired. What's the point of this waitForDisconnected() loop? If you have a slot that will fire after the disconnected signal is emited, I don't see the point to wait for it again in a loop.

  5. #5
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess+TcpSocket

    Quote Originally Posted by wysota View Post
    Why don't you try the event oriented approach? Connect appropriate signals to slots and wait for them to be fired. What's the point of this waitForDisconnected() loop? If you have a slot that will fire after the disconnected signal is emited, I don't see the point to wait for it again in a loop.

    Thanks for the help! I have made as follows:
    Qt Code:
    1. ...
    2. ...
    3. ...
    4. while(!(this->state()==QAbstractSocket::UnconnectedState))
    5. {
    6. QCoreApplication::processEvents();
    7.  
    8. ...
    9. }
    10. ...
    11. ...
    12. ...
    To copy to clipboard, switch view to plain text mode 

    and all works

  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: QProcess+TcpSocket

    But again, what is the point of having such a loop? The code above is the same as the previous one, only that you change the condition.

  7. #7
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess+TcpSocket

    Quote Originally Posted by wysota View Post
    But again, what is the point of having such a loop? The code above is the same as the previous one, only that you change the condition.
    I have simply shown all code. Now it is traced there is a connection with the client or not... If connection will be lost before performance of my program (coding of video of a file) that coding will be interrupted, if coding will be normally completed and QProcess will return normal end of the program - the client will receive the answer that all ok Thanks for the help At you a unique forum where it is possible to receive the fast and exact answer to a question

  8. #8
    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: QProcess+TcpSocket

    Don't you think listening for the QProcess::finished() signal or checking QProcess::state() is a better way to do it?

  9. #9
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess+TcpSocket

    Quote Originally Posted by wysota View Post
    Don't you think listening for the QProcess::finished() signal or checking QProcess::state() is a better way to do it?
    yes i'am use QProcess::finished() and if get signal finished() send OK state to client.

  10. #10
    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: QProcess+TcpSocket

    So what's the point of monitoring the socket? You don't even need the socket at all... you can use stdin/stdout to transfer all the data you need.

  11. #11
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess+TcpSocket

    Quote Originally Posted by wysota View Post
    So what's the point of monitoring the socket? You don't even need the socket at all... you can use stdin/stdout to transfer all the data you need.
    Each connection of the client and performance of process is processed in a separate stream:

    Qt Code:
    1. Server::Server(QObject *parent):QTcpServer(parent)
    2. {
    3.  
    4. if (!listen(QHostAddress::Any,1715)) {
    5. return;
    6. }
    7.  
    8. }
    9.  
    10. Server::~Server()
    11. {
    12.  
    13. }
    14.  
    15. void Server::incomingConnection(int socketDescriptor)
    16. {
    17. FortuneThread *thread = new FortuneThread(socketDescriptor,this);
    18. thread->start();
    19. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void FortuneThread::run()
    2. {
    3. Connection connection;
    4. if(!connection.setSocketDescriptor(socketDescriptor))
    5. {
    6. emit error(connection.error());
    7. return;
    8. }
    9. connect(&connection, SIGNAL(disconnected()), this, SLOT(deleteConnection()));
    10. exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    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: QProcess+TcpSocket

    I don't see a relation to what the busy loop here... If a connection drops, you'll get an error trying to write to the socket, I don't see what waitForDisconnected() is needed for... And processing application events from within a worker thread (because I assume it is the thread that calls processEvents()) seems suspicious.

  13. #13
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess+TcpSocket

    I shall explain. The client has an adjustment - how many simultaneously streams to start. The client considers quantity simultaneously started streams and holds connection until then while process of coding of a file will be completed or there will be a mistake. The server is located on unix, and the client on Windows. Earlier this system on C has been written. But I have decided to try with studying QT to copy this part on QT/C ++

  14. #14
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess+TcpSocket

    Quote Originally Posted by wysota View Post
    I don't see a relation to what the busy loop here... If a connection drops, you'll get an error trying to write to the socket, I don't see what waitForDisconnected() is needed for... And processing application events from within a worker thread (because I assume it is the thread that calls processEvents()) seems suspicious.

    if i'am comment QCoreApplication:rocessEvents();
    this->state() on that does not change

  15. #15
    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: QProcess+TcpSocket

    Quote Originally Posted by Fastman View Post
    I shall explain. The client has an adjustment - how many simultaneously streams to start. The client considers quantity simultaneously started streams and holds connection until then while process of coding of a file will be completed or there will be a mistake. The server is located on unix, and the client on Windows. Earlier this system on C has been written. But I have decided to try with studying QT to copy this part on QT/C ++
    But what does waitForDisconnected() do for you? I don't see any point in using it... You have a slot connected to the disconnected() signal, so why wait for the signal in a busy loop?

    Quote Originally Posted by Fastman View Post
    if i'am comment QCoreApplication:rocessEvents();
    this->state() on that does not change
    This is obvious, because timers won't fire and the state won't be able to change.

    Maybe processEvents() is wise enough to call the appropriate event queue based on the current thread id and thanks to that your application doesn't crash...

Similar Threads

  1. QProcess & Lame
    By clive in forum Qt Programming
    Replies: 3
    Last Post: 4th August 2007, 19:37
  2. QProcess and Pipes
    By KaptainKarl in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2007, 23:11
  3. QProcess extremely slow on Windows?
    By Pepe in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2007, 00:25
  4. problem with qprocess
    By deekayt in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2006, 13:30
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15:52

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.