PDA

View Full Version : problem in QAbstractSocket::SocketError



sattu
11th March 2011, 14:55
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 .



connect(clt,SIGNAL(error(QAbstractSocket::SocketEr ror)),this,SLOT(connection_error(QAbstractSocket:: SocketError)));


switch(err)
{
case 0:QMessageBox::critical(0,"connection error","The connection was refused by the peer (or timed out).",QMessageBox::Ok);
break;
case 2:QMessageBox::critical(0,"connection error","The host address was not found.",QMessageBox::Ok);
break;
case QAbstractSocket::NetworkError:QMessageBox::critica l(0,"connection error","An error occurred with the network .",QMessageBox::Ok);
break;

case QAbstractSocket::RemoteHostClosedError:
QMessageBox::critical(0,"connection error","disconnect .",QMessageBox::Ok);
break;
default:QMessageBox::critical(0,"connection error","undefine error.",QMessageBox::Ok);
qDebug()<<"error is ......."<<err;
break;
}



Is there anything to do more ?

unit
11th March 2011, 15:00
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.

sattu
11th March 2011, 15:31
I have function for stateChanged and disconnected signal.But when i removed the cable no one is called.

unit
11th March 2011, 15:34
ok. i try code and will tell later. But say me plz, what class is clt ?

And your OS plz.

unit
11th March 2011, 19:48
.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTcpSocket>
#include <QDateTime>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
QString statetoString(QAbstractSocket::SocketState);

private slots:
void on_pushButton_clicked();
void st_connected();
void st_error(QAbstractSocket::SocketError);
void st_stateChange(QAbstractSocket::SocketState);
void st_disconnected();

void on_pushButton_2_clicked();

private:
Ui::MainWindow *ui;
QTcpSocket *socket;
};

#endif // MAINWINDOW_H


.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
socket = new QTcpSocket();
connect(socket,SIGNAL(connected()),this,SLOT(st_co nnected()));
connect(socket,SIGNAL(error(QAbstractSocket::Socke tError)),this,SLOT(st_error(QAbstractSocket::Socke tError)));
connect(socket,SIGNAL(disconnected()),this,SLOT(st _disconnected()));
connect(socket,SIGNAL(stateChanged(QAbstractSocket ::SocketState)),this,SLOT(st_stateChange(QAbstract Socket::SocketState)));
}

MainWindow::~MainWindow()
{
delete ui;
delete socket;
}

void MainWindow::on_pushButton_clicked()
{
socket->connectToHost("adsl.by", 80);
}

void MainWindow::st_connected()
{
ui->textEdit->append("Connected at " + QDateTime::currentDateTime().toString("hh:mm:ss dd.MM.yyyy"));
}

void MainWindow::st_disconnected()
{
ui->textEdit->append("Disconnected at " + QDateTime::currentDateTime().toString("hh:mm:ss dd.MM.yyyy"));
}

void MainWindow::st_error(QAbstractSocket::SocketError socketError)
{
ui->textEdit->append("Error " + socket->errorString() + " at " + QDateTime::currentDateTime().toString("hh:mm:ss dd.MM.yyyy"));
}

void MainWindow::st_stateChange(QAbstractSocket::Socket State socketState)
{
ui->textEdit->append("State change to " + statetoString(socketState) + " at " + QDateTime::currentDateTime().toString("hh:mm:ss dd.MM.yyyy"));
}

void MainWindow::on_pushButton_2_clicked()
{
if(socket->isOpen())
socket->disconnectFromHost();
}

QString MainWindow::statetoString(QAbstractSocket::SocketS tate socketState)
{
QString statestring;
switch(socketState)
{
case QAbstractSocket::UnconnectedState : statestring="the socket is not connected";
break;
case QAbstractSocket::HostLookupState : statestring="the socket is performing a host name lookup";
break;
case QAbstractSocket::ConnectingState : statestring="the socket has started establishing a connection";
break;
case QAbstractSocket::ConnectedState : statestring="a connection is established";
break;
case QAbstractSocket::BoundState : statestring="the socket is bound to an address and port";
break;
case QAbstractSocket::ClosingState : statestring="the socket is about to close";
break;
case QAbstractSocket::ListeningState : statestring="listening state";
break;
default: statestring="unknown state";
}
return statestring;
}


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


State change to the socket is performing a host name lookup at 21:42:39 11.03.2011
State change to the socket has started establishing a connection at 21:42:40 11.03.2011
State change to a connection is established at 21:42:40 11.03.2011
Connected at 21:42:40 11.03.2011
Error The remote host closed the connection at 21:42:51 11.03.2011
State change to the socket is about to close at 21:42:51 11.03.2011
State change to the socket is not connected at 21:42:51 11.03.2011
Disconnected at 21:42:51 11.03.2011

sattu
12th March 2011, 06:34
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.

unit
12th March 2011, 09:01
Maybe it's platform specific or network card driver problem.

If you want known interface state try use QNetworkConfiguration

mero
12th March 2011, 14:34
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/qnetworkconfigurationmanager.html#isOnline

sattu
14th March 2011, 04:56
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 ?

unit
14th March 2011, 09:24
Do you can use 4.7.2 ?

sattu
16th March 2011, 06:12
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