1.服务器头文件
#ifndef TCP_01_H
#define TCP_01_H
#include <QtWidgets/QWidget>
#include "ui_tcp_01.h"
#include <QTcpServer>//监听套接字
#include <QTcpSocket>//通信套接字
class TCP_01 : public QWidget
{
Q_OBJECT
public:
TCP_01(QWidget *parent = 0);
~TCP_01();
private slots:
void sendInfo();
void closeConnect();
private:
Ui::TCP_01Class ui;
QTcpServer* tcpServer;//监听套接字
QTcpSocket* tcpSocket;//通信套接字
bool flag;
};
#endif // TCP_01_H
2.服务器CPP
#include "tcp_01.h"
#include <QMessageBox>
TCP_01::TCP_01(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
flag = false;
tcpServer = nullptr;
tcpSocket = nullptr;
tcpServer = new QTcpServer(this);//监听套接字,可以不指定父对象,指定父对象是为了自动回收
tcpServer->listen(QHostAddress::Any, 8888);//监听
connect(tcpServer, &QTcpServer::newConnection,
[=]()
{
//取出建立好连接的套接字
tcpSocket = tcpServer->nextPendingConnection();
//获取对方的IP 和端口
QString ip = tcpSocket->peerAddress().toString();
qint16 port = tcpSocket->peerPort();
QString temp = QString::fromLocal8Bit("[%1:%2]:成功连接").arg(ip).arg(port);
QMessageBox::information(this, "Tip",temp);
connect(tcpSocket,&QTcpSocket::readyRead,
[=]()
{
//从通信套接字从取出内容
QByteArray arry = tcpSocket->readAll();
if (arry == "0xxxxqiut")
{
ui.readtextEdit->append(QString::fromLocal8Bit("对方已下线!"));
}
else
{
ui.readtextEdit->setAlignment(Qt::AlignLeft);
ui.readtextEdit->append(arry);
flag = true;
}
}
);
}
);
connect(ui.sendButton,SIGNAL(clicked(bool)),this, SLOT(sendInfo()));
connect(ui.closeButton,SIGNAL(clicked(bool)),this, SLOT(closeConnect()));
}
TCP_01::~TCP_01()
{
}
void TCP_01::sendInfo()
{
if (nullptr == tcpSocket)
{
return;
}
//获取编辑区内容
QString content = ui.writetextEdit->toPlainText();
if (flag == true)
{
ui.readtextEdit->append("\n");
}
ui.readtextEdit->setAlignment(Qt::AlignRight);
ui.readtextEdit->append(content);
//给对方发数,使用套接字是tcpSocket
tcpSocket->write(content.toUtf8().data());
ui.writetextEdit->clear();
flag = false;
}
void TCP_01::closeConnect()
{
if (nullptr == tcpSocket)
{
return;
}
//主动和客户端端口断开连接
tcpSocket->write("0xxxxqiut");
tcpSocket->disconnectFromHost();
tcpSocket->close();
tcpSocket = nullptr;
QMessageBox::information(this, "Tip",QString::fromLocal8Bit("成功断开连接"));
}
3.客户端头文件
#ifndef TCP_02_H
#define TCP_02_H
#include <QtWidgets/QWidget>
#include "ui_tcp_02.h"
#include <QTcpSocket>
class TCP_02 : public QWidget
{
Q_OBJECT
public:
TCP_02(QWidget *parent = 0);
~TCP_02();
private:
Ui::TCP_02Class ui;
QTcpSocket* tcpSocket;
bool flag;
private slots:
void connectToServer();
void sendInfo();
void closeConnect();
};
#endif // TCP_02_H
4.客户端CPP
#include "tcp_02.h"
#include <QHostAddress>
#include <QMessageBox>
TCP_02::TCP_02(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
tcpSocket = nullptr;
flag = false;
tcpSocket = new QTcpSocket(this);
connect(ui.connnectButton, SIGNAL(clicked(bool)), this, SLOT(connectToServer()));
connect(ui.sendButton,SIGNAL(clicked(bool)), this, SLOT(sendInfo()));
connect(ui.closeButton,SIGNAL(clicked(bool)), this, SLOT(closeConnect()));
connect(tcpSocket, &QTcpSocket::connected,
[=]()
{
QMessageBox::information(this, "Tip",QString::fromLocal8Bit("成功和服务器建立连接"));
}
);
connect(tcpSocket, &QTcpSocket::readyRead,
[=]()
{
//获取对方发送的内容
QByteArray arry = tcpSocket->readAll();
if (arry == "0xxxxqiut")
{
ui.READtextEdit->append(QString::fromLocal8Bit("对方已下线!"));
}
ui.READtextEdit->setAlignment(Qt::AlignLeft);
ui.READtextEdit->append(arry);
flag = true;
}
);
}
TCP_02::~TCP_02()
{
}
void TCP_02::connectToServer()
{
QString IP = ui.lineEditIP->text();
qint16 port = ui.lineEditPort->text().toInt();
//主动和服务器建立连接
tcpSocket->connectToHost(QHostAddress(IP), port);
}
void TCP_02::sendInfo()
{
if (flag == true)
{
ui.READtextEdit->append("\n");
}
QString str = ui.WritetextEdit->toPlainText();
ui.READtextEdit->setAlignment(Qt::AlignRight);
ui.READtextEdit->append(str);
tcpSocket->write(str.toUtf8().data());
ui.WritetextEdit->clear();
flag = false;
}
void TCP_02::closeConnect()
{
//主动和对方断开连接
tcpSocket->write("0xxxxqiut");
tcpSocket->disconnectFromHost();
tcpSocket->close();
tcpSocket = nullptr;
QMessageBox::information(this, "Tip",QString::fromLocal8Bit("成功断开连接"));
}
5.实现效果图