1
votes

I have faced with a weird problem. When I try to connect, for example to offline server on 127.0.0.1:8080, socket signal emits "error" as it should but when I connect to 127.0.0.10:8080 or 127.123.4.5:8080 signal does not emit errors. What's the problem and how can I catch failed connection error?

Code 1:

remote_server = new Server(nickname, ip, port);

connect(remote_server, SIGNAL(error(QAbstractSocket::SocketError)),
        SLOT(connection_failed(QAbstractSocket::SocketError)));

remote_server->establish_connection();

Code 2:

master_socket = std::move(
            std::unique_ptr<QTcpSocket>(new QTcpSocket(nullptr))
            );
connect(master_socket.get(), SIGNAL(error(QAbstractSocket::SocketError)),
        SIGNAL(error(QAbstractSocket::SocketError)));

master_socket->connectToHost(ip.c_str(), port);
master_socket->waitForConnected(2000);
1
If your servers listen on loopback interface (which is 127.0.0.1/8) then both your addresses (127.0.0.10, 127.123.4.5) will be up and available. Could you please provide "netstat -nap | grep LISTEN" output to clarify your issue?Murad Tagirov

1 Answers

0
votes

if your tcp socket is in another thread ( seperate from ui thread ) use

connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)) , Qt::DirectConnection);

Qt::DirectConnection