Hi All,
I've debugged Tcp communication done by QTcpSocket and I've found a problem already posted in Qt lists but nobody replies to. Every time I connect to a QTcpServer socket, the connection is done 2 times: the first time it's resetted, the second one it was done correctly.
The post I'm referring to is this one:

http://lists.trolltech.com/qt-intere...ad00472-0.html

------------------------------------------------------------------------
Hi There,

Okay, actually I asked Trolltech support but not ticket number for over two
hours and I am in a rush now so probably someone have experienced the same
before.


There is an interesting thing going on TCP channels with both Qt version
4.1.4 and 4.2.0; the test platform was windows xp sp 2 on the client side
(multiple computers).

The implementation is using a QTcpSocket inside a class based on QThread. We
found during testing that the QTcpSocket opens a ghost connection in the
test case (as well as in our real life application).

For example, the ethereal shows the socket as it is first created from the
client to the server with the following bytestream (SYN type):
0000 00 10 f3 06 82 f9 00 0e 7b cb 99 72 08 00 45 00 ........ {..r..E.
0010 00 30 1a 83 40 00 80 06 c4 81 8a f9 02 c7 8a f9 .0..@... ........
0020 03 0a 05 14 07 d4 fd 2b 89 bc 00 00 00 00 70 02 .......+ ......p.
0030 c0 00 13 8b 00 00 02 04 05 b4 01 01 04 02 ........ ......

Then the server found the socket and responds (SYN, ACK):
0000 00 0e 7b cb 99 72 00 10 f3 06 82 f9 08 00 45 00 ..{..r.. ......E.
0010 00 2c 01 10 00 00 ff 06 9e f8 8a f9 03 0a 8a f9 .,...... ........
0020 02 c7 07 d4 05 14 6e 87 f8 82 fd 2b 89 bd 60 12 ......n. ...+..`.
0030 10 00 71 77 00 00 02 04 05 b4 8a f9 ..qw.... ....

Suddently, the client who built the connection drops it (RST):
0000 00 10 f3 06 82 f9 00 0e 7b cb 99 72 08 00 45 00 ........ {..r..E.
0010 00 28 1a 85 00 00 80 06 04 88 8a f9 02 c7 8a f9 .(...... ........
0020 03 0a 05 14 07 d4 fd 2b 89 bd fd 2b 89 bd 50 04 .......+ ...+..P.
0030 00 00 79 62 00 00 ..yb..

Then, the client builds another connection which goes fine. This means a
network overload and also confuses our server so we must find a solution for
the problem. Remarkably, when QTcpSocket was removed and native O/S calls
were used, the ghost socket problem disappeared. The TCP stack setting looks
to be okay and the success of the native calls verify that the problem is
related to the sample code and probably to Qt.

The issue is clearly reproducible and happens every single time after the
first connect, namely if and when connectToHost and waitForConnected used.

Please take a look on the following simple example code so that you may have
a test on your boxes. In the debug output, it is clearly visible that every
other port number is found. It is possible to reproduce the problem even in
step by step mode so this must is not timing issue.

QTcpSocket *Socket;
Socket = new QTcpSocket();
for (int i = 0; i < 10; i++) {

qDebug("Connecting to host...");
Socket->connectToHost(HostName, Port);
if (!Socket->waitForConnected(5000)) {
qDebug(QString("TEST_Connection error:
%1.").arg(Socket->errorString()).toAscii());
} else {
qDebug("Connection established");
Socket->dumpObjectInfo();
qDebug(QString("Port:").arg(Socket->localPort()).toAscii());

msleep(1000);

kone::bmtcp::Message *Message =
MessageFactory.getFactor().createHeartBeatMsg();

qint64 WrittenBytes = Socket->write((const
char*)Message->_buffer, Message->_bufferSize);
Socket->flush();

if (WrittenBytes != Message->_bufferSize) {
qDebug(QString("SendTcpMessage failed. Bytes
written: %1 instead of %2
bytes.").arg(WrittenBytes).arg(Message->_bufferSize).toAscii());
} else {
qDebug("Sending was OK");
}

msleep(1000);

Socket->disconnectFromHost();
Socket->close();
qDebug("Connection to server has been closed.");
}

msleep(1000);
}

It does not even help if I create (new) and delete the QTcpSocket every time
in the for cycle, the observed behavior is the same, two sockets created to
the host server and the first one gets terminated by the QTcpSocket based
client in about 1-2 ms.

Please find attached a small ethereal trace (endig .199 is the client and
.10 is the server).

Bye,
Sandor
------------------------------------------------------------------------

I've the same proble with Qt 4.2.3.

Has somebody experience with this?

Thanks,
Michele De Candia