当前位置:网站首页>Qt客户端套接字QTcpSocket通过bind指定本地ip
Qt客户端套接字QTcpSocket通过bind指定本地ip
2022-06-10 22:53:00 【大橘】
背景:
我做一个上位机软件,需要同时控制几个下位机,使用套接字方式通讯。但制作阶段我不希望总上机调试,而是在本机模拟通信过程。
本机网卡可以指定多个ip地址,我希望客户端(模拟下位机)连接服务端(模拟上位机)时,能够指定ip和端口。这样在连接时,才能识别是哪个下位机。而下位机不可能设计为在连接后先发个包确认身份。至少目前我不希望下位机做修改,而是我在上位机端自己搞定。
按照帮助说明使用QTcpSocket类的bind函数可以绑定ip和端口,也就是指定。但事实上一旦精确到端口,会各种不爽。
不需要识别客户端身份时,不用管它,客户端会根据自己所在的主机ip,自动选择随机端口。这时候随便连接断开很灵活,不收任何影响。
一旦绑定了ip和端口,是不可以频繁断开重连的。网上各种方法,无非是监听套接字不要自己直接用QTcpServer,要通过API调用。或者端口复用。
事实上对于我都不太适用。适用Qt就是为了跨平台,不想尝试跟OS有关的特定API。而端口复用对于qt其实并不太灵活,还是要等OS的响应。
解决方法:
仅限于我的需求,能识别客户端身份就好。
很简单,使用bind绑定的时候,只绑定ip就行了,端口就让它随机。本机网卡可以多几个ip,每个客户端指定一个ip,不管端口。比较高效。
注意:
qt比较坑的一些set函数,比如setPeerName,setLocalAddress等,首先是protect类型,还要写个子类间接调用才行。即使能调用也是然并卵,这些函数的作用仅仅是为了设定一些函数的返回值,在实际应用中意义不大。或者说对我的需求意义不大。
往往这些set函数会达到误导人的效果,不是set以后就一定能get。
边栏推荐
- Things about Bluetooth development (10) -- getting to know ble for the first time
- 【Go语言学习】——并发编程
- The same customized right-click menu beautification on this site - Xingze V Club
- Binary tree pruning
- Four ways to add names to threads in the thread pool
- Deepin20菜单启动选项后自检到iwlwifi停机
- Several common current transformer sampling circuits
- csdn每日一练——有序表的折半查找
- Merge sort
- 【Turtle表白合集】“海底月是天上月,眼前人是心上人。”余生多喜乐,长平安~(附3款源码)
猜你喜欢

Easyrecovery15 simple and convenient data recovery tool

博文推荐|构建 IoT 应用——FLiP 技术栈简介
![[untitled]](/img/7e/aab9560ef5a1b93f737a82561ec114.png)
[untitled]

The serial port in the visa test panel under LabVIEW or max does not work

Compared with the "South-to-North Water Transfer", what will the "east to west" of the fire bring to cloud computing?
![[pyGame games] I'm not afraid you can't walk the maze series: the ultimate AI walks the maze. After learning, it will take you to open the door to a new world ~ (with game source code)](/img/57/dcf291b044b5e5860a7fdc817076d5.jpg)
[pyGame games] I'm not afraid you can't walk the maze series: the ultimate AI walks the maze. After learning, it will take you to open the door to a new world ~ (with game source code)

【Pygame小游戏】不怕你走不过系列:极致AI走迷宫,学习完带你打开新世界大门~(附游戏源码)
![[pyGame] stir up your brain and play the](/img/0c/fd558c843705af19720d790da4ff06.jpg)
[pyGame] stir up your brain and play the "24 o'clock" idea together ~ (awesome)

Things about Bluetooth development (10) -- getting to know ble for the first time

【颜值检测神器】来,请拿出你们的绝活(这颜值,对得起观众么?)
随机推荐
【漫天烟花】绚烂烟花点亮夜空也太美了叭、某程序员携带烟花秀给大家拜年啦~
Serial port missing in Ni Max in LabVIEW
Insert sort
【颜值检测神器】来,请拿出你们的绝活(这颜值,对得起观众么?)
2022 college entrance examination quantitative paper | please answer the questions for quantitative candidates
[pyGame] stir up your brain and play the "24 o'clock" idea together ~ (awesome)
File转为MultipartFile的方法
上海炒股开户是安全的吗?
【Pygame小游戏】趣味益智游戏 :打地鼠,看一下能打多少只呢?(附源码)
After deepin20 menu startup option, the self-test indicates that iwlwwifi is stopped
Typecho blog site wide deployment of Tencent cloud CDN tutorial - Xingze V Club
示波器和频谱分析仪的区别
[pyGame games] here it is. This Gobang game is super A. share it with your friends~
Leetcode-713 subarray with product less than k
【Turtle表白合集】“海底月是天上月,眼前人是心上人。”余生多喜乐,长平安~(附3款源码)
The serial port in the visa test panel under LabVIEW or max does not work
Openresty installation
Bluetooth development (3) -- look at the air bag
数据库表结构
csdn每日一练——找出最接近元素并输出下标