I'm trying to create a TCP server in C++ with QT. I have the code but as soon as I try to connect to the server with SocketTest it says connection refused (most likely due to the server not running).
This is in my tcplistener.h:
#ifndef TCPLISTENER_H
#define TCPLISTENER_H
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QTcpServer>
class tcp_listener : public QTcpServer
{
Q_OBJECT
signals:
public slots:
void newConnectionFromServer()
{
QTcpSocket* newConnection = nextPendingConnection();
qDebug("New connection from %d", newConnection->peerAddress().toIPv4Address());
}
public:
tcp_listener(QObject *parent = 0)
: QTcpServer(parent)
{
listen(QHostAddress::Any, 30000);
connect(this, SIGNAL(newConnection()), SLOT(newConnectionFromServer()));
}
};
#endif // TCPLISTENER_H
This is in my engine.h:
#ifndef ENGINE_H
#define ENGINE_H
#include <QCoreApplication>
#include "tcplistener.h"
class engine
{
public:
void init()
{
qDebug("Initializing AuraEmu...");
tcp_listener list();
}
};
#endif // ENGINE_H
And this is my main.cpp:
#include <QCoreApplication>
#include "engine.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
engine eng = engine();
eng.init();
return a.exec();
}
Anybody know what the problem is?
The other answer, and my comment before that already cover what you did wrong, so I'll just supply the solution.
I've added comments because you said you come from Java and C#, but really, don't try to program C++ like it's Java or C#, because it's not.
class engine
{
public:
void init()
{
qDebug("Initializing AuraEmu...");
tcp_listener *list = new tcp_listener(); // Allocate on the heap instead of the stack.
}
~engine()
{
delete list; // C++ is an UNMANAGED language, there is no garbage collector
}
private:
tcp_listener *list; // This is a pointer to an object.
};
eng.init();
here you create
tcp_listener list();
and after eng.init() finished you detroy it, because
it is object on stack.
Related
I know that this type of problem has been reported before, but the answers weren't helpful to me. This is the code situation:
// main.cpp
#include <QApplication>
#include "game.h"
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
Game game;
game.show();
return app.exec();
}
// game.h
#ifndef GAME_H
#define GAME_H
#include "scene.h"
class Game {
public:
Game();
void show();
private:
GameScene scene;
};
#endif // GAME_H
// scene.h
#ifndef SCENE_H
#define SCENE_H
#include "GraphicsManager.h"
struct GameScene {
static GraphicsManager<MainWindow> window() { return win; }
private:
static GraphicsManager<MainWindow> win;
};
#endif // SCENE_H
// game.cpp
#include "game.h"
Game::Game() {
scene.window().render();
}
void Game::show() {
qDebug() << "crash test before show";
scene.window().view()->show(); // crashes here
qDebug() << "crash test after show";
}
// GraphicsManager.h
#ifndef GRAPHICSMANAGER_H
#define GRAPHICSMANAGER_H
#include <QGraphicsScene>
#include <QGraphicsView> // inherits QWidget
#include <QGraphicsPixmapItem>
#include <QImage>
#include <QBrush>
template <typename T>
struct GraphicsManager {
void render() { static_cast<T*>(this)->render(); }
QGraphicsScene* scene() { return static_cast<T*>(this)->scene(); }
QGraphicsView* view() { return static_cast<T*>(this)->view(); }
};
struct MainWindow :
GraphicsManager<MainWindow> {
void render() {
m_scene = new QGraphicsScene(0, 0, 800, 800);
m_view = new QGraphicsView(m_scene);
m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_view->setFixedSize(800, 800);
}
QGraphicsScene* scene() { return m_scene; }
QGraphicsView* view() { return m_view; }
private:
QGraphicsScene* m_scene; // raw pointer is used here, since
QGraphicsView* m_view; // they both inherit QObject,
};
#endif // GRAPHICSMANAGER_H
Now admittedly I am very new to Qt in general, so it's possible that I don't understand some of its mechanics. But as you can see in the minimal code example above, I want to try to use CRTP when it comes to the graphics interface module, and according to my tests, everything works fine until the QWidget::show() method is called in Game::show(). Right after that call the application crashes.
The crash report message does not provide any useful information or hints as to why this happened. Does anyone have an idea of what I have done wrong?
EDIT: This is the compiler message:
11:05:06: Running steps for project test3...
11:05:06: Starting: "C:\CMake\bin\cmake.exe" --build C:/a_Skola/Programmeringsmetodik/space_invaders_testsite/build-test3-Desktop_Qt_6_4_1_MinGW_64_bit-Debug --target all
ninja: no work to do.
11:05:06: The process "C:\CMake\bin\cmake.exe" exited normally.
11:05:06: Elapsed time: 00:00.
I'm new in QT5 and decided to create simple multithreaded tcp server. I've read examples provided in QT but they have additional functions and complexity which I'm trying to avoid at this point. Here is my code:
server.h
#ifndef SERVER_H
#define SERVER_H
#include <QTcpServer>
#include <QTcpSocket>
#include "threadz.h"
class Server : public QTcpServer
{
Q_OBJECT
public:
explicit Server(QObject *parent = 0);
signals:
public slots:
void newConnection();
private:
QTcpServer *serv;
};
#endif // SERVER_H
threadz.h
#ifndef THREADZ_H
#define THREADZ_H
#include <QThread>
#include <QDebug>
#include "server.h"
class Threadz : public QThread
{
Q_OBJECT
public:
explicit Threadz(QObject *parent = 0);
void run(QTcpServer *serv);
signals:
public slots:
};
#endif // THREADZ_H
server.cpp
#include "server.h"
Server::Server(QObject *parent) :
QTcpServer(parent)
{
serv = new QTcpServer(this);
connect(serv, SIGNAL(newConnection()), this, SLOT(newConnection()));
if(!serv->listen(QHostAddress::Any, 1234))
{
qDebug() << "Error";
}
}
void Server::newConnection()
{
Threadz *thred = new Threadz();
thred->start();
}
threadz.cpp
#include "threadz.h"
Threadz::Threadz(QObject *parent) :
QThread(parent)
{
}
void Threadz::run(QTcpServer *serv)
{
QTcpSocket *socket = serv->nextPendingConnection();
socket->write("Hello!r\n");
socket->waitForBytesWritten();
socket->close();
}
main.cpp
#include <QCoreApplication>
#include "simplehttp.h"
#include "server.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Server gio;
return a.exec();
}
Basically, Im trying to create new thread for every client that connects server. But problem is that when I run this code and connect server with telnet 127.0.0.1 1234 nothing is printed on the screen. No errors but nothing is printed neither. Im using ubuntu 14.04. Sorry if question is silly and thanks in advance.
The QThread::run function is not implemented.
void QThread::run() [virtual protected]
The starting point for the thread. After calling start(), the newly created thread calls this function. The default implementation simply calls exec().
You can reimplement this function to facilitate advanced thread management. Returning from this method will end the execution of the thread.
Instead you implemented void Threadz::run(QTcpServer *serv) but something needs to call this function.
I'm trying to get a web page using Qt and print it on the screen.The problem is it always prints "". It won't make it to the done SLOT. I don't know if there's something wrong with the connect(). The code gets compiled with no errors.
Trying not to use event loops yet.
Here's the code:
net.h
#ifndef NET_H
#define NET_H
#include <QObject>
#include <QtNetwork>
#include <QString>
#include <QDebug>
class net : public QObject
{
Q_OBJECT
public:
explicit net(QObject *parent = 0);
void get_site(QString url);
QString data;
signals:
public slots:
void err(QNetworkReply *);
void done(QNetworkReply *);
private:
};
#endif // NET_H
net.cpp:
#include "net.h"
net::net(QObject *parent) :
QObject(parent)
{
}
void net::get_site(QString url) {
QNetworkAccessManager man;
QNetworkRequest request;
request.setUrl (QUrl(url));
connect (&man , SIGNAL(finished(QNetworkReply*)) ,this, SLOT(done(QNetworkReply*)));
connect (&man , SIGNAL(finished(QNetworkReply*)) ,this, SLOT(err(QNetworkReply *)));
man.get (QNetworkRequest(QUrl(url)));
}
void net::done(QNetworkReply * reply) {
data = QString(reply->readAll ());
}
void net::err(QNetworkReply * reply) {
data = QString(reply->errorString ());
}
And main.cpp:
#include <QCoreApplication>
#include "net.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
net netobj;
netobj.get_site("http://stackoverflow.com");
qDebug() << netobj.data;
return a.exec();
}
There are some major problems in your code. First you should have the QNetworkAccessManager as a class member in .h file :
class net : public QObject
{
...
private:
QNetworkAccessManager nam;
};
Also you should not connect the finished signal to two different slots. Your get_site function should be like :
void net::get_site(QString url) {
QNetworkRequest request;
request.setUrl (QUrl(url));
connect (&man , SIGNAL(finished(QNetworkReply*)) ,this, SLOT(done(QNetworkReply*)));
man.get (QNetworkRequest(QUrl(url)));
}
And you should manage the returned reply in the following way :
void net::done(QNetworkReply * reply) {
if (reply->error() == QNetworkReply::NoError)
{
data = QString(reply->readAll ());
}
else
{
data = QString(reply->errorString ());
}
}
Your QNetworkAccessManager instance goes out of scope at the end of your get_site function. According to Qt docs, one QNetworkAccessManager should be enough for the whole Qt application. Your object should persist outside the scope of that function, since it's likely the response will take longer to receive than it will take for that function to end. Make the QNetworkAccessManager a member of your net class, instead of an automatic variable local to get_site.
Note that you also need to manage the lifetime of the QNetworkReply object, not by using delete, but by using deleteLater() or else you might leak memory.
char totalbuffer[2048] = {0};
this is the initialization of array I want. I had tried to put in main.cpp and my header file,qt creator keep show the error.
this is my .h header file code :
#ifndef QTPROJECT2_H
#define QTPROJECT2_H
#include <QDialog>
#include <QMainWindow>
#include <QtNetwork/QHostAddress>
#include <QLabel>
#include <QPushButton>
#include <QUdpSocket>
#include <QString>
#include <QTcpSocket>
#include <QDataStream>
#include <qstring.h>
#include <QStandardItem>
namespace Ui {
class QtProject2;
}
class QtProject2 : public QDialog
{
Q_OBJECT
public:
explicit QtProject2(QWidget *parent = 0);
~QtProject2();
void start(QString address, quint16 port);
char totalbuffer[2048]={0};
QStandardItemModel* ListModel;
private slots:
void on_pushButton_clicked();
public slots:
void startTransfer();
void disconnected();
void readyRead();
signals:
void socketReady();
private:
Ui::QtProject2 *ui;
QTcpSocket *client;
};
#endif // QTPROJECT2_H
this is my main.cpp coding:
#include "qtproject2.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtProject2 w;
w.show();
char totalbuffer[2048]={0};
return a.exec();
}
this is my project qtproject2.cpp:
#include "qtproject2.h"
#include "ui_qtproject2.h"
QtProject2::QtProject2(QWidget *parent) :
QDialog(parent),
ui(new Ui::QtProject2)
{
ui->setupUi(this);
}
QtProject2::~QtProject2()
{
delete ui;
QtProject2 Client2;
Client2.close();
}
void QtProject2::startTransfer()
{
ui->slabel->setText("Connected!");
}
void QtProject2::disconnected()
{
ui->slabel->setText("Disconnected!");
}
void QtProject2::readyRead()
{
client->waitForBytesWritten(1000);
client->waitForReadyRead(3000);
char CustomerData_MacAdd[][18]={"14:13:12:11:67:11","52:22:22:22:22:22", "14:22:44:55:22" };
char CustomerData_Username[][10]={"Robert","Alex","Ivan"};
QByteArray buffer1 = client->readLine();
char *temp = buffer1.data();
char buffer[1024]={0};
if (strncmp(temp,"*CLIENT",6)==0)
{
int j;
for(j=9;j<26;j++)
{ buffer[j-9]=temp[j];}
}else if(strncmp(temp,"*ALERT",5)==0)
{
int j;
for(j=8;j<25;j++)
{buffer[j-8]=temp[j];}
}
//char totalbuffer[2048]={0};
int k;
for (k=0;k<3;k++){
if (strncmp(buffer,CustomerData_MacAdd[k],16)==0){
strncat(totalbuffer,"User is in Queue : ",19);
strncat (totalbuffer,CustomerData_Username[k],size_t(CustomerData_Username[k]));
strncat(totalbuffer,"\n",2);
ui->label->setText(totalbuffer);
}}
ui->slabel->setText(buffer1);
}
void QtProject2::on_pushButton_clicked()
{
client = new QTcpSocket(this);
connect(client, SIGNAL(connected()),this, SLOT(startTransfer()));
connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(client, SIGNAL(readyRead()), this, SLOT(readyRead()));
/*Connect to User Define Port Address*/
client->connectToHost("127.0.0.1", 1234);
}
Focus for the char totalbuffer[2048]={0}; , i really no idea where i should put it.
I had searched with google and stackoverflow, all the method is using class and call in the .cpp file. but my one is GUI application,that's different with console.
What should i do? I really need a help. Appreciate the helps and thanks you guys~
EDITED***
this is the error shows.
error assigning to an array from an initializer list
I think the initialize list can't put in the header file and main.cpp so how i going to initialize an array? like C++ visual studio by using public variable....
You can use the memset or bzero functions
memset((void*)&totalbuffer, 0, sizeof(totalbuffer));
or
bzero((void*)&totalbuffer, sizeof(totalbuffer));
This is not related to Qt, but basic C++.
Class members are initialized in the constructor. You can just use std::fill there.
I would like to know how to call the function Do_Download() from the SocketTest class outisde the main() function.
The first cTest.Do_Download() does work, but when I call the test() function, the csTest.Do_Download() does not work.
So it looks like I can only acces SocketTest from inside the main() function, and not from any other function.
Does somebody know how this can be solved?
Thanks!
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
SocketTest cTest;
cTest.Do_Download();
return a.exec();
}
void test()
{
qDebug() << "test main functie";
SocketTest csTest;
csTest.Do_Download();
}
SocketTest.h:
#ifndef SOCKETTEST_H
#define SOCKETTEST_H
#include <QObject>
#include <QTcpSocket>
#include <QDebug>
#include <QHttp>
#include <QFile>
#include <QString>
class SocketTest : public QObject
{
Q_OBJECT
public:
explicit SocketTest(QObject *parent = 0);
void Do_Download();
signals:
public slots:
void stateChanged ( int state );
void responseHeaderReceived ( const QHttpResponseHeader & resp );
void requestFinished ( int id, bool error );
private:
QTcpSocket *socket;
QHttp *http;
QHttp *http2;
};
#endif // SOCKETTEST_H
If your DoDownload function is doing anything asynchronously (likely, when dealing with the Qt networking classes), the SocketTest you are creating in test() is being destroyed before it can act on any return value.
It works in main() because the event loop starts and the SocketTest instance hangs around.