1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
class Temperature : public QMainWindow
{
public:
Temperature(QWidget* parent =0, Qt::WindowFlags flags =0);
private:
QTcpServer* serveur;
QLabel* labelStatus;
double tempActuelle;
double tempDesiree;
private slots:
void nouvelleConnexion();
};
Temperature::Temperature(QWidget* parent, Qt::WindowFlags flags):
QMainWindow(parent, flags)
{
serveur = new QTcpServer(this);
setFixedSize(300,200);
if (!serveur->listen(QHostAddress::Any, 2156))
{
QMessageBox::critical(this, tr("Simulateur Temperature"), tr("Impossible de démarrer le serveur: %1.").arg(serveur->errorString()));
close();
return;
}
labelStatus = new QLabel(tr("Simulation de temperature"), this);
statusBar()->addWidget(labelStatus, 1);
QMessageBox::information(this, tr("Simulateur Temperature"), tr("Serveur démarré"));
QString str;
labelStatus->setText(tr("Serveur démarré sur le port : ")+str.setNum(serveur->serverPort()));
connect(serveur, SIGNAL(newConnection()), this, SLOT(nouvelleConnexion()));
}
void Temperature::nouvelleConnexion()
{
QMessageBox::information(this, tr("Simulateur Temperature"), tr("Nouvelle connexion"));
QTcpSocket* clientConnection = serveur->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
QByteArray block;
QDataStream sflux(&block, QIODevice::WriteOnly);
//envoie temperature actuelle, temperature désirée
sflux << tempActuelle << "," << tempDesiree;
clientConnection->write(block);
//a enlever après
clientConnection->disconnectFromHost();
} |