示例功能
创建一个tcp服务器,当有客户端连接时,向客户端发送下面列表中的一段文字。该示例可配合BlockingFortuneClient
示例使用
QVector<QString> fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
<< tr("You've got to think about tomorrow.")
<< tr("You will be surprised by a loud noise.")
<< tr("You will feel hungry again in another hour.")
<< tr("You might have mail.")
<< tr("You cannot kill time without injuring eternity.")
<< tr("Computers are not intelligent. They only think they are.");
代码解析
- 程序执行前会根据当前网络配置,决定是否创建新的
network session
- 如果创建
network session
,将网络位置的identifier
写入注册表,下次启动优先使用该网络配置.如果不创建,执行第三步。
- 创建TCPServer,监听所有网络接口并自动选择端口
- 当有新连接时想客户端发送一段文字
小知识点
-
QSettings 如果不设保存地址,而是使用
Scope
中的枚举指定,则配置信息被保存到win注册表中,具体路径视枚举值确定enum Scope { UserScope, SystemScope };
例如当枚举值为UserScope时,路径在
HKEY_CURRENT_USER\SOFTWARE
下 -
QLabel的成员函数
setTextInteractionFlags
可设置QLabel显示的文字是否可以被复制选中等 -
获取本地所以ip地址
-
创建一个0-10的随机数
QRandomGenerator::global()->bounded(10)