Connecting a QWebSocket to a given IPv6 address (no URL)
-
Hello,
How exactly should I connect a QWebSocket to a server that is not behind a particual URL (so no QUrl to open), but merely an IPv6 address?
Think of it this way. I run a Qt program, and as a result a QWebSocketServer is listening. A friend wants to connect their QWebSocket to my QWebSocketServer. I give him my IPv6 address. How exactly is he supposed to connect his QWebSocket at this point?
I feel like the answer is about using
QWebSocket::open(const QNetworkRequest&)
but how exactly do I set an IPv6 address in a QNetworkRequest? -
Hello,
How exactly should I connect a QWebSocket to a server that is not behind a particual URL (so no QUrl to open), but merely an IPv6 address?
Think of it this way. I run a Qt program, and as a result a QWebSocketServer is listening. A friend wants to connect their QWebSocket to my QWebSocketServer. I give him my IPv6 address. How exactly is he supposed to connect his QWebSocket at this point?
I feel like the answer is about using
QWebSocket::open(const QNetworkRequest&)
but how exactly do I set an IPv6 address in a QNetworkRequest? -
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 itsnewConnection
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?
-
Why the brackets for the ip?
-
Why the brackets for the ip?
-
I would go with https://doc.qt.io/qt-6/qhostinfo.html#from Name to see if you pass a valid ip
-
I would go with https://doc.qt.io/qt-6/qhostinfo.html#from Name to see if you pass a valid ip
@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.
-
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.
-
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.
-
What did you do that made things work ? It might be helpful to other people :-)
-
@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!
-