- QT5 简单本地局域网TCP通信
注意:*.pro文件中要增加 QT += network
TCP client端
头文件中:
#include <QtNetwork>
QTcpSocket *client;
char *data="hello qt!";
类的实现文件下,类的构造函数中:
client = new QTcpSocket(this);
client->connectToHost(QHostAddress("10.21.11.66"), 6665);
client->write(data);
TCP server端
头文件:
#include <QtNetwork>
QTcpServer *server;
QTcpSocket *clientConnection;
public slots:
void readClient();
void acceptConnection();
类的实现文件下,类的构造函数中:
server = new QTcpServer();
server->listen(QHostAddress::Any, 6665);
connect(server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
类的实现文件下:
void 类名::acceptConnection()
{
clientConnection = server->nextPendingConnection();
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readClient()));
}
void 类名::readClient()
{
//QString str = clientConnection->readAll();
//或者
char buf[1024];
clientConnection-&

本文介绍了在QT 5.6.1中使用C++进行本地局域网TCP通信的步骤,强调了.pro文件中添加QT += network的重要性。还涉及了QString类型转换、控件操作及UDP通信的基础知识,包括UDP通信中发送端和接收端的概念。
最低0.47元/天 解锁文章
1716

被折叠的 条评论
为什么被折叠?



