IP address format
-
@koahnig said in IP address format:
Both should work.
You need to give more information on OS and Qt version.
Alsoshowing the part of code where you are using the address to connect would help.
Win10 x64, Qt 5.15.2 (msvc 2019 64 bit)
QString ip = "192.168.010.011"; QTcpSocket *sock = new QTcpSocket; sock->connectToHost(ip, 4321); if(sock->waitForConnected(3000)){ //done }
"waitForConnected" returns "Host not found" error
-
@sitesv
trysock->connectToHost( QHostAddress(ip), 4321);
in the hope it does better parsing.
Edit: but it doesnt
-
You may find that "192.168.010.011" is being treated as an IPv4 address with two components in decimal and the last two in octal due to the leading zero. 010 octal is 8 decimal, and 011 octal is 9 decimal, so the address being sought is 192.168.8.9, which evidently does not exist.
You may also find you can specify the components in hex "0xC0.0xA8.0X0A.0x0B".
Try this:
QHostAddress blah("192.168.010.011"); qDebug() << blah.toString();
-
@ChrisW67 said in IP address format:
You may find that "192.168.010.011" is being treated as an IPv4 address with two components in decimal and the last two in octal due to the leading zero. 010 octal is 8 decimal, and 011 octal is 9 decimal, so the address being sought is 192.168.8.9, which evidently does not exist.
Hi!
I see.
Very strange behaviour... -
@sitesv said in IP address format:
QString ip = "192.168.010.011";
I am not sure if it is a problem, but I've got many years ago problems with parsing "0010" to int.
Problem was very ugly, becauseatoi()
decode0010
as octal, so the parsed value is 8 and not 10! -
@KroMignon said in IP address format:
Problem was very ugly, because
atoi()
decode0010
as octal, so the parsed value is 8 and not 10!This ought never be the case! While the
strtol()
, whichatoi()
calls, accepts abase
of0
to mean interpret string as C-type-string, where0
would indicate octal, all theatoi()
s I have been able to find are defined as:strtol(nptr, NULL, 10);
which should force base 10 always.....
-
@JonB said in IP address format:
This ought never be the case! While the strtol(), which atoi() calls, accepts a base of 0 to mean interpret string as C-type-string, where 0 would indicate octal, all the atoi()s I have been able to find are defined as:
strtol(nptr, NULL, 10);which should force base 10 always.....
Sorry, my bad, it was not
atoi()
butsscanf()
!And, to be clear, this is not a bug, but the way
sscanf()
parse a decimal value:- a string starting with
0
should be parsed as octal - a string starting with
x
should be parsed as hexadecimal
- a string starting with