QTcpSocket states on Windows and Linux
-
Hy,
It seems that QTcpSocket behaves differently on different platforms. I don't understand something here or that's normal behavior ... I wrote this simple test class: (header file content is not pasted here)
@#include "TcpSocketTester.h"
TcpSocketTester::TcpSocketTester(QObject *parent) :
QObject(parent)
{
connect(&_socket, SIGNAL(connected()), this, SLOT(socketConnected()));
connect(&_socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(socketError(QAbstractSocket::SocketError)));
connect(&_socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
connect(&_socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));_socket.connectToHost(QHostAddress("192.168.1.96"), 22154);
}
void TcpSocketTester::socketConnected()
{
qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss.zzz") << ": CONNECTED";
}void TcpSocketTester::socketError(QAbstractSocket::SocketError error)
{
qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss.zzz") << ": SOCKET ERROR " << error;
}void TcpSocketTester::socketDisconnected()
{
qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss.zzz") << ": DISCONNECTED";
}void TcpSocketTester::socketStateChanged(QAbstractSocket::SocketState state)
{
qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss.zzz") << ": STATE " << state;
}@IP address "192.168.1.96" that I try to connect to is not alive!!!
This is output that I get under Ubuntu 10.04:
"08:57:28.663" : STATE QAbstractSocket::HostLookupState
"08:57:28.663" : STATE QAbstractSocket::ConnectingState
"08:57:28.664" : STATE QAbstractSocket::ConnectedState
"08:57:28.664" : CONNECTED
"08:57:49.578" : SOCKET ERROR QAbstractSocket::RemoteHostClosedError
"08:57:49.578" : STATE QAbstractSocket::ClosingState
"08:57:49.578" : STATE QAbstractSocket::UnconnectedState
"08:57:49.578" : DISCONNECTEDThis is output that I get under Windows XP:
"09:13:17.546" : STATE QAbstractSocket::HostLookupState
"09:13:17.546" : STATE QAbstractSocket::ConnectingState
"09:13:59.546" : STATE QAbstractSocket::UnconnectedState
"09:13:59.546" : SOCKET ERROR QAbstractSocket::NetworkErrorSo it seems that under Linux socket signals that it is connected even if IP address is unreachable. Under Windows it seems to work as expected. But I had some cases under windows when it behaved the same way as in Linux, just didn't caught what was the reason.
Maybe someone knows what is happening with these sockets??