Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
#ifndef TCP_CLIENT_H
#define TCP_CLIENT_H

#include <QxtNetwork/qxtrpcpeer.h>

class TCPClient : public QObject
{
    Q_OBJECT
public:
    TCPClient();
    ~TCPClient();

private:
    QxtRPCPeer *client;

    ///Serverin IP
    QString hostIp;

    ///Serverin portti
    int hostPort;

    ///Ollaanko yhteydessä vai ei
    bool connected;

signals:
    ///Signaalin (viestin) lähettäminen serverille
    void sendSignal(QVector<QString>);

    ///Signaali, kun serveriin yhdistäminen onnistuu
    void connectedToServer();

    ///Signaali, kun serveriin yhdistäminen epäonnistuu
    void serverError();

private slots:
    void connectToServer();
    void connectToServer(QString, int);
    void disconnectFromServer();
    void sendData(QVector<QString>);
    void connectionEstablished();
    void connectionError();
};

#endif // TCP_CLIENT_H

...

Code Block
#include "tcp_server.h"

TCPServer::TCPServer()
{
    server = new QxtRPCPeer();
    serverIp = QHostAddress::Any;
    serverPort = 45123;

    server->attachSlot(QString("signal1"), this, SLOT(receiveSignal()));

    connect(this->server, SIGNAL(clientConnected(quint64)), this, SLOT(newClient(quint64)));
    connect(this->server, SIGNAL(clientDisconnected(quint64)), this, SLOT(deleteClient(quint64)));
}
TCPServer::~TCPServer()
{
    delete server;
}
void TCPServer::startServer()
{
    server->listen(this->serverIp, this->serverPort);

    qDebug() << "server up";
}
void TCPServer::startServer(QHostAddress address, int port)
{
    server->listen(address, port);

    qDebug() << "server up";
}
void TCPServer::stopServer()
{
    server->stopListening();
    clients.empty();

    qDebug() << "server down";
}

void TCPServer::newClient(quint64 clientId)
{
    clients.append(clientId);

    qDebug() << clientId << " connected";
}
void TCPServer::deleteClient(quint64 clientId)
{
    int delClient = clients.indexOf(clientId);
    clients.removeAt(delClient);

    qDebug() << clientId << " disconnected";
}

main.cpp

Code Block

///Pääohjelma