Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWebSocket vs QTcpSocket
Forum Updated to NodeBB v4.3 + New Features

QWebSocket vs QTcpSocket

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    enne9
    wrote on last edited by
    #1

    Hello,
    I wrote an application similar to a chat. On the server, I keep track of the connection opened with the clients by collecting the associated QTcpSocket.
    Once a client sends a message, the server broadcasts it to all the other clients by retrieving the stored QTcpSocket.

    In Qt documentation, I found out about the possibility of using QWebSocket. What should be the advantage of using it over a general QTcpSocket?

    Maybe it is more a general question other than Qt specific, but I wasn't able to find an answer online, with articles only writing about normal TCP connection being unidirectional through a request/response mechanism. But this doesn't seem true to me since I can just keep the connection open and then send a message from the server to the client whenever I want (without any need for a previous request or polling mechanism).

    Is anyone able to help me? Thanks

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      They're simply two different protocols:
      QTcpSocket: tcp/ip communication
      QWebSocket: websocket communication

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • E Offline
        E Offline
        enne9
        wrote on last edited by
        #3

        Yes, I understand, but I'm not able to get the advantages/disadvantages in the suggested case of a chat application since I find examples in Qt using both of them.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          QWebSocket is more high-level - you don't have to care about the packets and add an own layer to know when a telegram ends since it's already handled by the protocol.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • E Offline
            E Offline
            enne9
            wrote on last edited by
            #5

            Thank you, this makes sense.
            When I was using QTcpSocket in fact I had to manually handle the distinction between the type of content (Json, QByteArray) when writing the slot for the readyRead(). And in the case of binary data, I was using a custom protocol to tell the number of bytes expected and continue reading (calling again the onReadyRead()) until everything was received.

            Instead, looking at the methods provided for the QWebSocket class, it seems that there are separate signals for string and binary content and binary data can be dealt with all at once.
            Am I right?

            Now that I look at it, probably mine was a poor design choice.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved