Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Qt Networking

[Solved] Qt Networking

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xXDarkShadowXx
    wrote on last edited by
    #1

    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);
    }

    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xXDarkShadowXx
        wrote on last edited by
        #3

        [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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You're welcome !

          No worry, can happen. Happy coding :)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved