...
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; QString hostIp; int hostPort; bool connected; signals: void sendSignal(QVector<QString>); void connectedToServer(); 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
Code Block |
---|
#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
Code Block |
---|
#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
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";
}
|