QT实现简单的tcp通信聊天工具
目录结构:
pro文件中添加:
1. tcp_server :
1.1 服务器端头文件
/******app.h ******/
#ifndef APP_H
#define APP_H
#include
#include //*
#include //*
#include //*
QT_BEGIN_NAMESPACE
namespace Ui { class app; }
QT_END_NAMESPACE
class app : public QWidget
{
Q_OBJECT
public:
app(QWidget *parent = nullptr);
~app();
//private slots:
//void on_pushButton_Connect_clicked();
private:
Ui::app *ui;
QTcpServer * m_tcpServer;
QTcpSocket * m_tcpSocket;
QHostAddress m_IP;//ip为QhostAdderss格式
quint16 m_PORT;//port为无符号bit,2byte即short类型
};
#endif // APP_H
1.2 服务器端cpp文件
/******app.cpp文件(服务器端)******/
#include "app.h"
#include "ui_app.h"
#include // 网络编程头文件
#include
#include
#include
app::app(QWidget *parent)
: QWidget(parent)
, ui(new Ui::app)
{
ui->setupUi(this);
//实例化
m_tcpServer = new QTcpServer(this);
//获取本机ip地址,这里获得的是所有ipv4的ip,将所有ip放到下拉菜单中,需要用户自己选定本机对应的ip地址
QList list = QNetworkInterface::allAddresses();
//The QNetworkInterface class provides a listing of the host's IP addresses and network interfaces.
foreach (QHostAddress address, list) {
if (address.protocol() == QAbstractSocket::IPv4Protocol) {
QString ipAddr = address.toString();
ui->comboBox_IP->addItem(ipAddr); //循环将ip添加到comboBox里
}
}
//设置potr栏默认值为9000
ui->lineEdit_PORT->setText("9000");
//点击启动服务器按钮以启动服务器,获取ip和port
connect(ui->pushButton_Connect, &QPushButton::clicked, [=]()
{
//设置ip和端口, ip是无符号32位,port是short,toInt也尚可
m_IP = QHostAddress(ui->comboBox_IP->currentText());//获取当前选中的ip
qDebug() << "ip:" << m_IP;//在qt端测试
m_PORT = ui->lineEdit_PORT->text().toInt();
qDebug() << "port" << m_PORT;
//监听端口
m_tcpServer->listen(m_IP, m_PORT);
});
//当有新的连接时,触发槽
connect(m_tcpServer, &QTcpServer::newConnection, this, [=](){
//服务器端打印连接信息
ui->textEdit_Receive->append("新的客户端连接!");
//接受客户端套接字对象,相当于accept,并将所有连接排序(队列,响应顺序)
m_tcpSocket = m_tcpServer->nextPendingConnection();
//服务器给客户端 发送数据(数据会隐式转换成QByteArray)
//发送
connect(ui->pushButton_Send, &QPushButton::clicked, this, [=](){
m_tcpSocket->write("Server say:" + ui->textEdit_Send->toPlainText().toUtf8());
});
//接收
//要保证tcpSocket是一个有效的对象
connect(m_tcpSocket, &QTcpSocket::readyRead, this, [=](){
//接受客户端数据
QByteArray array = m_tcpSocket->readAll();
ui->textEdit_Receive->append(array);
});
});
}
app::~app()
{
delete ui;
}
2. tcp_Client:
2.1 tcp_client.h
#ifndef TCP_CLIENT_H
#define TCP_CLIENT_H
#include
#include //*
#include //*
namespace Ui {
class tcp_Client;
}
class tcp_Client : public QWidget
{
Q_OBJECT
public:
explicit tcp_Client(QWidget *parent = nullptr);
~tcp_Client();
private slots:
void on_pushButton_Clear_clicked();
private:
Ui::tcp_Client *ui;
QTcpSocket * m_tcpClientSocket;//*
};
#endif // TCP_CLIENT_H
2.2 tcp_Client.cpp
#include "tcp_client.h"
#include "ui_tcp_client.h"
#include
#include
#include
tcp_Client::tcp_Client(QWidget *parent) :
QWidget(parent),
ui(new Ui::tcp_Client)
{
ui->setupUi(this);
m_tcpClientSocket = new QTcpSocket;//实例化
ui->lineEdit_PORT->setText("9000");
ui->lineEdit_IP->setText("192.168.1.106");//设置默认值
m_tcpClientSocket->abort();//连接前 最后先清除已有连接
//点击链接按钮进行连接
connect(ui->pushButton_Connect, &QPushButton::clicked, this, [=](){
m_tcpClientSocket->connectToHost(QHostAddress(ui->lineEdit_IP->text()), ui->lineEdit_PORT->text().toInt());
ui->textEdit_Recv->setText("连接服务器成功!");
});
//读取数据
connect(m_tcpClientSocket, &QTcpSocket::readyRead, this, [=]()
{
QByteArray array = m_tcpClientSocket->readAll();
ui->textEdit_Recv->append(array);//在接受框中追加
});
//发送数据
connect(ui->pushButton_Send, &QPushButton::clicked, this, [=](){
m_tcpClientSocket->write("Client say:" + ui->textEdit_Send->toPlainText().toUtf8());
//ui->textEdit_Send->clear();
});
}
tcp_Client::~tcp_Client()
{
delete ui;
}
//清除发送框中的消息记录
void tcp_Client::on_pushButton_Clear_clicked()
{
ui->textEdit_Send->clear();
}
3. UI
3.1 服务器端
3.2 客户端
4. 运行效果
待完善:
1.获取客户端的ip和端口
2.利用多线程改为并发