霞浦网站建设,万网域名安全锿,大庆哈尔滨网站建设,城市建设网站调查问卷同时实现了QTcpSocket、QWebSocket和QLocalSocket的简单通讯deamon#xff0c;支持自动获取本机ip#xff0c;多个客户端交互。在这个基础上你可以自己加错误检测、心跳发送、包封装解析和客户端自动重连等功能。
获取本机电脑ip#xff1a;
QString Widget::getIp()
{QSt… 同时实现了QTcpSocket、QWebSocket和QLocalSocket的简单通讯deamon支持自动获取本机ip多个客户端交互。在这个基础上你可以自己加错误检测、心跳发送、包封装解析和客户端自动重连等功能。
获取本机电脑ip
QString Widget::getIp()
{QString hostNameQHostInfo::localHostName();//主机信息查询返回本机主机名QHostInfo hostInfoQHostInfo::fromName(hostName);//通过主机名称获取IP地址返回一个hostInfo类QString localIP;//本地IP地址string数组QListQHostAddress addListhostInfo.addresses();//创建一个QList对象容器内部存储QHostAddress类型的数据返回一个address数组QHostAddress类提供一个IP地址的信息包括IPv4和Ipvif (!addList.isEmpty()){for (int i0;iaddList.count();i){QHostAddress aHostaddList.at(i);//访问指定位置的元素//测试//qDebug()aHost.toString();if (QAbstractSocket::IPv4ProtocolaHost.protocol()) //protocol()返回IP地址的协议类型,并且只返回IPv4的地址{localIPaHost.toString();//返回IP地址的字符串break;}}}qDebug()localIP;return localIP;
}tcp服务器连接介绍 1、定义并创建接收的信号与槽
//创建tcp服务器对象
m_tcpServer new QTcpServer(this);
//捕获客户端连接
connect(m_tcpServer,SIGNAL(newConnection()),this,SLOT(slottcpNewConnection()));2、开启监听ip和端口
m_tcpServer-listen(QHostAddress(ui-tcpip-text()),ui-tcpport-text().toInt());3、接入连接进来的客户端并配置接收。 //获取新的连接对象QTcpSocket *tcpSocket m_tcpServer-nextPendingConnection();//捕获客户端发送数据的信号connect(tcpSocket,QTcpSocket::readyRead,this,[](){QTcpSocket* socket qobject_castQTcpSocket*(sender());QString val socket-readAll();});//捕获客户端断开的信号connect(tcpSocket,QTcpSocket::disconnected,this,[](){});
}4、发送数据 tcpSocket-write(ui-tcpsendedit-text().toLatin1());tcp客户端连接流程 1、定义并连接ip和端口 m_tcpSocket new QTcpSocket(this);m_tcpSocket-connectToHost(QHostAddress(ui-tcpip-text()),ui-tcpport-text().toInt());2、绑定连接、断开连接和接收的信号与槽 connect(m_tcpSocket,QTcpSocket::connected,this,[](){});connect(m_tcpSocket,QTcpSocket::disconnected,this,[](){});connect(m_tcpSocket,QTcpSocket::readyRead,this,Widget::slotTcpReadData);3、发送 m_tcpSocket-write(ui-tcpsendedit-text().toLatin1());websocket服务器连接流程 1、定义并创建接收的信号与槽 //使用给定的serverName构造一个新的QWebSocketServer。该服务器名称将在HTTP握手阶段被用来识别服务器。它可以为空此时不会将服务器名称发送给客户端。//SslMode指示服务器是通过wssSecureMode还是wsNonSecureMode运行。QWebSocketServer::SecureMode服务器以安全模式运行通过wss;QWebSocketServer::NonSecureMode服务器以非安全模式运行通过wsm_webServer new QWebSocketServer(,QWebSocketServer::NonSecureMode,this);connect(m_webServer,SIGNAL(newConnection()),this,SLOT(slotWebNewConnect()));2、开启监听ip和端口 m_webServer-listen(QHostAddress(ui-webip-text()),ui-webport-text().toInt());3、接入连接进来的客户端并配置接收。 QWebSocket* webSocket m_webServer-nextPendingConnection();connect(webSocket,QWebSocket::disconnected,this,[](){});connect(webSocket,QWebSocket::textMessageReceived,this,[](QString val){});4、发送数据 webSocketsendTextMessage(ui-websendedit-text());//sendBinaryMessage发送二进制数据。websocket客户端连接流程 1、定义并连接ip和端口 m_webSocket new QWebSocket();m_webSocket-open(QUrl(QString(ws://%1:%2).arg(ui-webip-text()).arg(ui-webport-text())));2、绑定连接、断开连接和接收的信号与槽 connect(m_webSocket,QWebSocket::connected,this,[](){});connect(m_webSocket,QWebSocket::disconnected,this,[](){});connect(m_webSocket,QWebSocket::textMessageReceived,this,Widget::slotwebReadData);3、发送 m_webSocket-sendTextMessage(ui-websendedit-text());//sendBinaryMessage发送二进制数据。本地通信服务器连接流程 1、定义并创建接收的信号与槽 //创建本地服务器对象m_localServer new QLocalServer(this);//捕获客户端连接connect(m_localServer,SIGNAL(newConnection()),this,SLOT(slotlocalnewConnection()));2、开启监听key值 QLocalServer::removeServer(m_oldKey); //删除已有的连接m_localServer-listen(ui-localkey-text()); //监听客户端m_oldKey ui-localkey-text();3、接入连接进来的客户端并配置接收。 if (m_localServer-hasPendingConnections()){//获取新的连接对象QLocalSocket* localSocket m_localServer-nextPendingConnection();//捕获客户端发送数据的信号connect(localSocket,QLocalSocket::readyRead,this,[](){QLocalSocket* socket qobject_castQLocalSocket*(sender());QString val socket-readAll();});//捕获客户端断开的信号connect(localSocket,QLocalSocket::disconnected,this,[](){});}4、发送 localSocket-write(ui-tcpsendedit-text().toLatin1());本地通信客户端连接流程 1、定义并连接key值 m_localSocket new QLocalSocket(this);m_localSocket-connectToServer(ui-localkey-text(),QIODevice::ReadWrite);2、绑定连接、断开连接和接收的信号与槽 connect(m_localSocket,QLocalSocket::connected,this,[](){});connect(m_localSocket,QLocalSocket::disconnected,this,[](){});connect(m_localSocket,QLocalSocket::readyRead,this,Widget::slotlocalReadData);3、发送 m_localSocket-write(ui-localsendedit-text().toLatin1());完整示例代码