Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. Connecting a QWebSocket to a given IPv6 address (no URL)
Forum Updated to NodeBB v4.3 + New Features

Connecting a QWebSocket to a given IPv6 address (no URL)

Scheduled Pinned Locked Moved Solved QtWebEngine
13 Posts 7 Posters 502 Views 3 Watching
  • 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.
  • piervalliP Offline
    piervalliP Offline
    piervalli
    wrote last edited by piervalli
    #3

    Ipv4 or IPv6 you need an ip 'ws:// {ip}:{port}' or if you need open ssl use 'wss://' you can use Qurl.

    1 Reply Last reply
    1
    • P Offline
      P Offline
      Pippin
      wrote last edited by Pippin
      #4

      Thanks guys!

      Things are not doing well but I'm hopeful this will change soon. This is the relevant piece of code:

      const auto path = "ws://[" + ip.toString() + "]:54321";
      const auto url = QUrl(path);
      
      socketToOnlineServer.open(url);
      

      When I run this code, the IPv6 given is mine (I got it here) and my other application (on the same PC) is running a listening QWebSocketServer. The other relevant piece of code is:

      server.listen(QHostAddress::Any, 54'321);
      

      That server never gets to emit the newConnection signal somehow. Yes, I made sure to connect its newConnection signal to a slot of mine. My OS is Ubuntu LTS 24.04, and just to be sure, I deactivated my firewall by running:

      sudo ufw disable
      

      But unfortunately, that didn't change anything. My listening QWebSocketServer never gets to meet my connecting QWebSocket from the other application.

      Would you see any reason why this didn't work?

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

        Why the brackets for the ip?

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

        P 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          Why the brackets for the ip?

          P Offline
          P Offline
          Pippin
          wrote last edited by
          #6

          @Christian-Ehrlicher From the link provided by @JonB

          literal IPv6 addresses should be put inside square brackets

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

            I would go with https://doc.qt.io/qt-6/qhostinfo.html#from Name to see if you pass a valid ip

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

            P 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              I would go with https://doc.qt.io/qt-6/qhostinfo.html#from Name to see if you pass a valid ip

              P Offline
              P Offline
              Pippin
              wrote last edited by Pippin
              #8

              @Christian-Ehrlicher Indeed I don't pass a valid IP...

              auto info = QHostInfo::fromName(/* my Ipv6 address here */);
              std::cout << info.errorString().toStdString() << std::endl;
              std::cout << info.lookupId() << std::endl;
              

              gives:

              Unknown error
              -1
              

              I'm not sure what's wrong though. I'm just passing my regular IPv6 address, which I got from https://whatismyip.org/

              Nothing changes if I provide my IPv4 address instead. I use Proton VPN but then again, nothing changes if I turn it off and edit the code with my real IPv6 address.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                danish777
                wrote last edited by SGaist
                #9

                To connect a QWebSocket client to a server using an IPv6 address, your friend needs to format the address properly in a WebSocket-compatible URL. IPv6 addresses must be enclosed in square brackets. Here's an example:

                QWebSocket socket;
                QUrl url(QStringLiteral("ws://[2001:0db8:85a3::8a2e:0370:7334]:12345"));
                socket.open(url);
                
                

                The ws:// scheme is used for unencrypted WebSocket.

                Use wss:// if the server uses SSL.

                Replace 12345 with the port your QWebSocketServer is listening on.

                If you're using

                QWebSocket::open(const QNetworkRequest&)
                

                , just wrap the QUrl like this:

                QNetworkRequest request(QUrl(QStringLiteral("ws://[ipv6-address]:port")));
                socket.open(request);
                
                

                Also make sure:

                Both client and server have proper IPv6 connectivity.

                Firewall or router isn’t blocking the IPv6 port.

                Let me know if you hit issues with specific address formats or port bindings.

                1 Reply Last reply
                1
                • GrecKoG Offline
                  GrecKoG Offline
                  GrecKo
                  Qt Champions 2018
                  wrote last edited by
                  #10

                  @Pippin you most likely need to open a port on your internet router.

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    Pippin
                    wrote last edited by
                    #11

                    It's all good now, thank you guys. I can send and receive messages with a QWebSocket and a QWebSocketServer in NonSecureMode. I was curious to see if everything would keep working if I set the QWebSocketServer to SecureMode, and change all request URLs to "wss://[ipv6]:port" on the client side, but the socket gets disconnected immediately.

                    I don't know anything about certificates and SSL configuration, so that's something I'll have to look up eventually.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote last edited by
                      #12

                      What did you do that made things work ? It might be helpful to other people :-)

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      P 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        What did you do that made things work ? It might be helpful to other people :-)

                        P Offline
                        P Offline
                        Pippin
                        wrote last edited by Pippin
                        #13

                        @SGaist said in Connecting a QWebSocket to a given IPv6 address (no URL):

                        What did you do that made things work ? It might be helpful to other people :-)

                        There were two things:

                        • The QWebSocketServer was set to SecureMode, while on the client's side, the request URLs were all "ws://", so changing the QWebSocketServer to NonSecureMode helped a lot.
                        • When the lookup of QHostInfo is successful, the lookupId of is set to -1, and the errorString is then "Unknown error". So there really was no problem with this to begin with!
                        1 Reply Last reply
                        1
                        • P Pippin has marked this topic as solved

                        • Login

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