0

Problem

I am unable to get any further information regarding this error:

QAbstractSocket::UnknownSocketError

The QT QAbstractSocket::SocketError provides only a basic explanation that some error has occurred

An unidentified error occurred.

enum value = -1

Calling QTcpSocket::errorString() provides this:

"Unknown error"

There is one question regarding this here on SO but provides no real solution to solving the issue (and what was suggested I have done)

I have absoltely no idea how to further progress with this error, since each time my client attempts to connect (after calling connectToHost()) I get this error.

Code:

//Server

//...
if (tcpServer.listen(QHostAddress().AnyIPv4, 5000)) {
    qDebug() << "tcpserver started on port : 5000";
}
else{
    qDebug() << "tcpserver failed to start";
}
//...

I also went on to explicitly set the server ip to localhost and port 5000, but without success.

//Client

//...
tcp_con = new QTcpSocket(new QObject());
tcp_con->connectToHost("127.0.0.1", 5000);

switch (tcp_con->error()) {
    //...
    case QAbstractSocket::UnknownSocketError:
    qDebug() << "tcp error UnknownSocketError closed : " << tcp_con->errorString();
    return;
    //...
}

Client debug output:

tcp error UnknownSocketError closed :  "Unknown error"

Any advice?

p.s. I looked for some stacktrace/backtrace option, didn't find anything - if there is, please leave a comment

Community
  • 1
  • 1
CybeX
  • 2,060
  • 3
  • 48
  • 115
  • There are different errors in programming, and some of them has nothing known about the cause. Some errors may be found only by checking the code logic. Also you can put here a [MCVE](http://stackoverflow.com/help/mcve) and we will try to help – Vladimir Bershov Feb 04 '17 at 17:25
  • 2
    Also the part `new QTcpSocket(new QObject());` looks strange – Vladimir Bershov Feb 04 '17 at 17:29
  • @VladimirBershov thanks for the suggestion. I have changed that to `new QTcpSocket(this)` where `this` is a `QDialog`. And `QDialog` indirectly inherits `QObject`. – CybeX Feb 04 '17 at 17:38
  • @VladimirBershov this change resulted in no difference in outcome. This error persists p.s. I too thought this might be a problem but seems like it wasn't – CybeX Feb 04 '17 at 17:41
  • what about a MCVE, and what about [QAbstractSocket::waitForConnected](http://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected) for the `tcp_con` variable – Vladimir Bershov Feb 04 '17 at 17:56
  • 1
    @VladimirBershov indeed, it makes little sense to check `error()` right after `connectToHost()` when nothing has happend yet. I would recommend to connect to the `error()` signal instead and do the error handling in there. – Kevin Krammer Feb 05 '17 at 08:38
  • @KevinKrammer thank you for this suggestion, it makes much more sense. I shall implement this too – CybeX Feb 05 '17 at 12:22

1 Answers1

2

It is wrong to check an error immediately after the connectToHost(), because this is not a complete action, and the errorString() will always return "Unknown error". You have to call the QAbstractSocket::waitForConnected() method like this:

tcp_con->connectToHost("127.0.0.1", 5000);
if (tcp_con->waitForConnected(1000))
    qDebug("Connected!");

Or you can don't call the waitForConnected() and asynchronously wait while the signal connected() will be emitted:

connect(tcp_con, SIGNAL(error(QAbstractSocket::SocketError)),
        this, SLOT(onError(QAbstractSocket::SocketError)));
    connect(tcp_con, SIGNAL(connected()),
        this, SLOT(onConnect()));
//...
void MyClass::onConnect()
{
    qDebug() << "Connected!";
}

void MyClass::onError(QAbstractSocket::SocketError)
{  
    QTcpSocket* sock = (QTcpSocket*)sender();
    qDebug() << "Socket error:" << sock->errorString();
}
Vladimir Bershov
  • 2,701
  • 2
  • 21
  • 51
  • Thank you for this suggestion, i had a hunch that it might be something like this, i will implement it and return with an answer – CybeX Feb 05 '17 at 12:19