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. QTcpServer Server and Client example
Qt 6.11 is out! See what's new in the release blog

QTcpServer Server and Client example

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 3.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.
  • R Offline
    R Offline
    rafael
    wrote on last edited by
    #1

    I have a server application and client application which can communicate with each other when i use QT 5.5.0 built with mingw 32 bit.

    the client and server are both supposed to run on the same machine.

    on my main computer i use QT 5.5.1 built with visual studio 2013 64 bit.

    both machines have firewall disabled and anti virus disabled and not connected to the network.

    both machine use the exact same source code.

    Any suggestions as to why it works on one machine and not the other.

    1 Reply Last reply
    0
    • kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @rafael
      Hello,
      What kind of errors are you getting? As the question is put currently, there could be only (very) wild guesses involved. Do elaborate on the issue, please.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rafael
        wrote on last edited by
        #3

        There is no error. Code compiles. The strange behavior is that the server is not getting the data sent by the client. Client says its connected when i use connect to host. Server doesnt recognize that client is connected.

        I start server and use listen qhostadress any , port 1234. It says server is ready. In my other machine it works fine. i step through the debugger im not getting to the part where client is connected.

        kshegunovK 1 Reply Last reply
        0
        • R rafael

          There is no error. Code compiles. The strange behavior is that the server is not getting the data sent by the client. Client says its connected when i use connect to host. Server doesnt recognize that client is connected.

          I start server and use listen qhostadress any , port 1234. It says server is ready. In my other machine it works fine. i step through the debugger im not getting to the part where client is connected.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @rafael
          Well, I suppose I'd guess some. Do you use threading in your server? Can you break the server on the nextPendingConnection() call and see what it returns? Any errors returned from QTcpSocket::error? What about the socket state?

          Kind regards.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rafael
            wrote on last edited by rafael
            #5

            i've used the code he wrote for the server side

            http://voidrealms.com/index.php?r=tutorial/view&id=256

            i copied the fortune client code.

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

            #include <QMessageBox>

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

            connect(socket, SIGNAL(readyRead()), this, SLOT(readFortune()));
            connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
            

            }

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

            void MainWindow::displayError(QAbstractSocket::SocketError socketError)
            {
            switch (socketError) {
            case QAbstractSocket::RemoteHostClosedError:
            break;
            case QAbstractSocket::HostNotFoundError:
            QMessageBox::information(this, tr("Fortune Client"),
            tr("The host was not found. Please check the "
            "host name and port settings."));
            break;
            case QAbstractSocket::ConnectionRefusedError:
            QMessageBox::information(this, tr("Fortune Client"),
            tr("The connection was refused by the peer. "
            "Make sure the fortune server is running, "
            "and check that the host name and port "
            "settings are correct."));
            break;
            default:
            QMessageBox::information(this, tr("Fortune Client"),
            tr("The following error occurred: %1.")
            .arg(socket->errorString()));
            }

            }

            void MainWindow::on_pushButton_clicked()
            {
            socket->connectToHost("127.0.0.1",1234);

            }

            void MainWindow::readFortune()
            {

            }

            1 Reply Last reply
            0
            • R Offline
              R Offline
              rafael
              wrote on last edited by
              #6

              changed

              void incomingConnection (int handle);

              to

              void incomingConnection(qintptr handle);

              and example program is now working properly.

              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