Results 1 to 8 of 8

Thread: Bytes are lost when sending data from C socket to QTcpSocket

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    craigt Guest

    Default Re: Bytes are lost when sending data from C socket to QTcpSocket

    Ok fantastic!

    Using setParent of the socket in ArdHandler seems to have sorted it out. I've run it a couple time for hundreds of cycles with no missing data. It makes sense actually. The socket is initialised from the handler's constructor which is in turn called from MainWindow so its essentially MainWindow code. I didn't even stop to consider that.

    I think it is safer to not make the socket a member variable of the handler. If you want, then just keep a pointer to the socket as a member variable of the handler.
    So would I then declare/intialise the socket in MainWindow, also move it to the QThread and provide a pointer to the handler?

    Several times I've seen it recommended that the socket be declared and initialised in the run() of the QThread which will safely make it belong to the thread. It becomes a little hard to get access to it that way though.

    You can do whatever you want as long as you make sure that a little-endian machine can read data sent by a big-endian machine and vice versa. And that different data alignment/structure packing on different platforms doesn't influence the data sent over the wire.
    I am aware of this. I've left it out for now as I'm working on very similar platforms and testing the 2 programs mainly on the same machine. Some the data is coming through correctly so the endianess must be matched. It isn't going to change mid send as far as I understand, its normally fixed to a given platform. I will add this to the code once I get a basic prototype going.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Bytes are lost when sending data from C socket to QTcpSocket

    Quote Originally Posted by craigt View Post
    Using setParent of the socket in ArdHandler seems to have sorted it out. I've run it a couple time for hundreds of cycles with no missing data. It makes sense actually. The socket is initialised from the handler's constructor which is in turn called from MainWindow so its essentially MainWindow code. I didn't even stop to consider that.
    It's simpler than that. When you use moveToThread(), the object is moved together will all its children. However since the socket is not a child of the handler, it is kept in the thread that created the ArdHandler object.

    So would I then declare/intialise the socket in MainWindow, also move it to the QThread and provide a pointer to the handler?
    No, you can create the socket in constructor of the handler object and set the handler object as parent of the socket. Then when you move the handler, the socket will move together with it.

    Several times I've seen it recommended that the socket be declared and initialised in the run() of the QThread which will safely make it belong to the thread. It becomes a little hard to get access to it that way though.
    No, you can still a pointer to the socket in the containing object. However in your situation you don't have to subclass QThread at all.


    I am aware of this. I've left it out for now as I'm working on very similar platforms and testing the 2 programs mainly on the same machine. Some the data is coming through correctly so the endianess must be matched.
    If you use the same platform then they will match, however you should think about it already because later you will just forget about it and might run into trouble later.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QTcpSocket get disconnected when server is sending huge data
    By KernelCoder in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2011, 08:45
  2. Replies: 8
    Last Post: 18th December 2010, 15:07
  3. Replies: 1
    Last Post: 18th October 2010, 16:07
  4. Events lost in IPC through local socket in Windows
    By yogesh in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2009, 11:38
  5. socket read/write bytes
    By nowire75 in forum Newbie
    Replies: 3
    Last Post: 4th July 2007, 23:12

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.