You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

QtNetwork modulin laajennos.

QxtRPCPeer-luokka

QxtRPCPeer-luokka tarjoaa mahdollisuuden signaalien lähettämiseen verkoyhteyden välityksellä. Seuraavana esimerkki, jossa esitellään käyttäjän (Client) ja serverin (Server) toiminta, sekä pieni pääohjelma jossa käyttäjä yhdistetään serveriin.

tcp_client.h

#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

tcp_client.cpp

 #include "tcp_client.h"

TCPClient::TCPClient()
{
    client = new QxtRPCPeer();
    hostIp = "127.0.0.1";
    hostPort = 45123;
    connected = false;

    qRegisterMetaTypeStreamOperators< QVector<QString> >("QVector<QString>");
    connect(this->client, SIGNAL(connectedToServer()), this, SLOT(connectionEstablished()));
    connect(this->client, SIGNAL(serverError(QAbstractSocket::SocketError)), this, SLOT(connectionError()));
    client->attachSignal(this, SIGNAL(sendSignal(QVector<QString>)), QString("signal1"));
}
TCPClient::~TCPClient()
{
    delete client;
}

void TCPClient::connectToServer()
{
    client->connect(this->hostIp, this->hostPort);
}
void TCPClient::connectToServer(QString ip, int port)
{
    client->connect(ip, port);
}
void TCPClient::disconnectFromServer()
{
    client->disconnectServer();
    connected = false;
}

void TCPClient::sendData(QVector<QString> data)
{
    emit sendSignal(data);
}

void TCPClient::connectionEstablished()
{
    connected = true;
    qDebug() << "connected";
}
void TCPClient::connectionError()
{
    qDebug() << "connection error";
}

tcp_server.h

#ifndef TCP_SERVER_H
#define TCP_SERVER_H

#include <QxtNetwork/qxtrpcpeer.h>

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

    void startServer();
    void startServer(QHostAddress, int);
    void stopServer();

private:
    QxtRPCPeer *server;
    QHostAddress serverIp;
    int serverPort;
    QList<quint64> clients;

signals:
    void clientConnected(quint64);
    void clientDisconnected(quint64);

private slots:
    void newClient(quint64);
    void deleteClient(quint64);

};

#endif // TCP_SERVER_H

tcp_server.cpp

#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

///Pääohjelma
  • No labels
You must log in to comment.