在Qt中实现TCP/IP
服务器端通信流程:
1:创建套接字
2:将套接字设置为监听模式
3:等待并接受客户端请求
可以通过QTcpServer提供的void newConnection()信号来检测是否有连接请求,如果有可以在对应的槽函数中调用nextPendingConnection函数获取到客户端的Socket信息(返回值为QTcpSocket*类型指针),通过此套接字与客户端之间进行通信。
4:接收或者向客户端发送数据
5:接收数据:使用read()或者readAll()函数
6:发送数据:使用write()函数
客户端通信流程:
1:创建套接字
2:连接服务器
可以使用QTcpSocket类的connectToHost()函数来连接服务器。
3:向服务器发送或者接受数据
下面例子为简单的TCP/IP通信的实现:
服务器代码cpp:
#include "severwidget.h"
#include "ui_severwidget.h"
SeverWidget::SeverWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::SeverWidget)
{
ui->setupUi(this);
this->setWindowTitle("服务器:端口8888");
tcpsocket = NULL;
tcpserver = NULL;
/*服务器流程*/
//监听套接字
tcpserver = new QTcpServer(this); //内存回收机制,在执行析构函数时自动释放内存
//监听(blind + listen) 第一个参数代表当前网卡所有的端口
tcpserver->listen(QHostAddress::Any,8888); //参数端口号只要跟客户端 一致即可
//连接成功会触发new connect信号
connect(tcpserver,&QTcpServer::newConnection,[=](){
//取出建立好连接的套接字
tcpsocket = tcpserver->nextPendingConnection();
//获取对方的ip和端口
QString ip = tcpsocket->peerAddress().toString(); //ip
qint16 port = tcpsocket->peerPort(); //port
QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port);
//将temp数据显示在窗口上
ui->textEditRead->setText(temp);
//服务器接收数据,当服务器接收到数据时,会触发一个readready的信号
connect(tcpsocket,&QTcpSocket::readyRead,[=](){
//从通信套接字中取出内容
QByteArray array= tcpsocket->readAll();
ui->textEditRead->append(array); //append 追加
});
});
}
SeverWidget::~SeverWidget()
{
delete ui;
}
void SeverWidget::on_btn_Send_clicked()
{
if(NULL == tcpsocket)
{
return;
}
//获取编辑区的内容
QString str = ui->textEditWrite->toPlainText();
//给对方发送套接字socket
tcpsocket->write(str.toUtf8().data());
}
void SeverWidget::on_btn_Close_clicked()
{
if(NULL == tcpsocket)
{
return;
}
//主动和客户端断开连接
tcpsocket->disconnectFromHost();
tcpsocket->close();
tcpsocket =NULL;
}
客户端cpp代码:
#include "clientwidget.h"
#include "ui_clientwidget.h"
#include <QHostAddress>
ClientWidget::ClientWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ClientWidget)
{
ui->setupUi(this);
this->setWindowTitle("客户端");
Tcpsocket = NULL;
Tcpsocket = new QTcpSocket(this);
/*监听connect信号,当与服务器连接成功时,显示连接成功在窗口上*/
connect(Tcpsocket,&QTcpSocket::connected,[=](){
ui->textEditRead->append("成功和服务器连接");
});
/*监听数据接收信号,当服务器发数据过来时,会触发一个readyread的信号,我们监听这个信号*/
connect(Tcpsocket,&QTcpSocket::readyRead,[=](){
//将接收到的数据显示在窗口上
QByteArray array = Tcpsocket->readAll();
ui->textEditRead->append(array); //追加 不要用settext,会覆盖原来的数据
});
}
ClientWidget::~ClientWidget()
{
delete ui;
}
void ClientWidget::on_btn_connect_clicked()
{
//获取服务器ip和端口
QString ip = ui->lineEditIP->text();
qint16 port = ui->lineEditPort->text().toInt();
//主动和服务器连接
Tcpsocket->connectToHost(QHostAddress(ip),port);
}
void ClientWidget::on_btn_send_clicked()
{
//获取编辑框里的内容
QString str = ui->textEditWrite->toPlainText();
//将获取到的数据发送给服务器
Tcpsocket->write(str.toUtf8().data());
}
void ClientWidget::on_pushButton_3_clicked()
{
//主动断开连接,
Tcpsocket->disconnectFromHost();
Tcpsocket->close();
ui->textEditRead->append("断开连接");
}
最终运行结果:
本例子可实现tcp服务器和tcp客户端双向通信。
本例子的源码下载地址:源码下载地址https://download.csdn.net/download/weixin_38931060/12683129