效果图
服务端完整代码
.h文件
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_TcpTest.h"
#include <QTcpServer>//监听套接字
#include <qtcpsocket.h>//通信套接字
class TcpTest : public QMainWindow
{
Q_OBJECT
public:
TcpTest(QWidget *parent = nullptr);
~TcpTest();
private:
Ui::TcpTestClass ui;
QTcpServer* tcpserver = nullptr;//监听套接字
QTcpSocket* tcpsocket = nullptr;//通信套接字
private slots:
void slot_send();
};
.cpp文件
#include "TcpTest.h"
#pragma execution_character_set("utf-8")
TcpTest::TcpTest(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
tcpserver = new QTcpServer(this);//监听套接字
tcpserver->listen(QHostAddress::Any, 8888);
setWindowTitle("服务器:8888");
connect(tcpserver, &QTcpServer::newConnection, this, [=]() {
//取出建立好连接的套接字
tcpsocket = tcpserver->nextPendingConnection();
//获取对方的ip和端口
QString ip = tcpsocket->peerAddress().toString();
qint16 port = tcpsocket->peerPort();
QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port);
ui.textEditRead->setText(temp);
connect(tcpsocket, &QTcpSocket::readyRead, this, [=]() {
//从通信套接字中取出内容
QByteArray array = tcpsocket->readAll();
ui.textEditRead->append(array);
});
});
connect(ui.pbSend, &QPushButton::clicked, this, &TcpTest::slot_send);
connect(ui.pbClose, &QPushButton::clicked, this, [=]() {
if (tcpsocket == nullptr)
{
return;
}
//主动和客户端端口连接
tcpsocket->disconnectFromHost();
tcpsocket->close();
tcpsocket == nullptr;
});
tcpsocket = new QTcpSocket(this);//通信套接字
}
TcpTest::~TcpTest()
{}
void TcpTest::slot_send()
{
if (tcpsocket == nullptr)
{
return;
}
//获取编辑区内容
QString str = ui.textEditWrite->toPlainText();
//给对方发送数据,使用套接字是tcpsocket
tcpsocket->write(str.toUtf8().data());
}
main.cpp文件
#include "TcpTest.h"
#include <QtWidgets/QApplication>
#include "ui_Client.h"
#include "Client.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TcpTest w;
w.show();
Client w2;
w2.show();
return a.exec();
}
客户端完整代码
Client.h文件
#pragma once
#include <QWidget>
#include "ui_Client.h"
#include <QTcpSocket>//通信套接字
class Client : public QWidget
{
Q_OBJECT
public:
Client(QWidget *parent = nullptr);
~Client();
private:
Ui::ClientClass ui;
QTcpSocket* tcpsocket = nullptr;
};
Client.cpp文件
#pragma execution_character_set("utf-8")
#include "Client.h"
#include <QTcpServer>//监听套接字
#include <qtcpsocket.h>//通信套接字
Client::Client(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
tcpsocket = new QTcpSocket(this);
connect(tcpsocket, &QTcpSocket::connected, this, [=]() {
ui.textEditRead->setText("成功和服务器建立好连接!!!");
});
connect(ui.pbConnect, &QPushButton::clicked, this, [=]() {
//获取服务器IP和端口
QString ip = ui.lineEditIp->text();
qint16 port = ui.lineEditPort->text().toInt();
//主动和服务器建立连接
tcpsocket->connectToHost(QHostAddress(ip), port);
});
connect(ui.pushButtonSend, &QPushButton::clicked, this, [=]() {
//获取编辑框的内容
QString str = ui.textEditWrite->toPlainText();
//发送数据
tcpsocket->write(str.toUtf8().data());
});
connect(tcpsocket, &QTcpSocket::readyRead, this, [=]() {
//获取对方发送的内容
QByteArray array = tcpsocket->readAll();
//追加到编辑区
ui.textEditRead->append(array);
});
connect(ui.pushButtonClose, &QPushButton::clicked, this, [=]() {
//主动和对方断开连接
tcpsocket->disconnectFromHost();
});
}
Client::~Client()
{}