1

I have watched VoidRealm's Youtube video, C++ Qt 67 - QTCPServer - a basic TCP server application and followed his instructions, however, I can't connect to the QTcpServer I created using Telnet.

My Code:

//myserver.h

#ifndef MYSERVER_H
#define MYSERVER_H

#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>

class MyServer : public QObject
{
    Q_OBJECT
public:
    explicit MyServer(QObject *parent = nullptr);

    void newConnection();
signals:

private:
    QTcpServer *server;
};
#endif // MYSERVER_H

//myserver.cpp

#include "myserver.h"

MyServer::MyServer(QObject *parent)
    : QObject{parent}
{
    server = new QTcpServer(this);

    connect(server,SIGNAL(newConnection()),this,SLOT(newConnection()));

    if(!server->listen(QHostAddress::Any, 1234))
    {
        qDebug() << "Server could not start!";
    }else{
        qDebug() << "Server started!";
    }
}

void MyServer::newConnection()
{
    QTcpSocket *socket = server->nextPendingConnection();

    socket->write("hello client\r\n");
    socket->flush();

    socket->waitForBytesWritten(3000);

    socket->close();
}

Running this code, in the Console screen I get

qt.core.qobject.connect: QObject::connect: No such slot MyServer::newConnection() in ..\First_Server\myserver.cpp:8
Server started!

but when I open a command prompt and do the following steps:

...>telnet
Welcome to Microsoft Telnet Client

Escape Character is 'CTRL+]'

Microsoft Telnet> open 127.0.0.1 1234
Connecting To 127.0.0.1...

It is not connecting. Can anyone tell me what I have done wrong? I am using Qt 6.3.0.

  • Your console msg told you: "no such slot". Looks like you have a typo and forgot `public slots:` section. That's because you use old connection syntax, with the new one, it is no longer a requirement, but still good to have for clarity. https://stackoverflow.com/questions/47196736/are-public-slots-sections-still-necessary-in-qt5 – pptaszni Jul 29 '22 at 07:21
  • As for telnet connection, technically should work, but you are on windows, so ... I assume that in `main` you have `QApplication::exec`? – pptaszni Jul 29 '22 at 07:24

1 Answers1

1

You error message indicates the issue:

qt.core.qobject.connect: QObject::connect: No such slot MyServer::newConnection() in ..\First_Server\myserver.cpp:8 Server started!

You have created the connection between the signal and slot here.

connect(server,SIGNAL(newConnection()),this,SLOT(newConnection()));

First of all, it is not a good idea to have the same slot name as the signal as it becomes confusing. I would call your slot as "handleNewConnection" for example. Secondly, you ought to use the "new" signal-slot syntax. Thirdly, and most importantly, you have not declared your newConnection() method to be a slot. You would need to declare it so in your header file:

private Q_SLOTS:
    void newConnection();

You can also make the slots public if it has to be accessed from outside, it looks like that you would only use it privately here.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Excellent idea to suggest the "new" (well... it was introduced in Qt 5? or late Qt 4?) Signal-slot syntax as it helps avoid all kind of headaches ("Why the hell does it not work...? *5 hours later* Ah, typo in the name for the SLOT() macro.... ARGH) - maybe you could include an example to help get the idea across? – CharonX Jul 29 '22 at 07:56