Qt中TCP通信

总体流程

客户端部分

UI部分

TcpClient.h

#pragma once

#include <QtWidgets/QWidget>
#include "ui_TcpClient.h"
#include <QTcpSocket>
#include <QHostAddress>

class TcpClient : public QWidget
{
	Q_OBJECT

public:
	TcpClient(QWidget *parent = Q_NULLPTR);

private:
	Ui::TcpClientClass ui;
	QTcpSocket* m_pTcpSocket = nullptr;

private slots:
void on_pushButtonLink_clicked();
void on_pushButtonSend_clicked();
void on_pushButtonClose_clicked();
};

TcpClient.cpp

#include "TcpClient.h"

#pragma execution_character_set("utf-8")

TcpClient::TcpClient(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);

	m_pTcpSocket = new QTcpSocket(this);
	//connect()
	ui.lineEditIP->setText("127.0.0.1");
	ui.lineEditPort->setText("8000");

	connect(m_pTcpSocket, &QTcpSocket::connected,
		[=]()
	{
		ui.textEditRead->setText("成功和服务器建立连接");
	});

	connect(m_pTcpSocket, &QTcpSocket::readyRead,
		[=]()
	{
		//获取对方发送的内容
		QByteArray array = m_pTcpSocket->readAll();
		ui.textEditRead->append(array);
	});

}


void TcpClient::on_pushButtonLink_clicked()
{
	QString strIP = ui.lineEditIP->text();
	qint16 nPort = ui.lineEditPort->text().toInt();

	m_pTcpSocket->connectToHost(QHostAddress(strIP), nPort);
}

void TcpClient::on_pushButtonSend_clicked()
{
	//获取编辑框内容
	QString strContent = ui.textEditWrite->toPlainText();
	m_pTcpSocket->write(strContent.toUtf8().data());
}

void TcpClient::on_pushButtonClose_clicked()
{
	m_pTcpSocket->disconnectFromHost();
	m_pTcpSocket->close();
}

服务端部分

UI部分

TcpServer.h

#pragma once

#include <QtWidgets/QWidget>
#include "ui_TcpServer.h"
#include <QTcpServer>
#include <QTcpSocket>

class TcpServer : public QWidget
{
	Q_OBJECT

public:
	TcpServer(QWidget *parent = Q_NULLPTR);

private:
	Ui::TcpServerClass ui;
	//监听套接字
	QTcpServer* m_pTcpServer = nullptr;
	//通信套接字
	QTcpSocket* m_pSocket = nullptr;
	
private slots:
	void on_pushButtonSend_clicked();
	void on_pushButtonClose_clicked();
	
};

TcpServer.cpp

#include "TcpServer.h"

#pragma execution_character_set("utf-8")

TcpServer::TcpServer(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	//监听套接字,指定父对象,让其自动回收空间
	m_pTcpServer = new QTcpServer(this);
	m_pTcpServer->listen(QHostAddress::Any, 8000);
	connect(m_pTcpServer, &QTcpServer::newConnection,
		[=]()
	{
		//取出建立好链接的套接字
		m_pSocket = m_pTcpServer->nextPendingConnection();
		//获取对方的IP和端口
		QString strIP = m_pSocket->peerAddress().toString();
		qint16 nPort = m_pSocket->peerPort();
		QString strTemp = QString("[%1:%2]:成功连接").arg(strIP).arg(nPort);
		ui.textEditRead->setText(strTemp);

		connect(m_pSocket, &QTcpSocket::readyRead,
			[=]()
		{
			//从通信套接字中取出内容
			QByteArray array = m_pSocket->readAll();
			ui.textEditRead->append(array);
		});
	});


	

}

void TcpServer::on_pushButtonSend_clicked()
{
	if (nullptr == m_pSocket)
	{
		return;
	}

	//获取编辑区的内容
	QString strContent = ui.textEditWrite->toPlainText();
	if (strContent.isEmpty())
	{
		return;
	}
	//给对方发送数据,使用的是套接字是m_pSocket
	m_pSocket->write(strContent.toUtf8().data());
}

void TcpServer::on_pushButtonClose_clicked()
{
	if (nullptr == m_pSocket)
	{
		return;
	}

	//主动和客户端断开连接
	m_pSocket->disconnectFromHost();
	m_pSocket->close();

	m_pSocket = nullptr;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值