Originally Posted by
wysota
That's not entirely correct but it is better to stick to such approach if you don't feel confident with Qt. One method triggers a request and another handles the response. You can write a simple dispatcher method that will handle all responses by calling appropriate methods.
I do have interest in learning what QT has to offer. I see some stuff really powerful like having a signal calling a slot each time there's data available to read in the socket instead of running a loop checking for available data all the time... I see that like an interrupt of a microcontroller where a small function is called to handle a state.
Originally Posted by
wysota
It depends how you implement it. The proper approach would be to block the user interface for the duration of executing the request.
In this case I was talking about the TCP Server clients... for instance if I'm checking data on the server using a php get/post on the meanwhile the client in the socket will be able to send another message that will be processed right way. In a security point of view I guess this have a lot of leaks...
So tell me... my http2 code is this:
#include "http2.h"
{
url.append(data);
m_manager = new QNetworkAccessManager(this);
connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(httpdown(QNetworkReply*)));
QNetworkRequest request;
request.
setUrl(QUrl(url
));
request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
m_manager->get(request);
}
{
result->deleteLater();
return data;
}
#include "http2.h"
http2::http2(QObject *parent, QByteArray url, QByteArray data) :
QObject(parent)
{
url.append(data);
m_manager = new QNetworkAccessManager(this);
connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(httpdown(QNetworkReply*)));
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
m_manager->get(request);
}
QByteArray http2::httpdown(QNetworkReply* result)
{
result->deleteLater();
QByteArray data = result->readAll();
return data;
}
To copy to clipboard, switch view to plain text mode
Can I have/move this:
connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(httpdown(QNetworkReply*)));
connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(httpdown(QNetworkReply*)));
To copy to clipboard, switch view to plain text mode
Directly in my tcpserver? and move the httpdown to there as well? in order to use it to run the next code in the program?
Thanks.
Added after 1 8 minutes:
Can I have/move this:
connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(httpdown(QNetworkReply*)));
connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(httpdown(QNetworkReply*)));
To copy to clipboard, switch view to plain text mode
Directly in my tcpserver? and move the httpdown to there as well? in order to use it to run the next code in the program?
Thanks.
Yep, I changed some stuff and seems like I can..
But this is the way I should do it?
tcpserver
::tcpserver(QObject *parent
) :{
[I] [ ... Server Config and Start Stuff ... ][/I]
http2 *h = new http2(this,"http://localhost/test.php","?pag=1");
connect(h->m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(httpdown(QNetworkReply*)));
}
void tcpserver::httpdown(QNetworkReply* result)
{
result->deleteLater();
qDebug() << data;
h->deleteLater();
}
tcpserver::tcpserver(QObject *parent) :
QObject(parent)
{
server = new QTcpServer(this);
[I] [ ... Server Config and Start Stuff ... ][/I]
http2 *h = new http2(this,"http://localhost/test.php","?pag=1");
connect(h->m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(httpdown(QNetworkReply*)));
}
void tcpserver::httpdown(QNetworkReply* result)
{
result->deleteLater();
QByteArray data = result->readAll();
qDebug() << data;
h->deleteLater();
}
To copy to clipboard, switch view to plain text mode
Little problem... It works fine but if I add h->deleteLater(); Do I have to delete it or not? I'm already out of the tcpserver::tcpserver so in my experience with QT "h" is not available anymore...
Do I need to disconnect the signal... because if I want to do more http requests I'll have to connect finished(QNetworkReply*) to another slot... or my program would be going back.
Thanks!
Bookmarks