首先
在.pro的文件中加入网络模块 QT += network
主界面
因为嫌麻烦,就一次性打开两个窗口
如果想要有层次感,可以在添加一个界面,用按钮跳转
#include "tcp_server.h"
#include "client.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Tcp_server w;
w.show();//打开服务端窗口
client c;
c.show();//打开客户端窗口
return a.exec();
}
服务端
头文件.h
#ifndef TCP_SERVER_H
#define TCP_SERVER_H
#include <QMainWindow>
#include<QTcpServer>//TCP服务类
#include<QTcpSocket>//TCP套接字
#include <QByteArray>//字节数组
QT_BEGIN_NAMESPACE
namespace Ui { class Tcp_server; }
QT_END_NAMESPACE
class Tcp_server : public QMainWindow
{
Q_OBJECT
public:
Tcp_server(QWidget *parent = nullptr);
~Tcp_server();
private:
Ui::Tcp_server *ui;
QTcpServer * TcpServer;//服务端指针
QTcpSocket* TcpSocket;//使用链表,存入用户套接字
public slots:
void slot_TcpServer();//获取客户端的连接
void slot_TcpServer_readRead();//读取
void slot_pushButton_bind();//监听
void slot_pushButton_send();//发送
};
#endif // TCP_SERVER_H
实现.cpp
#include "tcp_server.h"
#include "ui_tcp_server.h"
Tcp_server::Tcp_server(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Tcp_server)
{
ui->setupUi(this);
TcpServer=new QTcpServer(this);//申请服务端TCP类空间
/*======================连接信号槽========================*/
connect(TcpServer,SIGNAL(newConnection()),this,SLOT(slot_TcpServer()));//连接客户端
connect(ui->pushButton_monitor,SIGNAL(clicked(bool)),this,SLOT(slot_pushButton_bind()));//监听
connect(ui->pushButton_send,SIGNAL(clicked(bool)),this,SLOT(slot_pushButton_send()));//发送
}
Tcp_server::~Tcp_server()
{
delete ui;
}
//建立客户端的连接-
void Tcp_server::slot_TcpServer()
{
TcpSocket=TcpServer->nextPendingConnection();//读取已经连接挂起的客户端
QString ip=TcpSocket->peerAddress().toString();//ip地址
qint64 port=TcpSocket->peerPort();//端口号
QString temp=QString("客户端地址ip:%1 \t,端口号port:%2").arg(ip).arg(port);
ui->textEdit_receive->append(temp);//追加写入接收文本
connect(TcpSocket,SIGNAL(readyRead()),this,SLOT(slot_TcpServer_readRead()));//监听到套接字发送信号,去处理信息
}
//读取数据
void Tcp_server::slot_TcpServer_readRead()
{
//QTcpSocket* TcpSocket=(QTcpSocket*)sender();//接收发送信息的套接字
QByteArray buf=TcpSocket->readAll();//读取缓冲区数据
ui->textEdit_receive->append(buf);
}
//监听
void Tcp_server::slot_pushButton_bind()
{
QString ip=ui->lineEdit_ip->text();//填写的ip地址
int port=ui->lineEdit_port->text().toInt();//填写的port端口
bool ok=false;//用于判断监听是否成功
*//因为是监听任意地址,所以ip是可以不用填写的*
ok=TcpServer->listen(QHostAddress::Any,port);//双向绑定
//监听成功显示“取消监听”,失败则不变
if(ok==true)
{
ui->pushButton_monitor->setText("取消监听");
}
else
{
ui->pushButton_monitor->setText("监听");
}
}
//发送数据
void Tcp_server::slot_pushButton_send()
{
QString text=ui->lineEdit_send->text();//记录要发送的数据
ui->lineEdit_send->clear();//清空
ui->textEdit_send->setText(text);//将发送的数据放到发送文本显示,方便记录
TcpSocket->write(text.toUtf8());//将数据变成Utf8编解码,并发送
}
客户端
#ifndef CLIENT_H
#define CLIENT_H
#include <QWidget>
#include<QTcpSocket>//TCP套接字类
#include <QByteArray>//字节数组
#include<QHostAddress>//地址类
namespace Ui {
class client;
}
class client : public QWidget
{
Q_OBJECT
public:
explicit client(QWidget *parent = nullptr);
~client();
private:
Ui::client *ui;
QTcpSocket* TcpSocket;
public slots:
void slot_TcpSocket_readyRead();//套接字接受到数据信号
void slot_pushButton_connect();//连接按钮
void slot_TcpSocket_connected();//连接
void slot_TcpSocket_disconnected();//断开
void slot_pushButton_send();//发送
};
#endif // CLIENT_H
实现.cpp
#include "client.h"
#include "ui_client.h"
client::client(QWidget *parent) :
QWidget(parent),
ui(new Ui::client)
{
ui->setupUi(this);
TcpSocket=new QTcpSocket;
/*==================槽函数=========================*/
connect(TcpSocket,SIGNAL(readyRead()),this,SLOT(slot_TcpSocket_readyRead()));//接受信息
connect(TcpSocket,SIGNAL(connected()),this,SLOT(slot_TcpSocket_connected()));//连接
connect(TcpSocket,SIGNAL(disconnected()),this,SLOT(slot_TcpSocket_disconnected()));//断开连接
connect(ui->pushButton_connected,SIGNAL(clicked(bool)),this,SLOT(slot_pushButton_connect()));//绑定主机
connect(ui->pushButton_send,SIGNAL(clicked(bool)),this,SLOT(slot_pushButton_send()));//发送
}
client::~client()
{
delete ui;
}
void client::slot_TcpSocket_readyRead()
{
QByteArray buf=TcpSocket->readAll();//接收数据
ui->textEdit_receive->append(buf);//追加写填入数据
}
void client::slot_TcpSocket_connected()
{
ui->pushButton_connected->setText("连接成功");
ui->pushButton_send->setEnabled(true);//使能发送按钮
}
void client::slot_TcpSocket_disconnected()
{
ui->pushButton_connected->setText("断开连接");
ui->pushButton_send->setEnabled(false);//失能发送按钮
}
void client::slot_pushButton_connect()
{
QString ip=ui->lineEdit_ip->text();//主机ip
int port=ui->lineEdit_port->text().toInt();//主机port
TcpSocket->connectToHost(QHostAddress(ip),port);//连接主机
}
void client::slot_pushButton_send()
{
QString text=ui->lineEdit_send->text();//发送的文字
ui->lineEdit_send->setText("");//清空发射的文字框
ui->textEdit_send->append(text);//记录写入的文字
TcpSocket->write(text.toUtf8());//发送
}