I've been experiencing a weird problem using QTcpSocket, I've searched the web but can't find anybody else with the same problem.
I have two bare-bones applications, one client and one server, if I run them both on my local machine the client successfully makes all 50 connections to the server.
If I run the server on another computer connected to the network (let's say: 10.1.1.1) it will again connect every time without a problem.
Now, if I change the server's main.cpp to initialise multiple servers under different ports (8001 to 8050), and change the client to make one connection to each server. With both running locally it still works fine.
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
for (int i = 8001; i <= 8050; ++i) {
Server server(i);
return app.exec();
}
}
But, if I put the server on 10.1.1.1 and try it again then I connect to the first 20 without a problem, but then it hangs for a while (anywhere upwards of 5 seconds) before connecting to the next 20, and so on until all are connected.
As a final test, I put an instance of the server on another machine (10.1.1.2) and created 15 instances of a server on each machine and tried to connect to both I experienced the same problem. All 15 of the first machine connected fine, as did the next 5 of the 2nd machine before it hung until eventually connecting to the last 10.
This is on Qt version 4.7.2. And I've experienced this problem on Fedora 17, Windows 7 but not Scientific Linux 6.
The code for the client/server is included below, I removed all the includes to save some vertical space:
Client
client.h
class Client: public QObject {
Q_OBJECT
public:
Client(QObject* parent = 0);
~Client();
void start(QString address, quint16 port);
public slots:
void startTransfer();
void disconnect() { qDebug("disconnect"); }
private:
QTcpSocket client;
};
client.cpp
Client::Client(QObject* parent): QObject(parent) {
connect(&client, SIGNAL(connected()), this, SLOT(startTransfer()));
connect(&client, SIGNAL(disconnected()), this, SLOT(disconnect()));
}
Client::~Client() {
client.close();
}
void Client::start(QString address, quint16 port) {
QHostAddress addr(address);
qDebug(QString("connecting to %1:%2").arg(address, QString::number(port)).toLocal8Bit().constData());
client.connectToHost(addr, port);
}
void Client::startTransfer() {
qDebug("connected");
client.write("Hello, world", 13);
}
main.cpp
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
for (int i = 0; i < 50; i++) {
Client *client = new Client;
client->start("192.168.0.1", 8888);
}
return app.exec();
}
Server
server.h
class Server: public QObject {
Q_OBJECT
public:
Server(int port = 8888, QObject * parent = 0);
~Server();
public slots:
void acceptConnection();
void startRead();
private:
QTcpServer server;
QList<QTcpSocket *> clients;
};
server.cpp
Server::Server(int port, QObject* parent): QObject(parent) {
qDebug(qPrintable("new server instance on port " + QString::number(port)));
connect(&server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
server.listen(QHostAddress::Any, port);
}
Server::~Server() {
server.close();
}
void Server::acceptConnection() {
QTcpSocket *client = server.nextPendingConnection();
clients.append(client);
connect(client, SIGNAL(readyRead()), this, SLOT(startRead()));
}
void Server::startRead() {
QTcpSocket *client = dynamic_cast<QTcpSocket *>(sender());
char buffer[1024] = {0};
client->read(buffer, client->bytesAvailable());
QString response = QString("%2 on server %3").arg(buffer, QString::number(server.serverPort()));
std::cout << qPrintable(response) << std::endl;
client->close();
}
main.cpp
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
Server server;
return app.exec();
}
Related
I am trying to use Qt Remote Objects for creating client server application.
But encountered an issue that says "connectionToSource is null". How do I set the source connection ?
I am creating the server using the .rep file. The code I had used is as follows:
Data Model
// Rep File (DataModel.rep)
class DataModel {
PROP(double data)
}
Server Code
// DataBroadcaster class (DataBroadcaster.h)
// DataBroadcaster class inherits DataModelSimpleSource.h (generated from .rep file)
#include "rep_DataModel_Source.h"
class DataBroadcaster : public DataModelSimpleSource
{
Q_OBJECT
public:
DataBroadcaster(QObject *parent = nullptr);
~DataBroadcaster();
void Initialize();
private:
int QScopedPointer<QRemoteObjectHost> remoteHost;
}
// DataBroadcaster.cpp
DataBroadcaster::DataBroadcaster(QObject *parent): DataModelSimpleSource(parent){}
DataBroadcaster::~DataBroadcaster(){}
void DataBroadcaster::Initialize()
{
remoteHost.reset(new QRemoteObjectHost(QUrl(QStringLiteral("local:mydata"))));
remoteHost->enableRemoting(this, "DataModel");
}
// Server code main function
#include <QCoreApplication>
#include "DataBroadcaster.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
DataBroadcaster server;
server.Initialize();
double count = 0.0;
while(true)
{
server.pushData(count + 0.1);
}
return a.exec();
}
Client Code
// DataModelDataReplica is generated from .rep file used to generate source
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "rep_DataModel_replica.h"
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
// registered as qml type in main
qmlRegisterType<DataModelReplica>("CustomData", 1, 0, "MyData");
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
QML Code
// DisplayPage.qml Added to QML file
import CustomData 1.0
MyData {
id: dataClient
node: Node {
registerUrl: "local:mydata"
}
Button {
id: btn
text: "Server Data"
onClicked: {
// displaying some data from source
btn.text = dataClient.data
}
}
}
Execution:
Server code is running
Client code is running but when trying to get data I get following error message in debugger
**"qt.remoteobjects: connectionToSource is null"**
I am unable to figure out what am I missing.
If anyone has any idea about how to resolve or where to look for please suggest.
I was able to find the problem. In the Server Code main function, the while loop is used which never exits because of which the "exec()" function was never called and Qt event loop never started because of which the Qt Remote object didn't share data. So I changed the following code using the Signal and Slot mechanism to create a non-blocking function. Following is the code change that I made.
// Server Code main function
#include <QCoreApplication>
#include "DataBroadcaster.h"
#include "ServerController.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
DataBroadcaster server;
server.Initialize();
ServerController controller(&server);
return a.exec();
}
// Server Controller
#include <QTimer>
#include "DataBroadcaster.h"
class ServerController : public QObject
{
Q_OBJECT
public:
explicit ServerController(QObject *parent = nullptr, DataBroadcast *server = nullptr);
~ServerController();
public Q_SLOTS:
void SendData();
private:
QTimer timer;
double count = 0.0;
DataBroadcast *m_server = nullptr;
}
ServerController::ServerController(QObject *parent, DataBroadcast *server): QObject(parent), m_server(server)
{
connect(&timer, SIGNAL(timeout()), this, SLOT(SendData()));
timer.start();
}
ServerController::~ServerController()
{
timer.stop();
}
ServerController::SendData()
{
count += 0.1;
if(m_server != nullptr)
{
m_server->pushData(count);
}
}
See EDIT1 at the end of the question for a possible solution - It would be great if somebody could comment on my interpretation, so that I can understand better what's happening
I'm writing a simple TCP client, based on QTcpSocket and managed by a QStateMachine (connect to server -> transmit data -> if disconnected for any reason, reconnect to server).
I noticed that if the connection is shut down on the server side (client is notified with RemoteHostClosedError), after reconnection the QTcpSocket write() method succeeds but no data is transmitted on the wire - nothing is received by the server, and the bytesWritten() signal on the client side does not fire up.
I found in the documentation for error() signal (https://doc.qt.io/qt-5/qabstractsocket.html#error) that
When this signal is emitted, the socket may not be ready for a reconnect attempt. In that case,
attempts to reconnect should be done from the event loop".
I think I'm already ok with that, as the reconnection happens in one of the QStateMachine states, and QStateMachine should have its own event loop according to the QT docs.
Below some simplified code to reproduce the issue (sorry, not so minimal but I could not find a simpler way to show the problem):
testclient.h
#ifndef TESTCLIENT_H
#define TESTCLIENT_H
#include <QObject>
#include <QTcpSocket>
#include <QDebug>
#include <QStateMachine>
class TestClient : public QObject
{
Q_OBJECT
public:
explicit TestClient(QObject *parent = nullptr);
public slots:
void start();
signals:
// FSM events
void fsmEvtConnected();
void fsmEvtError();
private slots:
void onSocketConnected(); // Notify connection to TCP server
void onSocketDisconnected(); // Notify disconnection from TCP server
void onSocketBytesWritten(qint64 bytes); // Notify number of bytes written to TCP server
void onSocketError(QAbstractSocket::SocketError err);
// FSM state enter/exit actions
void onfsmConnectEntered();
void onfsmTransmitEntered();
void onfsmTransmitExited();
private:
// Member variables
QTcpSocket* m_socket; // TCP socket used for communications to server
QStateMachine* m_clientFsm; // FSM defining general client behaviour
private:
void createClientFsm(); // Create client FSM
};
#endif // TESTCLIENT_H
testclient.cpp
#include "testclient.h"
#include <QState>
#include <QThread> // Sleep
//-----------------------------------------------------------------------------
// PUBLIC METHODS
//-----------------------------------------------------------------------------
TestClient::TestClient(QObject *parent) : QObject(parent)
{
m_socket = new QTcpSocket(this);
connect(m_socket, SIGNAL(connected()),this, SLOT(onSocketConnected()));
connect(m_socket, SIGNAL(disconnected()),this, SLOT(onSocketDisconnected()));
connect(m_socket, SIGNAL(bytesWritten(qint64)),this, SLOT(onSocketBytesWritten(qint64)));
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
}
void TestClient::start()
{
createClientFsm();
m_clientFsm->start();
}
//-----------------------------------------------------------------------------
// TCP CONNECTION MANAGEMENT SLOTS
//-----------------------------------------------------------------------------
void TestClient::onSocketConnected()
{
qDebug() << "connected...";
emit fsmEvtConnected();
}
void TestClient::onSocketDisconnected()
{
qDebug() << "disconnected...";
emit fsmEvtError();
}
void TestClient::onSocketBytesWritten(qint64 bytes)
{
qDebug() << bytes << " bytes written...";
}
void TestClient::onSocketError(QAbstractSocket::SocketError err)
{
qDebug() << "socket error " << err;
}
//-----------------------------------------------------------------------------
// FSM MANAGEMENT
//-----------------------------------------------------------------------------
void TestClient::createClientFsm()
{
m_clientFsm = new QStateMachine(this);
// Create states
QState* sConnect = new QState();
QState* sTransmit = new QState();
// Add transitions between states
sConnect->addTransition(this, SIGNAL(fsmEvtConnected()), sTransmit);
sTransmit->addTransition(this, SIGNAL(fsmEvtError()), sConnect);
// Add entry actions to states
connect(sConnect, SIGNAL(entered()), this, SLOT(onfsmConnectEntered()));
connect(sTransmit, SIGNAL(entered()), this, SLOT(onfsmTransmitEntered()));
// Add exit actions to states
connect(sTransmit, SIGNAL(exited()), this, SLOT(onfsmTransmitExited()));
// Create state machine
m_clientFsm->addState(sConnect);
m_clientFsm->addState(sTransmit);
m_clientFsm->setInitialState(sConnect);
}
void TestClient::onfsmConnectEntered()
{
qDebug() << "connecting...";
m_socket->connectToHost("localhost", 11000);
// Wait for connection result
if(!m_socket->waitForConnected(10000))
{
qDebug() << "Error: " << m_socket->errorString();
emit fsmEvtError();
}
}
void TestClient::onfsmTransmitEntered()
{
qDebug() << "sending data...";
m_socket->write("TEST MESSAGE");
}
void TestClient::onfsmTransmitExited()
{
qDebug() << "waiting before reconnection attempt...";
QThread::sleep(2);
}
main.cpp
#include <QCoreApplication>
#include "testclient.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TestClient client(&a);
client.start();
return a.exec();
}
To test, you can just launch netcat (nc -l -p 11000) , then close the nc process after receiving TEST MESSAGE and finally relaunch it again. The second time, TEST MESSAGE is not received, and we don't have the onSocketBytesWritten() printout, see below:
connecting...
connected...
sending data...
12 bytes written... <<<<<<<<<< Correct transmission, event fires up
socket error QAbstractSocket::RemoteHostClosedError
disconnected...
waiting before reconnection attempt...
connecting...
connected...
sending data... <<<<<<<<<< No transmission, event does not fire up, no socket errors!
EDIT1: I found out that if I create the QTcpSocket on connection and destroy it on disconnection, the problem does not happen. Is this the expected/proper way to use sockets?
Wouldn't it be possible instead to create the socket just once and just connect/disconnect? Maybe it is just a matter of flushing or cleaning up in a specific manner, but I could not find it so far.
Here are the modifications that make the code above work on server-side disconnection:
Move socket creation from class constructor to onfsmConnectEntered() - handler for entry in the "Connect" QState:
void TestClient::onfsmConnectEntered()
{
m_socket = new QTcpSocket(this);
connect(m_socket, SIGNAL(connected()),this, SLOT(onSocketConnected()));
connect(m_socket, SIGNAL(disconnected()),this, SLOT(onSocketDisconnected()));
connect(m_socket, SIGNAL(bytesWritten(qint64)),this, SLOT(onSocketBytesWritten(qint64)));
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
qDebug() << "connecting...";
m_socket->connectToHost("localhost", 11000);
// The rest of the method is the same
}
Delete the socket on disconnection, so that it is deallocated and will be created again on reconnection:
void TestClient::onSocketDisconnected()
{
qDebug() << "disconnected...";
m_socket->deleteLater();
m_socket = nullptr;
emit fsmEvtError();
}
Do not use waitForX methods as they block the event loop and prevent them from using that resource as the signals do not do their job correctly or the QStateMachine.
Considering the above, the solution is:
void TestClient::onfsmConnectEntered()
{
m_socket->connectToHost("localhost", 11000);
}
But even so your code has errors since it does not consider other cases such as:
If when you start the client the server is not running, your application will try to connect the error will be launched and nothing else.
If the server fails for a longer time than the 10000 ms timeout set to waitForConnected(), the same will happen as in the previous case.
Then the idea is to try to connect until you are sure of the connection and that can be done through a QTimer with an appropriate period.
testclient.h
#ifndef TESTCLIENT_H
#define TESTCLIENT_H
#include <QObject>
class QTcpSocket;
class QStateMachine;
class QTimer;
#include <QAbstractSocket>
class TestClient : public QObject
{
Q_OBJECT
public:
explicit TestClient(QObject *parent = nullptr);
public slots:
void start();
signals:
// FSM events
void fsmEvtConnected();
void fsmEvtError();
private slots:
void onSocketConnected(); // Notify connection to TCP server
void onSocketDisconnected(); // Notify disconnection from TCP server
void onSocketBytesWritten(qint64 bytes); // Notify number of bytes written to TCP server
void onSocketError(QAbstractSocket::SocketError err);
// FSM state enter/exit actions
void onfsmConnectEntered();
void onfsmTransmitEntered();
private:
// Member variables
QTcpSocket* m_socket; // TCP socket used for communications to server
QStateMachine* m_clientFsm; // FSM defining general client behaviour
QTimer* m_timer;
private:
void createClientFsm(); // Create client FSM
void tryConnect();
};
#endif // TESTCLIENT_H
testclient.cpp
#include "testclient.h"
#include <QState>
#include <QStateMachine>
#include <QTcpSocket>
#include <QThread> // Sleep
#include <QTimer>
//-----------------------------------------------------------------------------
// PUBLIC METHODS
//-----------------------------------------------------------------------------
TestClient::TestClient(QObject *parent) : QObject(parent)
{
m_socket = new QTcpSocket(this);
m_timer = new QTimer(this);
m_timer->setInterval(100);
connect(m_timer, &QTimer::timeout, this, &TestClient::tryConnect);
connect(m_socket, &QAbstractSocket::connected,this, &TestClient::onSocketConnected);
connect(m_socket, &QAbstractSocket::disconnected,this, &TestClient::onSocketDisconnected);
connect(m_socket, &QIODevice::bytesWritten,this, &TestClient::onSocketBytesWritten);
connect(m_socket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), this, &TestClient::onSocketError);
}
void TestClient::start()
{
createClientFsm();
m_clientFsm->start();
}
//-----------------------------------------------------------------------------
// TCP CONNECTION MANAGEMENT SLOTS
//-----------------------------------------------------------------------------
void TestClient::onSocketConnected()
{
m_timer->stop();
qDebug() << "connected...";
emit fsmEvtConnected();
}
void TestClient::onSocketDisconnected()
{
qDebug() << "disconnected...";
emit fsmEvtError();
}
void TestClient::onSocketBytesWritten(qint64 bytes)
{
qDebug() << bytes << " bytes written...";
}
void TestClient::onSocketError(QAbstractSocket::SocketError err)
{
qDebug() << "socket error " << err;
}
//-----------------------------------------------------------------------------
// FSM MANAGEMENT
//-----------------------------------------------------------------------------
void TestClient::createClientFsm()
{
m_clientFsm = new QStateMachine(this);
// Create states
QState* sConnect = new QState();
QState* sTransmit = new QState();
// Add transitions between states
sConnect->addTransition(this, SIGNAL(fsmEvtConnected()), sTransmit);
sTransmit->addTransition(this, SIGNAL(fsmEvtError()), sConnect);
// Add entry actions to states
connect(sConnect, &QAbstractState::entered, this, &TestClient::onfsmConnectEntered);
connect(sTransmit, &QAbstractState::entered, this, &TestClient::onfsmTransmitEntered);
// Create state machine
m_clientFsm->addState(sConnect);
m_clientFsm->addState(sTransmit);
m_clientFsm->setInitialState(sConnect);
}
void TestClient::tryConnect(){
m_socket->connectToHost("localhost", 11000);
}
void TestClient::onfsmConnectEntered()
{
m_timer->start();
}
void TestClient::onfsmTransmitEntered()
{
qDebug() << "sending data...";
m_socket->write("TEST MESSAGE");
}
I'm tring to build a client for a web service. My goal is to send a request to my server every second. I used this library to help me : QHttp
I create a timer that I link with a signal to my QCoreApplication app, and send my request when the timer reach 1 second.
Here's how I do it :
main.cpp
#include "request.h"
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
Request* request = new Request();
request->sendRequestPeriodically(1000, app);
return app.exec();
}
request.h
//lots of include before
class Request
{
Q_OBJECT
public:
Request();
void sendRequestPeriodically (int time, QCoreApplication app);
public slots:
void sendRequest (QCoreApplication app);
};
request.cpp
#include "request.h"
void Request::sendRequest (QCoreApplication app){
using namespace qhttp::client;
QHttpClient client(&app);
QUrl server("http://127.0.0.1:8080/?Clearance");
client.request(qhttp::EHTTP_GET, server, [](QHttpResponse* res) {
// response handler, called when the incoming HTTP headers are ready
// gather HTTP response data (HTTP body)
res->collectData();
// when all data in HTTP response have been read:
res->onEnd([res]() {
// print the XML body of the response
qDebug("\nreceived %d bytes of http body:\n%s\n",
res->collectedData().size(),
res->collectedData().constData()
);
// done! now quit the application
//qApp->quit();
});
});
// set a timeout for the http connection
client.setConnectingTimeOut(10000, []{
qDebug("connecting to HTTP server timed out!");
qApp->quit();
});
}
void Request::sendRequestPeriodically(int time, QCoreApplication app){
QTimer *timer = new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(sendRequest(app)));
timer->start(time); //time specified in ms
}
Request::Request()
{
}
I got these errors :
C:\Qt\5.7\mingw53_32\include\QtCore\qcoreapplication.h:211: erreur : 'QCoreApplication::QCoreApplication(const QCoreApplication&)' is private
Q_DISABLE_COPY(QCoreApplication)
C:\Users\ebelloei\Documents\qhttp\example\client-aircraft\main.cpp:7: erreur : use of deleted function 'QCoreApplication::QCoreApplication(const QCoreApplication&)'
I'm new to Qt, but I assume this comes from the fact I can't passe my QCoreApplication in parameters, is this right ?
First of all, if you need to access the global application instance, you shouldn't be passing it around. Use the qApp macro, or QCoreApplication::instance().
But that's besides the point: the QHttpClient client instance is a local variable, its lifetime managed by the compiler. There's no point to giving it a parent. The client gets destroyed right as sendRequest exits: your sendRequest is effectively a no-op because of that.
You also have errors in your connect statements: you can't pass argument values using the old style connect syntax:
// Wrong: the connection fails and does nothing.
connect(timer, SIGNAL(timeout()), this, SLOT(sendRequest(foo)));
// Qt 5
connect(timer, &QTimer::timeout, this, [=]{ sendRequest(foo); });
// Qt 4
this->foo = foo;
connect(timer, SIGNAL(timeout()), this, SLOT(sendRequest()));
// sendRequest would then use this->foo
Ideally, you should redesign your code to use QNetworkAccessManager. If not, then you must keep the QHttpClient instance alive within main:
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
qhttp::client::QHttpClient client;
auto request = new Request(&client);
request->sendRequestPeriodically(1000);
return app.exec();
}
And then:
class Request : public QObject
{
Q_OBJECT
QTimer m_timer{this};
qhttp::client::QHttpClient *m_client;
public:
explicit Request(qhttp::client::QHttpClient *client);
void sendRequestPeriodically(int time);
void sendRequest();
};
Request::Request(qhttp::client::QHttpClient *client) :
QObject{client},
m_client{client}
{
QObject::connect(&m_timer, &QTimer::timeout, this, &Request::sendRequest);
}
void Request::sendRequestPeriodically(int time) {
timer->start(time);
}
void Request::sendRequest() {
QUrl server("http://127.0.0.1:8080/?Clearance");
m_client->request(qhttp::EHTTP_GET, server, [](QHttpResponse* res) {
...
});
}
You can't pass a copy QCoreApplication object via its copy constructor, you have to pass a pointer to QCoreApplication.
All of "QCoreApplication app" should be replaced by "QCoreApplication *app" and calls to those functions must include a pointer to app not a copy of it.
request.h
//lots of include before
class Request
{
Q_OBJECT
public:
Request();
void sendRequestPeriodically (int time, QCoreApplication *app);
public slots:
void sendRequest (QCoreApplication *app);
};
request.cpp
#include "request.h"
void Request::sendRequest (QCoreApplication *app){
using namespace qhttp::client;
QHttpClient client(app);
QUrl server("http://127.0.0.1:8080/?Clearance");
client.request(qhttp::EHTTP_GET, server, [](QHttpResponse* res) {
// response handler, called when the incoming HTTP headers are ready
// gather HTTP response data (HTTP body)
res->collectData();
// when all data in HTTP response have been read:
res->onEnd([res]() {
// print the XML body of the response
qDebug("\nreceived %d bytes of http body:\n%s\n",
res->collectedData().size(),
res->collectedData().constData()
);
// done! now quit the application
//qApp->quit();
});
});
// set a timeout for the http connection
client.setConnectingTimeOut(10000, []{
qDebug("connecting to HTTP server timed out!");
qApp->quit();
});
}
void Request::sendRequestPeriodically(int time, QCoreApplication app){
QTimer *timer = new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(sendRequest(app)));
timer->start(time); //time specified in ms
}
Request::Request()
{
}
main.cpp
#include "request.h"
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
Request* request = new Request();
request->sendRequestPeriodically(1000, &app);
return app.exec();
}
Edit
As KubaOber said, there are more issues in your code, read his answer
This question already has an answer here:
Bind a QTcpSocket to a specific port
(1 answer)
Closed 2 years ago.
I'm using QTcpSocket to communicate between two applications. one is a c++ program and the other one is a web page written in PHP.
The goal is to send data from my webpage to my c++ program using sockets.
I don't know how to open a connection on a specific port e.g 12345 and listen to it if I got any data or not ..
I've written the following code so far:
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Mysocket = new QSocket(this);
Mysocket->Initialization();
}
MainWindow::~MainWindow()
{
delete ui;
}
QSocket.cpp
#include "qsocket.h"
#define SOCKET_PORT 12345
QSocket::QSocket(QObject *parent) :
QObject(parent)
{
}
void QSocket::Initialization()
{
//connected
socket = new QTcpSocket(this);
socket->connectToHost("localhost",SOCKET_PORT);
connect(socket,SIGNAL(connected()),this,SLOT(connected()));
connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
connect(socket,SIGNAL(bytesWritten(qint64)),this,SLOT(bytesWritten(qint64)));
connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
if(!socket->waitForConnected(1000))
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(0,"Error","Error in Socket Connection",
QMessageBox::Yes|QMessageBox::No);
socket->close();
}
socket80->close();
}
void QSocket::connected()
{
QMessageBox::StandardButton reply;
int PortNumber = socket->localPort();
reply = QMessageBox::question(0,"Connected","Socket Connection is Established",
QMessageBox::Yes|QMessageBox::No);
qDebug()<<"I'm Listening to Port: "<<PortNumber<<"on the local host \n";
}
void QSocket::disconnected()
{
qDebug()<<"Disconnected .... \n";
socket->close();
}
void QSocket::bytesWritten(qint64 bytes)
{
qDebug()<<"We wrote "<<bytes<<" \n";
}
void QSocket::readyRead()
{
qDebug()<<"Reading .... \n";
qDebug()<<socket->bytesAvailable();
qDebug()<<socket->readAll();
}
When I run this code it doesn't open any connection and gives error and it enters into this if statement ..
if(!socket->waitForConnected(1000))
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(0,"Error","Error in Socket Connection",
QMessageBox::Yes|QMessageBox::No);
socket->close();
}
if I changed the port number to 80 it works fine and all the functions work properly.
By reading some posts in SO I've realized that a solution would be to use QTcpServer but I don't have any specific experience in it.
What's the problem in here ?!
P.S.
My Platform Specs.
Ubuntu 13.10
Qt 5.2.1
You can use the following sample server. You can start listening on a port by calling start_listen(int port_no).
#include <QtNetwork>
#include <QMessageBox>
class server : public QTcpServer {
Q_OBJECT
public:
explicit server(QObject *parent = 0);
~server();
QTcpSocket server_socket;
public slots:
void tcpReady();
void tcpError( QAbstractSocket::SocketError error );
bool start_listen(int port_no);
protected:
void incomingConnection( int descriptor );
};
server::server(QObject *parent) : QTcpServer(parent) {
connect( &server_socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(tcpError(QAbstractSocket::SocketError)) );
connect( &server_socket, SIGNAL(readyRead()),
this, SLOT(tcpReady()) );
server_socket.setSocketOption(QAbstractSocket::KeepAliveOption, true );
}
server::~server() {
server_socket.disconnectFromHost();
server_socket.waitForDisconnected();
}
void server::tcpReady() {
QByteArray array = server_socket.read(erver_socket.bytesAvailable());
}
void server::tcpError(QAbstractSocket::SocketError error) {
QMessageBox::warning( (QWidget *)this->parent(), tr("Error"),tr("TCP error: %1").arg( server_socket.errorString() ) );
}
bool server::start_listen(int port_no) {
if( !this->listen( QHostAddress::Any, port_no ) ) {
QMessageBox::warning( (QWidget *)this->parent(), tr("Error!"), tr("Cannot listen to port %1").arg(port_no) );
}
else
return true;
}
void server::incomingConnection(int descriptor) {
if( !server_socket.setSocketDescriptor( descriptor ) ) {
QMessageBox::warning( (QWidget *)this->parent(), tr("Error!"), tr("Socket error!") );
return;
}
}
I am trying to figure out how to correctly use Qt TCP Sockets, as well as multithreading. I want, as a test project in support of something more complex but similar I will try in the future, to do the following: a simple application that either listens for exactly one incoming connection, or connects to a serversocket; next it prints everything it receives over that socket.
The situation where I connect to a serversocket (I use netcat with the -l option for this) works fine: everything netcat sends to my application is printed correctly. However, when I use my program to listen for that one incoming connection (generated by netcat), the connecting succeeds but then I get this runtime error:
QObject::connect: No such signal QThread::readyRead() in ..\QTcpTest\listener.cpp:8
Here is the entire code (don't mind the plain C I/O I use sometimes, I'll remove this later):
[ main.cpp ]
#include "peer.h"
#include <QCoreApplication>
#include <QThread>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static bool askIfServer();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Peer *p;
int rval;
if (askIfServer())
p = new Peer;
else
p = new Peer(false);
rval = a.exec();
delete p;
return rval;
}
static bool askIfServer()
{
bool isServer;
char ibuf[BUFSIZ];
fputs("Choose \'host\' or \'join\': ", stderr);
fgets(ibuf, BUFSIZ, stdin);
ibuf[strlen(ibuf) - 1] = '\0';
fflush(stdin);
if (strcmp(ibuf, "host") == 0)
isServer = true;
else if (strcmp(ibuf, "join") == 0)
isServer = false;
else
{
fputs("Failure.\n", stderr);
exit(-1);
}
return isServer;
}
[ peer.h ]
#ifndef PEER_H
#define PEER_H
#include "listener.h"
#include <QObject>
#include <QThread>
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QTcpServer>
class Peer : public QObject
{
Q_OBJECT
public:
static const quint16 PORT = 5483;
explicit Peer(bool isHost = true, QString hostname = "localhost", QObject *parent = 0);
public slots:
void acceptPeer();
private:
bool _isHost;
QTcpServer *_server;
QTcpSocket *_socket;
QThread *_lThread;
Listener *_listener;
};
#endif // PEER_H
[ peer.cpp ]
#include "peer.h"
Peer::Peer(bool isHost, QString hostname, QObject *parent) :
QObject(parent),
_isHost(isHost)
{
if (_isHost)
{
_server = new QTcpServer;
connect(_server, SIGNAL(newConnection()), this, SLOT(acceptPeer()));
_server->listen(QHostAddress::Any, PORT);
}
else
{
_socket = new QTcpSocket;
connect(_socket, SIGNAL(hostFound()), this, SLOT(acceptPeer()));
_socket->connectToHost(hostname, PORT);
}
}
void Peer::acceptPeer()
{
disconnect(this, SLOT(acceptPeer()));
if (_isHost)
{
_socket = _server->nextPendingConnection();
delete _server;
_server = NULL;
}
_lThread = new QThread;
_listener = new Listener(_socket);
_listener->moveToThread(_lThread);
_lThread->start();
}
[ listener.h ]
#ifndef LISTENER_H
#define LISTENER_H
#include <iostream>
#include <QObject>
#include <QByteArray>
#include <QtNetwork/QTcpSocket>
using std::cout;
class Listener : public QObject
{
Q_OBJECT
public:
explicit Listener(QTcpSocket *socket, QObject *parent = 0);
public slots:
void acceptData();
private:
QTcpSocket *_socket;
};
#endif // LISTENER_H
[ listener.cpp ]
#include "listener.h"
Listener::Listener(QTcpSocket *socket, QObject *parent) :
QObject(parent),
_socket(socket)
{
// I guess this is where it goes wrong
connect(_socket, SIGNAL(readyRead()), this, SLOT(acceptData()), Qt::QueuedConnection);
}
void Listener::acceptData()
{
QByteArray data = _socket->readAll();
cout << data.constData();
}
The thing I do not get is that the error message claims that I try to connect some signal from QThread (and I clearly do not: see listener.cpp:8); even stranger is that this only happens when the socket is created by the QTcpServer instance, not in the other case. What am I missing?
EDIT: SOLVED
See my own answer.
I do not use an extra thread anymore (see comment). The problem was deleting the QTcpServer instance in Peer::acceptPeer(), once a connection was established. I thought that I could delete it because I only need that one socket QTcpServer::nextPendingConnection() returns. Apparently this is wrong, the socket is probably destroyed as well, leaving me with a rogue pointer (I think) crashing the program. Now I only call _server->pauseAccepting(); right after I get the socket, and all works fine.
Here is the change I've made:
void Peer::acceptPeer()
{
disconnect(this, SLOT(acceptPeer()));
if (_isHost)
{
_socket = _server->nextPendingConnection();
_server->pauseAccepting(); // no deletion
}
_listener = new Listener(_socket);
}