This is Homework so just some conceptual tips woudl be nice.
At the Moment im creating a client server project, the client will send a message to the server(this part works great), but then the server sends a message back to the client and this is where I begin to have issues, I have created a separate class in QT called Connection Management in my client that deals with sending a receiving messages, this class is used by my Controller class, which handle the Application logic of all my UI Pages, now when my controller, sends a message to the server a message is sent back, atm I have it as an echo, my ConnectionManagement class handles Incomming messages with the readyRead() signal, the problem here is when someone is using my client and say press the next button, the next button slot will be called, and wait until after execution of NextButtonPressed() or w/e. until the read ready notices, there is something being sent back to it, i tried having a read i can call manually but it doesn't appear to work any suggestions?
a small bit of my code:
ConnectionManager.cpp
ConnectionManager::ConnectionManager(QObject *parent)
: QTcpServer(parent)
{
socket = new QTcpSocket(this);
connect(socket, SIGNAL(readyRead()), this, SLOT(readyread()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnect()));
socket -> connectToHost("192.168.0.119",60000);
}
QString ConnectionManager::getMessage()
{
//sleep(1);
while (inMessage == "")
{
//my attempt at calling read() manually it will read nothing
QByteArray data = socket->readAll();
//just checkign what i am getting my server will immediately return a trivial
//string i.e "carly, santos, andrew, bob"
std::cout << data.data() << "+ 8" << std::endl;
inMessage = data;
}
return inMessage;
}
void ConnectionManager::sendMessage(QString &message)
{
socket -> write(message.toUtf8());
}
//my socket for reading whenever something is sent?
void ConnectionManager::readyread()
{
QByteArray data = socket->readAll();
inMessage = data;
std::cout << data.data() << "+ 1" << std::endl;
}
ok now my controller.cpp
void Controller::openTALogin()
{
//TODO send message to Server
QString buffer = Fill::fillBufferState("Client", "TA");
//std::cout << buffer.toUtf8().data() << std::endl;
myConnection->sendMessage(buffer); // <- this works great =)
//sleep(7);
std::cout << "1234" << std::endl;
QString in = myConnection -> getMessage(); //<- gets nothing -.-
std::cout << in.toUtf8().data() << "+ 12" << std::endl;
//TODO recieve List from server
//ForNow
QLinkedList<QString> *testList = new QLinkedList<QString>();
Fill::fillLinkedList(testList, in);
//testList -> push_front("jabba:5:hut:1");
//testList -> push_front("blind:3:mice:2");
//testList -> push_front("eat:5:day:3");
//testList -> push_front("hello:4:goodbye:4");
delete userWindow;
taLoginWindow = new TALoginWindow(this, testList);
taLoginWindow -> show();
testList -> clear();
delete(testList);
}
the message is received here after this function executes, i remember form using C-Style TCP/IP connection the read would wait until there is data to continue, but it seems for be cool with getting nothing here?
you do your send message and then immediately try to get the responce, this won't work because you need to return to the event loop before anything can be written or read
split up your Controller::openTALogin method into 2
void Controller::openTALogin()
{
//TODO send message to Server
QString buffer = Fill::fillBufferState("Client", "TA");
//std::cout << buffer.toUtf8().data() << std::endl;
myConnection->sendMessage(buffer); // <- this works great =)
}
void Controller::gotResponce(QString in)
{
std::cout << in.toUtf8().data() << "+ 12" << std::endl;
//TODO recieve List from server
//ForNow
QLinkedList<QString> *testList = new QLinkedList<QString>();
Fill::fillLinkedList(testList, in);
//testList -> push_front("jabba:5:hut:1");
//testList -> push_front("blind:3:mice:2");
//testList -> push_front("eat:5:day:3");
//testList -> push_front("hello:4:goodbye:4");
delete userWindow;
taLoginWindow = new TALoginWindow(this, testList);
taLoginWindow -> show();
testList -> clear();
delete(testList);
}
and have the second part called in your readyRead:
void ConnectionManager::readyread()
{
QByteArray data = socket->readAll();
inMessage = data;
std::cout << data.data() << "+ 1" << std::endl;
emit recievedMessage(QString(inMessage)); // using signal here
}
Related
A proxy application which the client side redirect all connections to the server side application.
At the server side, QTcpServer::newConnection() signal is emitted and we connect QTcpSocket::readyRead() signal to our slot which receives the data from client and redirect the data to 3rd party application for handling HTTP/HTTPS requests. readyRead() signal emitted when client sends HTTP request but this does not happen with HTTPS requests.
Tried: Set QTcpServer::proxy() and QTcpSocket::proxy() to QNetworkProxy::NoProxy, use QSslSocket instead of QTcpSocket with ignoreSslErrors().
Code:
Client::Client(QObject *parent, QTcpSocket* clientCon)
: QObject(parent)
{
client = clientCon;
tunnel = new QSslSocket();
if (serverApp) // If application running as server
tunnel->connectToHost("127.0.0.1", 808); // Connect to CCProxy which handle the requests for us
else
{
tunnel->setProxy(QNetworkProxy::NoProxy);
tunnel->ignoreSslErrors();
tunnel->connectToHost(QHostAddress("serverip"), 9090); // Connect to the server side if running as client
}
tunnel->waitForConnected(5000); // 5 seconds timeout
if (tunnel->state() == tunnel->ConnectedState)
{
QObject::connect(client, SIGNAL(readyRead()), this, SLOT(ClientDataReceived()));
QObject::connect(tunnel, SIGNAL(readyRead()), this, SLOT(TunnelDataReceived()));
QObject::connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnected()));
QObject::connect(tunnel, SIGNAL(disconnected()), this, SLOT(ClientDisconnected()));
}
}
Client::~Client()
{
}
bool Client::IsTunnelEstablished()
{
return tunnel->state() == tunnel->ConnectedState ? true : false;
}
QString Client::GetTunnelErrorString()
{
return tunnel->errorString();
}
void Client::ClientDataReceived()
{
QByteArray data = client->readAll();
qDebug() << "Client [" << client->peerAddress().toString().remove(" ") << ":" << client->peerPort() << "] Data Received: " << data.size();
qint64 wr = tunnel->write(data, data.size());
if (wr != data.size())
qDebug() << "QTcpSocket::write() failed!"; // Never happened
data.clear();
tunnel->flush();
}
void Client::TunnelDataReceived()
{
QByteArray data = tunnel->readAll();
qDebug() << "Client Tunnel [" << client->peerAddress().toString().remove(" ") << ":" << client->peerPort() << "] Data Received: " << data.size();
client->write(data);
data.clear();
client->flush();
}
void Client::ClientDisconnected()
{
/*if (tunnel->state() == tunnel->ConnectedState)
tunnel->close();*/
qDebug() << "[" << client->peerAddress().toString().remove(" ") << ":" << client->peerPort() << "] Disconnected";
QObject::disconnect(client, SIGNAL(readyRead()), this, SLOT(ClientDataReceived()));
QObject::disconnect(tunnel, SIGNAL(readyRead()), this, SLOT(TunnelDataReceived()));
QObject::disconnect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnected()));
QObject::disconnect(tunnel, SIGNAL(disconnected()), this, SLOT(ClientDisconnected()));
client->deleteLater();
tunnel->deleteLater();
delete this; // Self delete
}
Server Initialization:
void BaseProtocol::StartServer()
{
server = new QTcpServer();
server->setProxy(QNetworkProxy::NoProxy);
server->listen(QHostAddress("0.0.0.0"), serverApp ? 9090 : 5050); // 9090 for server, 5050 for client
qDebug() << QObject::connect(server, SIGNAL(newConnection()), this, SLOT(ClientConnecting()));
}
void BaseProtocol::ClientConnecting()
{
QTcpSocket *client = server->nextPendingConnection(); // Accept the connection
qDebug() << "Client Connected: " << client->peerAddress().toString() << ":" << client->peerPort();
Client* clientHandler = new Client(this, client);
if (!clientHandler->IsTunnelEstablished())
{
qDebug() << "Tunnel Connection Failed: " << clientHandler->GetTunnelErrorString() << "\n";
client->disconnect();
client->deleteLater();
delete clientHandler;
}
}
As you see the client and server application are the same but start with different ports. The reason why I use a client side application instead of connecting to the server as proxy directly is because I want to encrypt the data between client and server later. (same problem also exist when connecting directly to server side)
This is supposed to be a transparent proxy so there is no need for SSL/TLS at all.
"Internet Options" are used to set the proxy server to "127.0.0.1:5050" at the client side.
I want to receive UDP messages using QT. The code I use (see bellow) works well, but the problem is that it only works for one message: after receiving one message, the code still running, but it seems that it doesn't receive anything anymore.
Indeed, after the first message received, the console display qDebug() << "in !";, meaning that it enters into the function, but never received other UDP messages.
I already check, I sure the message is well sent by the UDP server. So the problem must come from here. Any suggestions?
Launching UDP when the class is created:
App::App(QWidget *parent)
: QMainWindow(parent), imageLabel(new QLabel)
, scrollArea(new QScrollArea)
{
.....
StartUDP();
....
}
The UDP initialization:
void App::StartUDP()
{
socket = new QUdpSocket(this);
bool result = socket->bind(QHostAddress::AnyIPv4, 17);
qDebug() << result;
if(result)
{
qDebug() << "PASS";
}
else
{
qDebug() << "FAIL";
}
readyRead();
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()),Qt::QueuedConnection);
}
The function that what for UDP signal:
void App::readyRead()
{
qDebug() << "in !";
QHostAddress sender;
quint16 port;
while (socket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(socket->pendingDatagramSize());
socket->readDatagram(datagram.data(),datagram.size(),&sender,&port);
qDebug() <<"Message From :: " << sender.toString();
qDebug() <<"Port From :: "<< port;
qDebug() <<"Message :: " << datagram;
file_name = datagram;
m_SystemTrayIcon->showMessage(tr("Message"), tr("Nouveau fichier détecté"));
}
}
I created a library which will handle all HTTP requests and parsing of response data in JSON format. When I called the method that includes get request in my main application (with GUI), I received a memory corruption error. So I added QEventLoop and a timer to wait for the response before proceeding to other processes. I was able to get the response data by calling QNetworkReply.readall(). I needed to get the char* value of the response data so I called the QNetworkReply.data() but it is empty. Why?
Here are the codes I wrote:
Library which handles HTTP requests:
void HttpRequest::getRequest(string param1, string param2)
{
pManager_ = new QNetworkAccessManager(this);
QUrl cUrl(sampleUrl);
QNetworkRequest request(cUrl);
request.setRawHeader(keyHeader.c_str(), param1.c_str());
connect(pManager_, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
connect(pManager_, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError> & )), this,
SLOT(handleSslErrors(QNetworkReply*, const QList<QSslError> & )));
cUrl.addQueryItem("name", QString::fromStdString(param2));
pManager_->get(request); // memory corruption error encountered in main application after calling this
std::cout << "after calling get" << std::endl;
}
void HttpRequest::requestFinished(QNetworkReply *pReply)
{
QByteArray responseData;
std::cout << " request finished" << std::endl;
int responseStatus = pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
std::cout << " status code: " << responseStatus << std::endl;
if(pReply->error())
std::cout << " Error: " << pReply->errorString().toStdString() << std::endl;
else
{
responseData = pReply->readAll();
qDebug() << " Response data: " << responseData;
const char* pResponseData = responseData.data();
qDebug() << "pResponseData: " << pResponseData ;
// parsing here
}
pReply->deleteLater();
pManager_->deleteLater();
}
void HttpRequest::handleSslErrors(QNetworkReply *pReply, const QList<QSslError> & )
{
std::cout << " SSL ERROR" << std::endl;
int responseStatus = pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
}
Main GUI application:
DialogTest::DialogTest(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogTest)
{
// some codes here
if(enabled)
{
HttpRequest::instance()->getInformation(param1, param2); // memory corruption happened here when I called getRequest() method with no event loop
}
// other threads here
}
Here is the code that uses QEventLoop:
void HttpRequest::getRequest(string param1, string param2)
{
QTimer qTimer;
QEventLoop loop;
pManager_ = new QNetworkAccessManager(this);
QUrl cUrl(sampleUrl);
QNetworkRequest request(cUrl);
request.setRawHeader(keyHeader.c_str(), param1.c_str());
connect(&qTimer,SIGNAL(timeout()),&loop, SLOT(quit()));
connect(pManager_, SIGNAL(finished(QNetworkReply*)),&loop, SLOT(quit()));
QNetworkReply *pReply = pManager_->get(request);
qTimer.start(1000);
loop.exec();
int responseCode = pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
std::cout << "status code: " << responseCode << std::endl;
if(pReply->error())
{
std::cout << " Error: " << pReply->errorString().toStdString() << std::endl;
}
else
{
qDebug() << "[HttpRequest] Response data: " << pReply->readAll();
QByteArray response = pReply->readAll(); // it printed this value: "{"count":3,"codes":["x00000A","x00000B","x00000C"]}" which is correct
char* pResponseData = response.data();
qDebug() << "pResponseData: " << pResponseData ; //it printed this: pResponseData:
}
delete pReply;
delete pManager_;
}
I am expecting this response data from a HTTP get command:
"{"count":3,"codes":["x00000A","x00000B","x00000C"]}"
Problem:
What is the best way to implement this? I want to put all HTTP request in a library then call it my main application with GUI. Please note that:
When I use QEventLoop inside the library to wait for the response, QNetworkReply.data() is empty. I need the value of QNetworkReply.data() for parsing.
When I did not use QEventLoop and use signal and slot alone (as shown in the code above), memory corruption occurred in main application after executing HTTP get command. No response data is received.
an advice:
never use a direct delete for a QObject. BAD:
delete pReply;
delete pManager_;
Qt way,GOOD:
pReply->deleteLater();
pManager->deleteLater();
Better: no "new" (dynamic memory)
QNetworkAccessManager Manager_;
...
connect(&Manager_, SIGNAL(finished(QNetworkReply*)),&loop, SLOT(quit()));
..
pReply->deleteLater();
I would need to customize the QModbusDataUnit by sending custom hex via modbus, below my code, I'm trying to customize the post but I do not understand how to do it.
I should send the following values: 0x01,0x08,0x00,0x00,0x00,0x00,0xE0,0x0B
void connessione::clickButton(){
QModbusDataUnit readUnit(QModbusDataUnit::HoldingRegisters, 1, QVector<quint16>({0x01,0x08,0x00,0x00,0x00,0x00,0xE0,0x0B}));
qDebug() << "readUnit" << clientX->state();
if (auto *reply = clientX->sendReadRequest(readUnit, 255)) // client id 255
{
if (!reply->isFinished())
{
// connect the finished signal of the request to your read slot
qDebug() << "connected" << reply->errorString();
connect(reply, &QModbusReply::finished, this, &connessione::readReady);
}
else
{
qDebug() << "Errore" << reply->errorString();
delete reply; // broadcast replies return immediately
}
}
else
{
qDebug() << "Errore" << reply->errorString();
// request error
}
}
but my response is:
D/libmodbusMobile.so(15006): (null):0 ((null)): qt.modbus: (TCP
client) Sent TCP PDU: 0x0300080008 with tId: 0
where set 0x03 ?
this is not right, it's not equal to my QVector send, how to solve ?
Try to test a QModbusDataUnit as follows:
QModbusDataUnit myUnit;
QModbusDataUnit::RegisterType myType;
myType = static_cast<QModbusDataUnit::RegisterType>(0x04);
myUnit = QModbusDataUnit(myType,0x0c,0x03);
cout << hex;
cout << myUnit.registerType() << endl;
cout << myUnit.startAddress() << endl;
Notes:
To use “cout” in a console QT application;
#include <iostream>
using namespace std;
To create a QT Modbus console application include the following in the .pro file;
QT += serialbus widgets
qtConfig(modbus-serialport): QT += serialport
TARGET = modbusserver
I am using Qt 4.8 GCC 32bit on xUbuntu 14.04.
I have the following piece of code, a TCP server that I use in order to get some remote commands and send back some answers - via TCP socket:
struct _MyRequest
{
unsigned long Request;
unsigned long Data;
} __attribute__((packed));
struct _MyAnswer
{
unsigned long Error;
unsigned long Filler;
} __attribute__((packed));
_MyRequest request;
_MyAnswer answer;
RemoteCmdServer::RemoteCmdServer(QObject * parent)
: QTcpServer(parent)
{
qDebug() << "Server started";
listen(QHostAddress("172.31.250.110"), 5004);
connect(this, SIGNAL(newConnection()), this, SLOT(processPendingRequest()));
}
void RemoteCmdServer::processPendingRequest()
{
qDebug() << "Process request";
QTcpSocket * clientConnection = nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
// get the request
int ret = clientConnection->read((char*)&request, sizeof(request));
qDebug() << "READ: " << ret;
if(ret == sizeof(request))
{
// send answer
clientConnection->write((char*)&answer, sizeof(answer));
}
qDebug() << "Disconnecting...";
clientConnection->disconnectFromHost();
}
I am able to write correctly if I comment the if(ret == sizeof(request)) line.
Yet, I can't read from the socket (I always get 0 bytes).
I am 100% sure that the TCP-tool I use to send packets to my app works ok.
Here is the debug output from my app:
Server started
Process request
READ: 0
Disconnecting...
What am I doing wrong? Please advise!
You should wait for the data either in a non-blocking or blocking way. You can use waitForReadyRead to do it in a blocking way.
void RemoteCmdServer::processPendingRequest()
{
qDebug() << "Process request";
QTcpSocket * clientConnection = nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
if (clientConnection->waitForReadyRead())
{
// get the request
QByteArray message = clientConnection->readAll(); // Read message
qDebug() << "Message:" << QString(message);
}
else
{
qDebug().nospace() << "ERROR: could not receive message (" << qPrintable(clientConnection->errorString()) << ")";
}
qDebug() << "Disconnecting...";
clientConnection->disconnectFromHost();
}
You're trying to read data from the new connection without returning to the Qt event loop -- I don't think that's going to work.
After you've accepted the connect with...
QTcpSocket * clientConnection = nextPendingConnection();
You need to connect to its readyRead signal with something like...
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(my_read_slot()));
Where my_read_slot is the member function that will actually perform the read operation.