QTcpServer Server and Client example



  • 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.


  • Qt Champions 2016

    @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.



  • 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.


  • Qt Champions 2016

    @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.



  • 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()
    {

    }



  • changed

    void incomingConnection (int handle);

    to

    void incomingConnection(qintptr handle);

    and example program is now working properly.


Log in to reply
 

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