Client/Serveur problème envoi de données
Bonjour
J'apprend à me servir des sockets en Qt donc pour cela j'ai créer une application client/serveur.
J'essaye d'envoyer un message à partir du serveur mais ça ne marche pas.
Il n'y a aucun message d'erreur mais le client n'affiche rien, et il ne passe pas dans la fonction donneesRecues car il n'affiche pas
Citation:
QMessageBox::information(this,"Client","Recu");
Serveur
Code:
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 57 58 59
|
#include "Serveur.h"
Serveur::Serveur(QWidget *parent, Qt::WFlags flags): QMainWindow(parent, flags)
{
ui.setupUi(this);
serveur = new QTcpServer(this);
if(serveur->listen(QHostAddress::Any,5289))
{
connect(serveur,SIGNAL(newConnection()),this,SLOT(nouvelleConnexion());
}
connect(ui.infoButton,SIGNAL(clicked()),this,SLOT(envoiEssai()));
}
void Serveur::envoiEssai()
{
QMessageBox::information(this,"Serveur","click");
envoi();
}
void Serveur::nouvelleConnexion()
{
QTcpSocket *nouveauServeur = serveur->nextPendingConnection();
Serveurs << nouveauServeur;
connect(nouveauServeur, SIGNAL(readyRead()),this,SLOT(donneesRecues()));
connect(nouveauServeur,SIGNAL(disconnected()),this,SLOT(deconnexionServeur()));
}
void Serveur::deconnexionServeur()
{
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
if (socket == 0)
return;
Serveurs.removeOne(socket);
socket->deleteLater();
}
//fonction envoi message
void Serveur::envoi()
{
QByteArray paquet;
QDataStream out(&paquet, QIODevice::WriteOnly);
QString messageAEnvoyer="message";
out << (quint16) 0;
out << messageAEnvoyer;
out.device()->seek(0);
out << (quint16) (paquet.size() - sizeof(quint16));
Serveurs[0]->write(paquet);
}
Serveur::~Serveur()
{
serveur->close();
} |
Client
Code:
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 57 58 59 60 61 62 63 64 65 66
|
#include "Client.h"
Client::Client(QWidget *parent, Qt::WFlags flags): QMainWindow(parent, flags)
{
ui.setupUi(this);
socket = new QTcpSocket(this);
timerConnect.start(5000);
connect(socket,SIGNAL(readyRead{}),this,SLOT(donneesRecues()));
connect(&timerConnect,SIGNAL(timeout()),this,SLOT(connecte()));
connect(socket,SIGNAL(disconnected()),this,SLOT(deconnecte()));
connect(socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(erreurSocket(QAbstractSocket::SocketError)));
tailleMessage=0;
}
void Client::connecte()
{
//si socket connecter
if(socket->state()==QAbstractSocket::ConnectedState)
{
//stop timer de connection
timerConnect.stop();
}
else
{
QMessageBox::information(this,"Client","Connection");
//socket->close();
socket->abort();
socket->connectToHost("127.0.0.1",5289);
}
}
void Client::donneesRecues()
{
QMessageBox::information(this,"Client","Recu");
QString messageRecu;
QDataStream in(socket);
if(tailleMessage==0)
{
if(socket->bytesAvailable()<(int)sizeof(quint16))
return;
in>>tailleMessage;
}
if(socket->bytesAvailable()<tailleMessage)
return;
in>>messageRecu;
if(messageRecu=="message")
{
ui.label->setText("c bon");
QMessageBox::information(this,"Client","Ca marche");
}
tailleMessage=0;
}
void Client::deconnecte()
{
//Redemarre le timer de connection
timerConnect.start();
}
Client::~Client()
{
socket->close();
} |