File tree Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Original file line number Diff line number Diff line change 1
1
.DS_Store
2
2
* build-PolyChat *
3
+ * build *
3
4
4
5
# C++ objects and libs
5
6
* .slo
Original file line number Diff line number Diff line change @@ -17,18 +17,37 @@ static QHostAddress getIPAddress()
17
17
{
18
18
QHostAddress ipAddress;
19
19
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses ();
20
- for (int i = 0 ; i < ipAddressesList.size (); ++i)
20
+
21
+ // 优先寻找以192开头的IPv4地址
22
+ for (const QHostAddress &address : ipAddressesList)
21
23
{
22
- if (ipAddressesList.at (i) != QHostAddress::LocalHost
23
- && ipAddressesList.at (i).toIPv4Address ())
24
+ if (address != QHostAddress::LocalHost && address.toIPv4Address ())
24
25
{
25
- ipAddress = ipAddressesList.at (i);
26
- break ;
26
+ QString ipString = address.toString ();
27
+ if (ipString.startsWith (" 192" ))
28
+ {
29
+ ipAddress = address;
30
+ break ;
31
+ }
32
+ }
33
+ }
34
+
35
+ // 如果没有找到以192开头的地址,再考虑其他IPv4地址
36
+ if (ipAddress.isNull ())
37
+ {
38
+ for (const QHostAddress &address : ipAddressesList) {
39
+ if (address != QHostAddress::LocalHost && address.toIPv4Address ())
40
+ {
41
+ ipAddress = address;
42
+ break ;
43
+ }
27
44
}
28
45
}
29
46
30
- if (ipAddress.toString ().isEmpty ())
47
+ // 如果仍然没有找到,返回 localhost IPv4 地址
48
+ if (ipAddress.isNull ()) {
31
49
ipAddress = QHostAddress (QHostAddress::LocalHost);
50
+ }
32
51
33
52
return ipAddress;
34
53
}
You can’t perform that action at this time.
0 commit comments