Results 1 to 4 of 4

Thread: QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState

  1. #1
    Join Date
    May 2019
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Exclamation QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState

    Im having trouble with my telnet connection by TCP where i need to enter the initials twice as the server needs this (not my server) but then when i have the initials entered once and are about to type them in the second time it seems like im getting thrown off
    this is the code when i push a button it needs to connect by TCP to a ip entered in the GUI
    IMPORTANT when i try and connect to the server with putty it works fine
    Qt Code:
    1. void MainWindow::on_reboot_clicked()
    2. {
    3. QString hostName = ui->hostName->text();
    4. int port = ui->Port->text().toInt();
    5.  
    6. socket = new QTcpSocket(this);
    7.  
    8. //connection
    9. socket->connectToHost(hostName, port, QIODevice::ReadWrite);
    10.  
    11. if(socket->waitForConnected(300)){//if succesfull connection run this
    12. qDebug() << "connected";
    13.  
    14. //sending initials first time
    15. socket->write("admin\r\n");
    16. socket->waitForReadyRead(10);
    17. qDebug() << "reading first" << socket->readAll();
    18. socket->waitForBytesWritten(10);
    19. //socket->waitForReadyRead(10);
    20. socket->write("Admin1\r\n");
    21. socket->waitForReadyRead(10);
    22. socket->waitForBytesWritten(10);
    23. //socket->waitForReadyRead(10);
    24. qDebug() << "reading second" << socket->readAll();
    25.  
    26. //sending initials second time
    27. socket->write("admin\r\n");
    28. socket->waitForReadyRead(10);
    29. socket->waitForBytesWritten(10);
    30. //socket->waitForReadyRead(10);
    31. qDebug() << "reading third" << socket->readAll();
    32. socket->write("Admin1\r\n");
    33. socket->waitForReadyRead(10);
    34. socket->waitForBytesWritten(10);
    35. //socket->waitForReadyRead(10);
    36. qDebug() << "reading fourth" << socket->readAll();
    37.  
    38. //reboot command
    39. socket->write("reboot\r\n");
    40. socket->waitForBytesWritten(2000); // waiting the 2 seconds for the reboot command to take effect
    41.  
    42. qDebug() << "done";
    43. //closing connection
    44. socket->close();
    45.  
    46. }
    47. else {//if the program couldn't connect the program will output this in debug terminal
    48. qDebug() << "not connected";
    49.  
    50. }
    51. }
    To copy to clipboard, switch view to plain text mode 

    Here is what i am getting on the output terminal

    connected
    reading first "\xFF\xFB\x01"
    reading second "\xFF\xFB\x03"
    QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState
    reading third ""
    QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState
    reading fourth ""
    QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState
    done

    and i just can't seem to figure out what is wrong
    Last edited by anda_skoa; 8th May 2019 at 15:08. Reason: changed [qtclass] to [code]

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState

    Well, given the error messages it seems the socket has disconnected at some point.

    Aside from blocking I/O in a GUI not being a good idea, why do you wait for write after waiting for read?
    Would it make more sense either just wait for the response or wait for sending before waiting for the response?

    You also never check the return values of these waits.

    Cheers,
    _

  3. #3
    Join Date
    May 2019
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState

    Quote Originally Posted by anda_skoa View Post
    Well, given the error messages it seems the socket has disconnected at some point.

    Aside from blocking I/O in a GUI not being a good idea, why do you wait for write after waiting for read?
    Would it make more sense either just wait for the response or wait for sending before waiting for the response?

    You also never check the return values of these waits.

    Cheers,
    _
    yeah i can see what you mean that was an error i have changed it now so that i wait for bytes written first and then wait for a ready read and i might sound like a newbie but what response would i get from the the waits?
    And do you have any idea as to how i could change to code to maybe try non blocking communication

    my code as of now
    Qt Code:
    1. void MainWindow::on_reboot_clicked()
    2. {
    3. QString hostName = ui->hostName->text();
    4. int port = ui->Port->text().toInt();
    5.  
    6. socket = new QTcpSocket(this);
    7.  
    8. //connection
    9. socket->connectToHost(hostName, port, QIODevice::ReadWrite);
    10.  
    11. if(socket->waitForConnected(300)){//if succesfull connection run this
    12. qDebug() << "connected";
    13.  
    14. //sending initials first time
    15. socket->write("admin");
    16. socket->waitForBytesWritten(10);
    17. socket->waitForReadyRead(10);
    18. qDebug() << "reading first" << socket->readAll();
    19. socket->write("Admin1");
    20. socket->waitForBytesWritten(10);
    21. socket->waitForReadyRead(10);
    22. qDebug() << "reading second" << socket->readAll();
    23.  
    24. //reboot command
    25. socket->write("reboot");
    26. socket->waitForBytesWritten(2000); // waiting the 2 seconds for the reboot command to take effect
    27.  
    28. qDebug() << "done";
    29. //closing connection
    30. socket->close();
    31.  
    32. }
    33. else {//if the program couldn't connect the program will output this in debug terminal
    34. qDebug() << "not connected";
    35.  
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 10th May 2019 at 09:05. Reason: changed [qtcode] to [code]

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState

    Quote Originally Posted by liam0connor View Post
    i might sound like a newbie but what response would i get from the the waits?
    The waitForXYZ methods return true if the signal has been emitted or false if that was not the case.

    Quote Originally Posted by liam0connor View Post
    And do you have any idea as to how i could change to code to maybe try non blocking communication
    By connecting to the signals instead of using the waitFor methods.

    Usually the pattern is a "Command" or "Job", an object that encapsulates the steps necessary to achieve the goal.

    Something like
    Qt Code:
    1. class RebootCommand : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit RebootCommand(QObject *parent = nullptr);
    6.  
    7. void start(const QString &host, int port);
    8.  
    9. signals:
    10. void finished();
    11.  
    12. private slots:
    13. void onConnected();
    14. void onReadyRead();
    15.  
    16. private:
    17. enum State {
    18. Disconnected,
    19. WaitingForUserPrompt1,
    20. WaitingForPasswordPrompt1,
    21. WaitingForUserPrompt2,
    22. WaitingForPasswordPrompt2
    23. };
    24. State m_state = Disconnected;
    25.  
    26. QTcpSocket *m_socket = nullptr;
    27. };
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 8th May 2019, 11:32
  2. Qserialdevice 2.0 waitForBytesWritten halfduplex question
    By egadget1 in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2012, 22:11
  3. Replies: 1
    Last Post: 8th October 2010, 13:21
  4. QUdpSocket and UnconnectedState problem
    By RThaden in forum Qt Programming
    Replies: 0
    Last Post: 14th September 2009, 18:20
  5. waitForBytesWritten
    By smalls in forum Qt Programming
    Replies: 8
    Last Post: 1st March 2006, 17:42

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.