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. QT client to C server
Qt 6.11 is out! See what's new in the release blog

QT client to C server

Scheduled Pinned Locked Moved General and Desktop
21 Posts 2 Posters 8.1k 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.
  • K Offline
    K Offline
    KupiKupi
    wrote on last edited by
    #9

    Ok, I understood that. But I'm getting this:

    error: no match for 'operator<<' (operand types are '<unresolved overloaded function type>' and 'QAbstractSocket::SocketError')
    qDebug << e;

    I'm coming from visual studio so i tried to include the using namespace std; but with no result

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #10

      Ofcourse you can use the qDebug() in GUI Application. Just include the header file.
      Whatever error you get during the connection handleError Slot will be called and the error will get printed.

      157

      1 Reply Last reply
      0
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #11

        Did you put the parentheses for qDebug? "qDebug":http://qt-project.org/doc/qt-4.8/qdebug.html#basic-use is a function.

        157

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KupiKupi
          wrote on last edited by
          #12

          Ok I fixed it and now it runs. But still nothing happens as I press login. I assume the problem is in the login button slot

          @void Login::on_pushButton_clicked()
          {

          pSocket = new QTcpSocket (this);
          connect (pSocket, SIGNAL(connected()), SLOT(handleConnected()));
          connect (pSocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT (handleError(QAbstractSocket::SocketError)));
          pSocket->connectToHost("127.0.0.1", 9000);
          

          }@

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #13

            Are you sure it is getting called on click ?
            Have you declared on_pushButton_clicked() as a SLOT ?

            157

            1 Reply Last reply
            0
            • K Offline
              K Offline
              KupiKupi
              wrote on last edited by
              #14

              Yes. I already tested it by opening my next dialog directly. And it works. Isn't there a specific in order in the calling of connect and connectToHost? Or maybe should I place the connectToHost in my handleConnected?

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #15

                The order is correct. All the signals must be connected before calling "connectToHost":http://qt-project.org/doc/qt-5.0/qtnetwork/qabstractsocket.html#connectToHost
                The signal connected() gets emitted when the connection is established.

                Apart from these,
                Which OS are you using ?
                Try changing the port number. It may be blocked by the firewall.

                157

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  KupiKupi
                  wrote on last edited by
                  #16

                  I'm on windows with my client. However, the server is on a linux machine. I know i wrote 127.0.0.1 but I did that just to hide the real IP. I tried with ui->lineEdit->text() and ui->lineEdit_2->text().toInt() but still doesn't works.

                  The server is designed to listen to 9000 port. I changed the port and no result. After the client is connected, he should send a message to the server. The server should send back the message 'Hello message'. It's pretty straightforward, but I know it's functional since I got connected from a C client and successfully sent and recieved data. I don't think that the message thing should represent a problem, as I should be able to establish a connection anyway. The server uses select for client acceptance - don't know if it's a relevant info

                  1 Reply Last reply
                  0
                  • p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #17

                    Is it a public IP ? May be it is taking time to connect to the Server.
                    Try waiting for some time as the handleError() should return some error atleast like QAbstractSocket::SocketTimeoutError or QAbstractSocket::HostNotFoundError or another..

                    Another way to test would be to connect to a server using diifferent program.
                    As a quick test i using the python server, for e.g you can start a python HTTP server like this,
                    @
                    python -m SimpleHTTPServer
                    @

                    or

                    @
                    python3.3 -m http.server
                    @

                    By default it listens to port 8000.

                    So you can try connecting the client to python server with port 8000.

                    157

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      KupiKupi
                      wrote on last edited by
                      #18

                      I started a python HTTP server as you said. The server was serving HTTP on 0.0.0.0 port 8000. When I completed the fields of my client with 0.0.0.0 and 8000, i got QAbstractSocket::NetworkError in the bottom side of the screen.

                      If i completed with the IP i knew and the port 8000, still nothing as I press login

                      It is public but I just connected to the server with a pre-made C client via putty. The server works, it transmited the message back to the C client

                      1 Reply Last reply
                      0
                      • p3c0P Offline
                        p3c0P Offline
                        p3c0
                        Moderators
                        wrote on last edited by
                        #19

                        Can you post your complete client code here ?

                        157

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          KupiKupi
                          wrote on last edited by
                          #20

                          Sure.
                          The login.h
                          @#ifndef LOGIN_H
                          #define LOGIN_H

                          #include <QMainWindow>
                          #include <QtNetwork>
                          #include <QTcpServer>
                          #include <QTcpSocket>
                          #include <QMessageBox>
                          #include <QDebug>

                          namespace Ui {
                          class Login;
                          }

                          class Login : public QMainWindow
                          {
                          Q_OBJECT

                          public:
                          explicit Login(QWidget *parent = 0);
                          ~Login();
                          private slots:
                          void on_pushButton_clicked();
                          void on_ExitLogin_clicked();
                          void handleConnected();
                          void handleError(QAbstractSocket::SocketError);
                          private:
                          Ui::Login *ui;
                          QTcpSocket *pSocket;

                          };

                          #endif // LOGIN_H
                          @

                          The login.cpp

                          @#include "login.h"
                          #include "ui_login.h"
                          #include <QMessageBox>
                          #include "menu.h"
                          #include <QtNetwork>
                          #include <QLineEdit>
                          #include <QTcpSocket>
                          #include <QTcpServer>
                          #include <QAbstractSocket>
                          #include <QDebug>

                          using namespace std;

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

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

                          void Login::on_pushButton_clicked()
                          {
                          pSocket = new QTcpSocket (this);
                          connect (pSocket, SIGNAL(connected()), SLOT(handleConnected()));
                          connect (pSocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT (handleError(QAbstractSocket::SocketError)));
                          pSocket->connectToHost(ui->lineEdit->text(),ui->lineEdit_2->text().toInt());

                          }

                          void Login::on_ExitLogin_clicked()
                          {
                          QCoreApplication::instance()->exit();
                          }

                          void Login::handleConnected()
                          {
                          Menu mMenu;
                          mMenu.setModal(true);
                          mMenu.exec();
                          }
                          void Login::handleError(QAbstractSocket::SocketError e)
                          {
                          qDebug() << e;
                          }
                          @

                          The main.cpp
                          @#include "login.h"
                          #include <QApplication>
                          #include <QtNetwork>

                          int main(int argc, char *argv[])
                          {
                          QApplication a(argc, argv);
                          Login w;
                          w.show();
                          return a.exec();
                          }
                          @

                          I have some other classes like football, tennis etc since this should be a sports app. However, I have almost no code in them since I just designed them with drag and drop

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            KupiKupi
                            wrote on last edited by
                            #21

                            Anyway it seemed like the client was functional and I would like to thank you for your interest. However, I have another problem now and I'd be glad if you could help. My app offers a menu after I pass the login part. There are a lot of options in the menu. I want to communicate with the server via the same socket used at the login part. My pSocket is declared in the Login class and I have no idea how to make all my other functions in other classes to use it.

                            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