jeudi 29 octobre 2020

qTcpSocket server/client crash

I try to do a basic communication between serveur and client in c++ with qTcpSocket, and I don't understand what append. I launch the server first (everything ok), when I run my client after, it directly crashes without print my first qDebug() on the first line of program. If I launch my client first (everything ok), when I run server after, it directly crashes without print my first qDebug.

I show you my code but I don't think this is the problem :

main.cpp (client)

#include <QCoreApplication>
#include <QObject>
#include <QDebug>

#include "client.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug() << "App lancee !!!!!!!!!!!";
    Client c;
    c.doConnect();
    return a.exec();
}

client.h

#include <QDebug>
#include <QHostAddress>
#include <QDebug>

#include "client.h"

Client::Client(QObject *parent) : QObject(parent)
{
    _socket = new QTcpSocket(this);
}

void Client::doConnect()
{
    qDebug() << "DEBUT CONNECT";
    _socket->connectToHost(QHostAddress::LocalHost, 4242);
    _socket->waitForConnected();
    connect(_socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    qDebug() << "FIN CONNECT";
}

void Client::onReadyRead()
{
    QByteArray datas = _socket->readAll();
    qDebug() << "Tous va bien";
    _socket->write(QByteArray("ok !\n"));
}

client.cpp

#include <QDebug>
#include <QHostAddress>
#include <QDebug>

#include "client.h"

Client::Client(QObject *parent) : QObject(parent)
{
    _socket = new QTcpSocket(this);
}

void Client::doConnect()
{
    qDebug() << "DEBUT CONNECT";
    _socket->connectToHost(QHostAddress::LocalHost, 4242);
    _socket->waitForConnected();
    connect(_socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    qDebug() << "FIN CONNECT";
}

void Client::onReadyRead()
{
    QByteArray datas = _socket->readAll();
    qDebug() << "Tous va bien";
    _socket->write(QByteArray("ok !\n"));
}

main.cpp (server)

#include <QCoreApplication>
#include <QTcpServer>
#include <QTcpSocket>
#include <QDebug>
#include <QHostAddress>
#include <QAbstractSocket>
#include <thread>
#include <unistd.h>

#include "server.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Server s;
    return a.exec();
}

server.h

#ifndef SERVER_H
#define SERVER_H

#include <QObject>
#include <QVector>
#include <QTcpServer>
#include <QTcpSocket>

class Server : public QObject
{
    Q_OBJECT

public:
    explicit Server(QObject *parent = 0);
    static void sendNmea(QTcpSocket *clientSocket);


public slots:
    void onNewConnection();
    void onSocketStateChanged(QAbstractSocket::SocketState socketState);
    void onReadyRead();
private:
    QTcpServer  *_server;
    QList<QTcpSocket*>  _sockets;
    QVector<QString> _nmea;
};

#endif // SERVER_H

server.cpp

#include <thread>
#include <unistd.h>

#include "server.h"

Server::Server(QObject *parent) : QObject(parent)
{
    qDebug()<<"TCPCommunicationPort on port 4242\n";
    _server = new QTcpServer(this);
    _server->listen(QHostAddress::Any, 4242);
    connect(_server, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
    qDebug()<<"Waiting connexion:";


//    _nmea << tr("$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,589.4,M,46.9,M,,*47")
//         << tr("$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,544.4,M,46.9,M,,*47")
//         << tr("$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,594.4,M,46.9,M,,*47")
//         << tr("$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,537.4,M,46.9,M,,*47")
//         << tr("$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,556.4,M,46.9,M,,*47")
//         << tr("$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,579.4,M,46.9,M,,*47")
//         << tr("$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,538.4,M,46.9,M,,*47")
//         << tr("$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,554.4,M,46.9,M,,*47")
//         << tr("$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,534.4,M,46.9,M,,*47");
}

void Server::onNewConnection()
{
    QTcpSocket *clientSocket = _server->nextPendingConnection();
    connect(clientSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    connect(clientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onSocketStateChanged(QAbstractSocket::SocketState)));

    QTcpSocket client(clientSocket);
    _sockets.push_back(clientSocket);
    for (QTcpSocket* socket : _sockets) {
        socket->write(QByteArray::fromStdString(clientSocket->peerAddress().toString().toStdString() + " connected to server !\n"));
    }
    qDebug() << "Connected !";
    std::thread xthread(sendNmea, &client);
}

void Server::onReadyRead()
{
    QTcpSocket* sender = static_cast<QTcpSocket*>(QObject::sender());
    QByteArray datas = sender->readAll();
    for (QTcpSocket* socket : _sockets) {
        if (socket != sender)
            socket->write(QByteArray::fromStdString(sender->peerAddress().toString().toStdString() + ": " + datas.toStdString()));
    }
}
void Server::onSocketStateChanged(QAbstractSocket::SocketState socketState)
{
    if (socketState == QAbstractSocket::UnconnectedState)
    {
        QTcpSocket* sender = static_cast<QTcpSocket*>(QObject::sender());
        _sockets.removeOne(sender);
    }
}

void Server::sendNmea(QTcpSocket *clientSocket)
{
    while (42) {
        clientSocket->write(QByteArray::fromStdString(clientSocket->peerAddress().toString().toStdString() + " connected to server !\n"));
        sleep (1);
    }
}

Aucun commentaire:

Enregistrer un commentaire