- 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 st