使用QT实现简单的tcp/ip通信
小编:啊南 428阅读 2021.01.15
使用QT实现tcp/ip的通信非常的简单,下面我将直接贴出代码:
一、服务器端
1、头文件
#ifndef SERVERWIDGET_H
#define SERVERWIDGET_H
#include
#include
#include
namespace Ui {
class ServerWidget;
}
class ServerWidget : public QWidget
{
Q_OBJECT
public:
explicit ServerWidget(QWidget *parent = 0);
~ServerWidget();
private slots:
void on_buttonSend_clicked();
void on_buttonClose_clicked();
private:
Ui::ServerWidget *ui;
//监听套接字
QTcpServer *tcpServer;
//通信套接字
QTcpSocket *tcpSocket;
};
#endif // SERVERWIDGET_H
2、cpp文件
#include "serverwidget.h"
#include "ui_serverwidget.h"
ServerWidget::ServerWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ServerWidget)
{
ui->setupUi(this);
//初始化套接字指针
tcpServer = NULL;
tcpSocket = NULL;
setWindowTitle("服务器:8899");
//监听套接字,指定父对象,让其自动回收空间
this->tcpServer = new QTcpServer(this);
//监听客户端的请求
this->tcpServer->listen(QHostAddress::Any,8899);
connect(this->tcpServer, &QTcpServer::newConnection, [=](){
//取出建立好链接的套接字
this->tcpSocket = this->tcpServer->nextPendingConnection();
//获取对方的IP和端口
QString userIp = this->tcpSocket->peerAddress().toString();
qint16 userPort = this->tcpSocket->peerPort();
QString temp = QString("[%1:%2]:连接成功").arg(userIp).arg(userPort);
ui->textEditRead->setText(temp);
//接收数据
connect(this->tcpSocket,&QTcpSocket::readyRead,[=](){
//从通信套接字中取出内容
QByteArray array = tcpSocket->readAll();
ui->textEditRead->append(array);
});
});
}
ServerWidget::~ServerWidget()
{
delete ui;
}
//发送
void ServerWidget::on_buttonSend_clicked()
{
if(tcpSocket == NULL){
return;
}
//获取编辑区的内容
QString str = ui->textEditWrite->toPlainText();
//给对方发送数据,使用的套接字是tcpSocket
tcpSocket->write(str.toUtf8().data());
}
//关闭按钮
void ServerWidget::on_buttonClose_clicked()
{
if(tcpSocket == NULL){
return;
}
//主动和客户端断开 链接
tcpSocket->disconnectFromHost();
tcpSocket->close();
}
二、客户端
1、头文件
#ifndef CLIENTWIDGET_H
#define CLIENTWIDGET_H
#include
#include
namespace Ui {
class ClientWidget;
}
class ClientWidget : public QWidget
{
Q_OBJECT
public:
explicit ClientWidget(QWidget *parent = 0);
~ClientWidget();
private slots:
void on_buttonConnect_clicked();
void on_buttonSend_clicked();
void on_buttonClose_clicked();
private:
Ui::ClientWidget *ui;
//通信套接字
QTcpSocket *tcpSocket;
};
#endif // CLIENTWIDGET_H
2、cpp文件
#include "clientwidget.h"
#include "ui_clientwidget.h"
#include
ClientWidget::ClientWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ClientWidget)
{
ui->setupUi(this);
setWindowTitle("客户端");
//初始化套接字
tcpSocket = NULL;
//分配空间,指定父对象
tcpSocket = new QTcpSocket(this);
//建立连接
connect(tcpSocket,&QTcpSocket::connected,[=](){
ui->textEditRead->setText("连接服务器成功");
});
//接收数据
connect(tcpSocket,&QTcpSocket::readyRead,[=](){
//获取对方发送的内容
QByteArray array = tcpSocket->readAll();
//追加到编辑区中
ui->textEditRead->append(array);
});
}
ClientWidget::~ClientWidget()
{
delete ui;
}
//连接服务器
void ClientWidget::on_buttonConnect_clicked()
{
if(tcpSocket != NULL){
return;
}
//获取服务器IP和端口
QString ip = ui->lineEditIp->text();
qint16 port = ui->lineEditPort->text().toInt();
//主动和服务器建立链接
tcpSocket->connectToHost(QHostAddress(ip),port);
}
//发送数据
void ClientWidget::on_buttonSend_clicked()
{
if(tcpSocket == NULL){
return;
}
//获取编辑框内容
QString str = ui->textEditWrite->toPlainText();
//发送数据
tcpSocket->write(str.toUtf8().data());
}
//断开与服务器的连接
void ClientWidget::on_buttonClose_clicked()
{
if(tcpSocket == NULL){
return;
}
tcpSocket->disconnectFromHost();
tcpSocket->close();
}
三、实现效果图如下(程序中对应的控件对象名在下面可以直接对照,控件不多,就不一一列出来了):
相关推荐
- Qt加载XPM图像 XPM是一种基于ASCII编码的图像格式,一般用于创建图标。由于它是ASCII编码,很方便地在代码中使用。 Qt的QPixmap类支持XPM格式,原型如下:QPixmap::QPixmap(const char *const [] xpm)摘取Qt源码中的"严重警告"XPM图标:static const char* const c…
- 3DMAX提示和技巧 本主题标识使用 Civil View 的一些重要提示和技巧。常规使用屏幕分辨率至少为 1280x1024 的 Civil View。低于此分辨率时,一些面板将占用过多屏幕空间。 将视口设置为线框显示以达到最佳性能。 要尽可能简化用户界面,请在单个视口中工作并关闭 3ds Max 命令面…