QTcpServer - different behvaior in debug & release
-
.h file:
#ifndef IPSERVER
#define IPSERVER
#define HEADER_FENSERVEUR#include <QtWidgets>
#include <QtNetwork>
#include "emailoutlook.h"class IPServer : public QWidget
{
Q_OBJECTpublic: IPServer(); void SendToAll(const QString &message); private slots: void NewConnection(); void DataReceived(); void DisconnectClient(); private: EmailOutlook outApp; QTcpServer *server; QList<QTcpSocket *> clients; quint16 sizeMessage;
};
#endif // IPSERVER
.cpp file:
#include "ipserver.h"IPServer::IPServer()
{
outApp = EmailOutlook();
server = new QTcpServer(this);
if (!server->listen(QHostAddress::Any, 50869))
{
qDebug() << "Server cound not start" + server->errorString();
}
else
{
qDebug() << "Server has started on port " + QString::number(server->serverPort()) + ". Clients can now connect.";
connect(server, SIGNAL(newConnection()), this, SLOT(NewConnection()));
}sizeMessage = 0;
}
void IPServer::NewConnection()
{
//SendToAll("New Client connected");QTcpSocket *newClient = server->nextPendingConnection(); clients << newClient; SendToAll("New Client connected"); connect(newClient, SIGNAL(readyRead()), this, SLOT(DataReceived())); connect(newClient, SIGNAL(disconnected()), this, SLOT(DisconnectClient()));
}
void IPServer::DataReceived()
{
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
if (socket == 0)
return;QDataStream in(socket); if (sizeMessage == 0) { if (socket->bytesAvailable() < (int)sizeof(quint16)) return; in >> sizeMessage; } if (socket->bytesAvailable() < sizeMessage) return; QString message; in >> message; SendToAll(message); sizeMessage = 0;
}
void IPServer::DisconnectClient()
{
SendToAll("New client disconnected");QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender()); if (socket == 0) return; clients.removeOne(socket); socket->deleteLater();
}
void IPServer::SendToAll(const QString &message)
{
QByteArray paquet;
QDataStream out(&paquet, QIODevice::WriteOnly);out << (quint16) 0; out << message; out.device()->seek(0); out << (quint16) (paquet.size() - sizeof(quint16)); for (int i = 0; i < clients.size(); i++) { clients[i]->write(paquet); }
}
The IPServer class is initialized from another class:
IPServer *myServer;
myServer = new IPServer(); -
ok.
This code can someone else run and see if it can reproduced on other pc?