文章目录
- qt中TCP的实现
 
qt中TCP的实现
-  
学习视频
 -  
QT中可以通过TCP协议让服务器和客户端之间行通信。服务器和客户端的具体流程

 -  
下方的信号都是系统提供的,我们只需要写相应的槽函数
 
A、服务器:
- 创建QTcpServer对象
 - 启动服务器(监听)调用成员方法listen(
QHostAddress::Any,端口号) - 当有客户端链接时候会发送
newConnection()信号,触发自定义槽函数接受链接(使用nextPendingConnection()与客户端建立连接,得到一个与客户端通信的套接字QTcpSocket) - QTcpsocket发送数据用成员方法
write, - 读数据当客户端有数据来,QTcpSocket对象就会发送
readyRead()信号,触发自定义槽函数读取数据 
B、客户端 :
- 创建QTcpSocket对象
 - 链接服务器
connectToHost(QHostAddress("ip"),端口号) - QTcpsocket发送数据用成员方法
write, - 读数据当对方有数据来,QTcpSocket对象就会发送
readyRead()信号,触发自定义槽函数读取数据 
直接上源码
- 此代码在vs2017环境下写的
 - 会有详细注释,仔细阅读代码
 
先添加network模块
 
 测试效果:
 
 textSever.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_textSever.h"
#include <QTcpServer.h>
#include <QTcpSocket.h>
class textSever : public QMainWindow
{
    Q_OBJECT
public:
    textSever(QWidget *parent = nullptr);
    ~textSever();
signals:
	
public slots:
	void new_connect();// 有新的连接
	void rev_mag();// 接收发送过来的消息
	void send_mag();// 发送消息
private:
    Ui::textSeverClass ui;
	QTcpServer* severSocket;// 监听套接字
	QTcpSocket* clientSocket;// 通信套接字
};
 
textSever.cpp
#include "textSever.h"
textSever::textSever(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
	// 初始化 ui
	ui.sIP->setText("127.0.0.1");
	ui.sPort->setText("9999");
	// 初始化套接字
	severSocket = new QTcpServer(this);
	// 监听套接字  监听本地
	severSocket->listen(QHostAddress(ui.sIP->text()), ui.sPort->text().toInt());
	// 当有新的连接的时候会触发 newConnection 信号
	connect(severSocket, &QTcpServer::newConnection, this, &textSever::new_connect);
}
textSever::~textSever()
{}
void textSever::new_connect() {
	// 有点像客户端的套接字 accept
	// 连接客户端
	clientSocket = severSocket->nextPendingConnection();
	// ui.record->append("有新的连接。。。。。");在vs中会有乱码
	ui.record->append(QString::fromLocal8Bit("有新的连接。。。。。"));
	// 当客户端有消息发送过来的时候,就会触发 readyRead 信号
	connect(clientSocket, &QTcpSocket::readyRead, this, &textSever::rev_mag);
	// 点击按钮,向客户端发送数据
	connect(ui.sendBtn, &QPushButton::clicked, this, &textSever::send_mag);
}
void textSever::rev_mag()
{
	// 接收数据
	QByteArray array = clientSocket->readAll();
	ui.record->append(array);
}
void textSever::send_mag()
{
	//发送数据
	clientSocket->write(ui.msg->toPlainText().toUtf8().data());
	ui.record->append("Me Say: " + ui.msg->toPlainText());
	// 清除发送框的消息
	ui.msg->clear();
}
 
textClient.h
#pragma once
#include <QMainWindow>
#include "ui_textClient.h"
#include <QTcpSocket.h>
#include <QHostAddress.h>
class textClient : public QMainWindow
{
	Q_OBJECT
public:
	textClient(QWidget *parent = nullptr);
	~textClient();
signals:
public slots:
	void rev_mag();// 接收发送过来的消息
	void send_mag();// 发送消息
private:
	Ui::textClientClass ui;
	QTcpSocket* clientSocket;
};
 
textClient.cpp
#include "textClient.h"
textClient::textClient(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	//初始化
	clientSocket = new QTcpSocket(this);
	// 初始化 ui
	ui.sIP->setText("127.0.0.1");
	ui.sPort->setText("9999");
	
	//连接服务器  这个是非阻塞方式,连接不上也不管
	clientSocket->connectToHost(QHostAddress(ui.sIP->text()), ui.sPort->text().toInt());
	// 接收数据
	connect(clientSocket, &QTcpSocket::readyRead, this, &textClient::rev_mag);
	// 发送数据
	connect(ui.sendBtn, &QPushButton::clicked, this, &textClient::send_mag);
}
textClient::~textClient()
{}
void textClient::rev_mag()
{
	QByteArray array = clientSocket->readAll();
	ui.record->append(array);
}
void textClient::send_mag()
{
	clientSocket->write(ui.msg->toPlainText().toUtf8().data());
	ui.record->append("my say :" + ui.msg->toPlainText());
	ui.msg->clear();
}
 
main.cpp
#include "textSever.h"
#include "textClient.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
	//对于按钮文字显示不全的解决方法,必须放在main中的第一行
	QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication a(argc, argv);
    textSever w;
	w.setWindowTitle(QString::fromLocal8Bit("服务器"));
    w.show();
	
	textClient c;
	c.setWindowTitle(QString::fromLocal8Bit("客户端"));
	c.show();
    return a.exec();
}
                


















