Results 1 to 7 of 7

Thread: TcpSocket in a different thread

  1. #1
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    5

    Default TcpSocket in a different thread

    Hi all,

    I have a MainWindow class, which instantiates a 'Connector' class. The Connector is responsible for making TCP socket connections, and reading data and putting into a shared buffer.

    The data read from the TCP socket is to be processed by some method in the MainWindow class. My concern is, while the main window is processing with the data, further signals from the TCPSocket (data ready) might be lost, as the event loop of 'Connector' is blocked.

    So I want to push the 'reading of data' from the TCPSocket to a different thread so that the event loop is not blocked while processing of data is going on.

    How do I achieve this ? Should I use runnable or qthread ?

    The Connector looks like this :

    Qt Code:
    1. public :
    2. Connector(QString hostname, quint16 port, TemanejoMainWindow* p);
    3. ~Connector();
    4.  
    5. public slots:
    6. void getError(QAbstractSocket::SocketError);
    7. void readData();
    8. void connectToHost();
    To copy to clipboard, switch view to plain text mode 

    The constructor binds the signals and slots as :

    Qt Code:
    1. tcpSocket = new QTcpSocket(this);
    2.  
    3. connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)), this,
    4. SLOT(getError(QAbstractSocket::SocketError)));
    5.  
    6. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
    To copy to clipboard, switch view to plain text mode 

    The method "connectToHost" makes the TCP connection, while readData() is executed everytime data is ready to be read. To me, pushing readData
    method to a new thread makes sense.

    Any help on how to do this ?

    Best regards

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: TcpSocket in a different thread

    The data read from the TCP socket is to be processed by some method in the MainWindow class. My concern is, while the main window is processing with the data, further signals from the TCPSocket (data ready) might be lost, as the event loop of 'Connector' is blocked.
    Why do you think this?

  3. #3
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    5

    Default Re: TcpSocket in a different thread


  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: TcpSocket in a different thread

    The data read from the TCP socket is to be processed by some method in the MainWindow class. My concern is, while the main window is processing with the data, further signals from the TCPSocket (data ready) might be lost, as the event loop of 'Connector' is blocked.
    While the main window is processing the data, further signals are blocked (if you want to call it so), but are not lost. TcpSocket Signals will be processed once the main window is done with processing.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    5

    Default Re: TcpSocket in a different thread

    Ok. So separating the network connector to a different thread doesn't help in anyway, e.g. responsiveness or performance ? (The main GUI thread will be doing a lot of processing alongside.)

    Secondly, if I separate the network component to a different thread, what is the best way to communicate with the main GUI thread ? Via a shared buffer or a queued signal slot connection ?

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: TcpSocket in a different thread

    You can use queued signal, or you can use a combination of both. eg. you emit a signal indicating that data is avaiable in shared buffer.

    IMO it has to depend on what kind of data you want to send, how often you send, do you expect ack back from ui etc (a more of a design decision).

    See if someone else has other suggestions.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. The following user says thank you to Santosh Reddy for this useful post:

    gaganbm (3rd January 2013)

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: TcpSocket in a different thread

    Quote Originally Posted by gaganbm View Post
    Ok. So separating the network connector to a different thread doesn't help in anyway, e.g. responsiveness or performance ?
    It depends. If you process data for a long time and there is much more data coming in then eventually you will fill the input buffer and TCP will block the sending party from sending more data so the communication will become slower. But you can do the processing in a worker thread which will prevent both the GUI and networking from freezing.

    Secondly, if I separate the network component to a different thread, what is the best way to communicate with the main GUI thread ? Via a shared buffer or a queued signal slot connection ?
    Signals and slots. But the best way is to do networking in the main thread and processing in an extra thread.
    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.


  9. The following user says thank you to wysota for this useful post:

    gaganbm (5th January 2013)

Similar Threads

  1. duplicate data on TCPSocket
    By daemonna in forum Qt Programming
    Replies: 9
    Last Post: 4th August 2010, 06:29
  2. TCPSocket/Server
    By TheGrimace in forum Qt Programming
    Replies: 20
    Last Post: 31st August 2009, 22:38
  3. TcpSocket encrypted() signal
    By eleanor in forum Qt Programming
    Replies: 0
    Last Post: 23rd April 2009, 06:44
  4. how to operate tcpsocket in thread again
    By succulent_lily in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2008, 05:57
  5. QProcess+TcpSocket
    By Fastman in forum Qt Programming
    Replies: 14
    Last Post: 13th November 2007, 11:34

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.