[Solved] Qt Networking



  • For some strange reason, Qt won't allow my client to send bytes to my server unless i connect to it first.

    What i mean by that is, if I'm connected to my server and try sending bytes to it, the application crashes.

    But, if I'm already connected to my server, connect to it again and try sending bytes, it works.

    This problem absolutely blows my mind and if someone could help me fix it, i would really appreciate it.

    Headers :

    mainwindow.h

    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "socket.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    socket.h

    @
    #ifndef SOCKET_H
    #define SOCKET_H

    #include <QObject>
    #include <QTcpSocket>

    class socket : public QObject {
    Q_OBJECT
    public:
    explicit socket(QObject *parent = 0);
    void Connect();
    void sendPackets(QByteArray);
    private:
    QTcpSocket *TCPsocket;

    signals:

    public slots:

    };

    #endif // SOCKET_H
    @

    Source :

    main.cpp

    @
    #include "mainwindow.h"
    #include "socket.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    socket Chat;
    
    Chat.Connect(); // First connection
    Chat.sendPackets("hello"); // It works because i just connected to my server
    
    return a.exec&#40;&#41;;
    

    }
    @

    mainwindow.cpp

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    socket Chat;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked(){
    // Chat.Connect(); if this part is removed, i can't send data to my server even tho i'm already connected to it
    Chat.sendPackets("you"); // doesn't work for some strange reasons
    }
    @

    socket.cpp

    @
    #include "socket.h"

    socket::socket(QObject *parent) : QObject(parent) {

    }

    void socket::Connect(){
    TCPsocket = new QTcpSocket(this);

    TCPsocket->connectToHost("127.0.0.1", 2323);
    TCPsocket->waitForConnected(-1);
    

    }

    void socket::sendPackets(QByteArray Packets){
    TCPsocket->write(Packets);
    }

    @


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    in on_pushButton_clicked() you are creating a new object Chat that is not the same as the one in main.cpp. So if you don't connect first, you'll try to use an uninitialized pointer.



  • [quote author="SGaist" date="1374952628"]Hi and welcome to devnet,

    in on_pushButton_clicked() you are creating a new object Chat that is not the same as the one in main.cpp. So if you don't connect first, you'll try to use an uninitialized pointer.[/quote]

    Wow, it makes so much sense now.

    What a noob mistake.

    Thanks


  • Lifetime Qt Champion

    You're welcome !

    No worry, can happen. Happy coding :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.