Results 1 to 11 of 11

Thread: problem in QAbstractSocket::SocketError

  1. #1
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default problem in QAbstractSocket::SocketError

    Hi everyone,
    I am using qt 4.6.2. I am trying to use socket error for unplugging the network cable.I am using connect function to get the error.But when i removed the cable no error is occurring .

    Qt Code:
    1. connect(clt,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(connection_error(QAbstractSocket::SocketError)));
    2.  
    3.  
    4. switch(err)
    5. {
    6. case 0:QMessageBox::critical(0,"connection error","The connection was refused by the peer (or timed out).",QMessageBox::Ok);
    7. break;
    8. case 2:QMessageBox::critical(0,"connection error","The host address was not found.",QMessageBox::Ok);
    9. break;
    10. case QAbstractSocket::NetworkError:QMessageBox::critical(0,"connection error","An error occurred with the network .",QMessageBox::Ok);
    11. break;
    12.  
    13. case QAbstractSocket::RemoteHostClosedError:
    14. QMessageBox::critical(0,"connection error","disconnect .",QMessageBox::Ok);
    15. break;
    16. default:QMessageBox::critical(0,"connection error","undefine error.",QMessageBox::Ok);
    17. qDebug()<<"error is ......."<<err;
    18. break;
    19. }
    To copy to clipboard, switch view to plain text mode 


    Is there anything to do more ?

  2. #2
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: problem in QAbstractSocket::SocketError

    After remove network cable do you use any socket function?

    I think if remove cable you receive stateChanged or disconnected signal. And receive error only if you do any socket operation after cable removing.
    Try read Qt documentation before ask stupid question.

  3. #3
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: problem in QAbstractSocket::SocketError

    I have function for stateChanged and disconnected signal.But when i removed the cable no one is called.

  4. #4
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: problem in QAbstractSocket::SocketError

    ok. i try code and will tell later. But say me plz, what class is clt ?

    And your OS plz.
    Last edited by unit; 11th March 2011 at 16:02.
    Try read Qt documentation before ask stupid question.

  5. #5
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: problem in QAbstractSocket::SocketError

    .h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QTcpSocket>
    6. #include <QDateTime>
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. private:
    21. QString statetoString(QAbstractSocket::SocketState);
    22.  
    23. private slots:
    24. void on_pushButton_clicked();
    25. void st_connected();
    26. void st_error(QAbstractSocket::SocketError);
    27. void st_stateChange(QAbstractSocket::SocketState);
    28. void st_disconnected();
    29.  
    30. void on_pushButton_2_clicked();
    31.  
    32. private:
    33. Ui::MainWindow *ui;
    34. QTcpSocket *socket;
    35. };
    36.  
    37. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    .cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. socket = new QTcpSocket();
    10. connect(socket,SIGNAL(connected()),this,SLOT(st_connected()));
    11. connect(socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(st_error(QAbstractSocket::SocketError)));
    12. connect(socket,SIGNAL(disconnected()),this,SLOT(st_disconnected()));
    13. connect(socket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(st_stateChange(QAbstractSocket::SocketState)));
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. delete ui;
    19. delete socket;
    20. }
    21.  
    22. void MainWindow::on_pushButton_clicked()
    23. {
    24. socket->connectToHost("adsl.by", 80);
    25. }
    26.  
    27. void MainWindow::st_connected()
    28. {
    29. ui->textEdit->append("Connected at " + QDateTime::currentDateTime().toString("hh:mm:ss dd.MM.yyyy"));
    30. }
    31.  
    32. void MainWindow::st_disconnected()
    33. {
    34. ui->textEdit->append("Disconnected at " + QDateTime::currentDateTime().toString("hh:mm:ss dd.MM.yyyy"));
    35. }
    36.  
    37. void MainWindow::st_error(QAbstractSocket::SocketError socketError)
    38. {
    39. ui->textEdit->append("Error " + socket->errorString() + " at " + QDateTime::currentDateTime().toString("hh:mm:ss dd.MM.yyyy"));
    40. }
    41.  
    42. void MainWindow::st_stateChange(QAbstractSocket::SocketState socketState)
    43. {
    44. ui->textEdit->append("State change to " + statetoString(socketState) + " at " + QDateTime::currentDateTime().toString("hh:mm:ss dd.MM.yyyy"));
    45. }
    46.  
    47. void MainWindow::on_pushButton_2_clicked()
    48. {
    49. if(socket->isOpen())
    50. socket->disconnectFromHost();
    51. }
    52.  
    53. QString MainWindow::statetoString(QAbstractSocket::SocketState socketState)
    54. {
    55. QString statestring;
    56. switch(socketState)
    57. {
    58. case QAbstractSocket::UnconnectedState : statestring="the socket is not connected";
    59. break;
    60. case QAbstractSocket::HostLookupState : statestring="the socket is performing a host name lookup";
    61. break;
    62. case QAbstractSocket::ConnectingState : statestring="the socket has started establishing a connection";
    63. break;
    64. case QAbstractSocket::ConnectedState : statestring="a connection is established";
    65. break;
    66. case QAbstractSocket::BoundState : statestring="the socket is bound to an address and port";
    67. break;
    68. case QAbstractSocket::ClosingState : statestring="the socket is about to close";
    69. break;
    70. case QAbstractSocket::ListeningState : statestring="listening state";
    71. break;
    72. default: statestring="unknown state";
    73. }
    74. return statestring;
    75. }
    To copy to clipboard, switch view to plain text mode 


    i'm coded and have next result when remove cable after connect button clicked

    Qt Code:
    1. State change to the socket is performing a host name lookup at 21:42:39 11.03.2011
    2. State change to the socket has started establishing a connection at 21:42:40 11.03.2011
    3. State change to a connection is established at 21:42:40 11.03.2011
    4. Connected at 21:42:40 11.03.2011
    5. Error The remote host closed the connection at 21:42:51 11.03.2011
    6. State change to the socket is about to close at 21:42:51 11.03.2011
    7. State change to the socket is not connected at 21:42:51 11.03.2011
    8. Disconnected at 21:42:51 11.03.2011
    To copy to clipboard, switch view to plain text mode 
    Last edited by unit; 11th March 2011 at 20:52.
    Try read Qt documentation before ask stupid question.

  6. #6
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: problem in QAbstractSocket::SocketError

    Thanks for reply .But the above code is not working for me when i removed the cable.
    I am using fedora 11 (64bit linux kernel 2.6.29) .The clt is a object of client class which inherits QTcpSocket class.

  7. #7
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: problem in QAbstractSocket::SocketError

    Maybe it's platform specific or network card driver problem.

    If you want known interface state try use QNetworkConfiguration
    Try read Qt documentation before ask stupid question.

  8. #8
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem in QAbstractSocket::SocketError

    Quote Originally Posted by sattu View Post
    Hi everyone,
    I am using qt 4.6.2. I am trying to use socket error for unplugging the network cable.I am using connect function to get the error.But when i removed the cable no error is occurring .
    Is there anything to do more ?
    http://doc.trolltech.com/latest/qnet....html#isOnline

  9. #9
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: problem in QAbstractSocket::SocketError

    QNetworkConfigurationManager class was introduced in Qt 4.7.But i am using 4.6.2.So how can i add this class in my qt application ?

  10. #10
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: problem in QAbstractSocket::SocketError

    Do you can use 4.7.2 ?
    Try read Qt documentation before ask stupid question.

  11. #11
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: problem in QAbstractSocket::SocketError

    I tried the program in qt 4.7 (os xp2) and it is working fine.I think there is some problem in qt4.6.2
    thanks

Similar Threads

  1. Replies: 1
    Last Post: 8th October 2010, 12:21
  2. Problem with QAbstractSocket and states.
    By nomadscarecrow in forum Qt Programming
    Replies: 0
    Last Post: 17th March 2010, 15:55
  3. Replies: 4
    Last Post: 25th January 2009, 05:40
  4. Replies: 1
    Last Post: 25th February 2008, 10:36
  5. QAbstractSocket::abort() with SIGSEGV
    By mtrpoland in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2008, 17:05

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.