Results 1 to 5 of 5

Thread: QTcpSocket disconnection problem

  1. #1
    Join Date
    Feb 2006
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QTcpSocket disconnection problem

    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??

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket disconnection problem

    Without seeing the code, we can only guess. Could you show us the code which sends the data?

  3. #3
    Join Date
    Feb 2006
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket disconnection problem

    Qt Code:
    1. QFile file(filePath + "/" + fileName);
    2. file.open(QIODevice::ReadOnly);
    3. QByteArray byteArray;
    4.  
    5. while (numberOfPackages > 0){
    6. byteArray = file.read(1024);
    7. numberOfPackages--;
    8.  
    9. if(sock->state()!=QAbstractSocket::ConnectedState){
    10. printf("Socket disconnected error\n");
    11. return;
    12. }
    13.  
    14. sock->write(byteArray);
    15.  
    16.  
    17. if (sock->bytesToWrite() > 0 && !sock->waitForBytesWritten(10000)){
    18. printf("Socket write error\n");
    19. printf("socket error: %d %s, socket state: %d\n", sock->error(), sock->errorString().toAscii().data(),
    20.  
    21. sock->state());
    22. return;
    23. }
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket disconnection problem

    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?

  5. #5
    Join Date
    Feb 2006
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket disconnection problem

    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...

Similar Threads

  1. Problem in printing the data return /read from QTcpsocket
    By skumar434 in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2009, 20:36
  2. Challenging QTcpSocket reliability problem
    By Raccoon29 in forum Qt Programming
    Replies: 3
    Last Post: 13th January 2009, 11:38
  3. Problem in QTcpSocket
    By navi1084 in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2008, 13:12
  4. Problem with QTcpSocket in QThread
    By Raistlin in forum Qt Programming
    Replies: 8
    Last Post: 6th October 2007, 13:23
  5. problem with QTcpSocket
    By SuperSonik in forum Qt Programming
    Replies: 8
    Last Post: 31st January 2007, 17:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.