PDA

View Full Version : QTcpSocket disconnection problem



erdi
18th February 2006, 14:05
Hi all!!

I'm writing a simple p2p file sending application and I'm of course using QTcpSocket to transfer data between host. I'm using Win XP + QT4.0.1.

When I want to send a file in a non-GUI thread in a blocking way suddenly after sending an amount of data (about 50KB) connection is lost on the sending side. I send data in a small packs, 1KB large, one after another. When I read socket errors after the disconnection it says that on sending side the waitForBytesWritten() have timed out( timeout is set to 10 seconds) and the socket is in a disconnected state. On the side of reciever the written data (as I wrote about 50KB) is there, but there is an error saying that remote host has closed the connection and the socket is in a disconnected state. I also watched debuggigng messeges on the console, but there is nothing about QTcpSocket.

Can anyone help?? What am I doing wrong??

jacek
18th February 2006, 15:44
Without seeing the code, we can only guess. Could you show us the code which sends the data?

erdi
18th February 2006, 16:02
QFile file(filePath + "/" + fileName);
file.open(QIODevice::ReadOnly);
QByteArray byteArray;

while (numberOfPackages > 0){
byteArray = file.read(1024);
numberOfPackages--;

if(sock->state()!=QAbstractSocket::ConnectedState){
printf("Socket disconnected error\n");
return;
}

sock->write(byteArray);


if (sock->bytesToWrite() > 0 && !sock->waitForBytesWritten(10000)){
printf("Socket write error\n");
printf("socket error: %d %s, socket state: %d\n", sock->error(), sock->errorString().toAscii().data(),

sock->state());
return;
}

}

As I said the second if statement is executed after sending about 50KB of data.

jacek
18th February 2006, 16:36
I've tried your code and I was able to send 100kB file without any problems.

Does it wait 10 seconds before printing that "Socket write error" message?

erdi
19th February 2006, 22:50
I've found that the socket worked wrong because it was created by QTcpServer in one thread and I was using it in another one, in a class inheriting the QThread socket. When I send something in the same thread that socket was created in it works fine...

It is still a strange situation form me...