client.pro
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void read_data();
private slots:
void on_connect_btn_clicked();
void on_send_btn_clicked();
private:
Ui::Widget *ui;
QTcpSocket *client;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include
#include
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("client");
this->setWindowIcon(QIcon("./client.png"));
//实例化
this->client=new QTcpSocket(this);
//信号与槽函数的关联
connect(this->client,&QTcpSocket::connected,this,[=]()
{
QMessageBox::information(0,"客户端","连接成功");
});
connect(this->client,&QTcpSocket::readyRead,this,&Widget::read_data);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_connect_btn_clicked()
{
//获取ip和port
QString ip=this->ui->ip_edit->text();
int port=this->ui->port_edit->text().toUInt();
//连接
this->client->connectToHost(QHostAddress(ip),port);
};
widget.ui
server.pro
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void read_data();
private slots:
void on_send_btn_clicked();
private:
Ui::Widget *ui;
QTcpServer *server;
QTcpSocket *client;
};
#endif // WIDGET_H
widget.cpp
注意:ip号查看自己的电脑配置
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//设置窗口标题
this->setWindowTitle("server");
//实例化服务器套接字
this->server = new QTcpServer(this);
//创建监听队列并绑定ip和port
this->server->listen(QHostAddress("192.168.2.107"), 9999);
//等待客户端连接==》本质就是等信号产生 信号与槽函数关联
connect(this->server, &QTcpServer::newConnection, this,
[=]()
{
//连接客户端
this->client = this->server->nextPendingConnection();
//收消息的信号与槽函数的关联
connect(this->client, &QTcpSocket::readyRead, this, &Widget::read_data);
});
}
Widget::~Widget()
{
delete ui;
}
//读数据
void Widget::read_data()
{
//从当前的这个客户端读缓存区中读数据
QByteArray data = this->client->readAll();
//把读取到的数据追加写入到 接收文本框中
this->ui->recv_edit->append(data);
}
//发数据
void Widget::on_send_btn_clicked()
{
//获取到发送文本框中的内容
QString data = this->ui->send_edit->text();
//写给客户端
this->client->write(data.toUtf8());
//清空发送文本框中的内容
this->ui->send_edit->clear();
}
widget.ui