.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpSocket>
#include <QDateTime>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
private slots:
void on_pushButton_clicked();
void st_connected();
void st_disconnected();
void on_pushButton_2_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_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
To copy to clipboard, switch view to plain text mode
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(socket,SIGNAL(connected()),this,SLOT(st_connected()));
connect(socket,SIGNAL(disconnected()),this,SLOT(st_disconnected()));
}
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"));
}
{
ui
->textEdit
->append
("Error " + socket
->errorString
() + " at " + QDateTime::currentDateTime().
toString("hh:mm:ss dd.MM.yyyy"));
}
{
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();
}
{
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;
break;
default: statestring="unknown state";
}
return statestring;
}
#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_connected()));
connect(socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(st_error(QAbstractSocket::SocketError)));
connect(socket,SIGNAL(disconnected()),this,SLOT(st_disconnected()));
connect(socket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(st_stateChange(QAbstractSocket::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::SocketState 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::SocketState 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;
}
To copy to clipboard, switch view to plain text mode
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
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
To copy to clipboard, switch view to plain text mode
Bookmarks