AddIPAddress添加临时IP后,socket bind失败

问题描述

在Win10\Win11下使用addIPAddress添加临时IP成功后,立即创建socket,bind失败

cpp 复制代码
if(!m_socket->bind(QHostAddress(m_localIP), listenPort))
{
    qCritical() << QString("bind error %1").arg(m_socket->errorString());
    return;
}

错误输出:

bind error 这个地址不可用

解决方案

经测试addIPAddress添加临时IP后,未立即生效,至少要超过3秒的等待时间,故采用如下方案:

cpp 复制代码
int count = 0;
bool bindOK = false;
do
{
	bindOK = m_socket->bind(QHostAddress(m_localIP), listenPort);
	if(bindOK){
		break;
	}

	QThread::msleep(500);
} while (count++ < 12);

if (!bindOK)
{
	qCritical() << QString("bind error %1").arg(m_socket->errorString());
	return;
}

查询相关资料

https://learn.microsoft.com/zh-cn/windows/win32/api/iphlpapi/nf-iphlpapi-addipaddress

https://bbs.csdn.net/topics/390226048

https://stackoverflow.com/questions/23620805/addipaddress-not-taking-immediate-effect

相关推荐
阿白的白日梦16 小时前
winget基础管理---更新/修改源为国内源
windows
埃博拉酱5 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
唐宋元明清21885 天前
.NET 本地Db数据库-技术方案选型
windows·c#
DianSan_ERP5 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
加号35 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
tryCbest5 天前
Windows环境下配置pip镜像源
windows·pip
呉師傅5 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
百事牛科技5 天前
保护文档安全:PDF限制功能详解与实操
windows·pdf
一个人旅程~5 天前
如何用命令行把win10/win11设置为长期暂停更新?
linux·windows·经验分享·电脑
2501_946205525 天前
晶圆机器人双臂怎么选型?适配2-12寸晶圆的末端效应器有哪些?
服务器·网络·机器人