Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化 多网卡下优先广播 192 开头 IP #164

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
*build-PolyChat*
*build*

# C++ objects and libs
*.slo
Expand Down
31 changes: 25 additions & 6 deletions App/bll_polychat.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,37 @@ static QHostAddress getIPAddress()
{
QHostAddress ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
for (int i = 0; i < ipAddressesList.size(); ++i)

// 优先寻找以192开头的IPv4地址
for (const QHostAddress &address : ipAddressesList)
{
if (ipAddressesList.at(i) != QHostAddress::LocalHost
&& ipAddressesList.at(i).toIPv4Address())
if (address != QHostAddress::LocalHost && address.toIPv4Address())
{
ipAddress = ipAddressesList.at(i);
break;
QString ipString = address.toString();
if (ipString.startsWith("192"))
{
ipAddress = address;
break;
}
}
}

// 如果没有找到以192开头的地址,再考虑其他IPv4地址
if (ipAddress.isNull())
{
for (const QHostAddress &address : ipAddressesList) {
if (address != QHostAddress::LocalHost && address.toIPv4Address())
{
ipAddress = address;
break;
}
}
}

if (ipAddress.toString().isEmpty())
// 如果仍然没有找到,返回 localhost IPv4 地址
if (ipAddress.isNull()) {
ipAddress = QHostAddress(QHostAddress::LocalHost);
}

return ipAddress;
}
Expand Down
Loading