QT TCP服务器/客户端

服务器

首先要在.pro文件中添加network,否则将不能使用QTcpserver

cpp 复制代码
QT       += core gui network
cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#define PORT 8000

QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void newClientHandler();
    void clientInfoSlot();

    void on_sendButton_clicked();

private:
    Ui::Widget *ui;
    QTcpServer *server;
    QTcpSocket *socket;
};
#endif // WIDGET_H
cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    server = new QTcpServer;
    server->listen(QHostAddress::AnyIPv4,PORT);
    connect(server,&QTcpServer::newConnection,this,&Widget::newClientHandler);
}

Widget::~Widget()
{
    delete ui;
}
void Widget::newClientHandler()
{
    //建立TCP连接
    socket = server->nextPendingConnection();
    ui->addresslineEdit->setText(socket->peerAddress().toString());
    ui->portlineEdit->setText(QString::number(socket->peerPort()));
    connect(socket,&QTcpSocket::readyRead,this,&Widget::clientInfoSlot);

}
void Widget::clientInfoSlot()
{
    //获取信号的发出者
    QTcpSocket *socket=(QTcpSocket *)sender();
    ui->revlineEdit->setText(QString(socket->readAll()));
}

void Widget::on_sendButton_clicked()
{
    QByteArray ba;
    ba.append(ui->sendlineEdit->text().toLatin1());
    socket->write(ba);
}

客户端

同样的,别忘了在.pro文件中修改

cpp 复制代码
QT       += core gui network
cpp 复制代码
#ifndef CHAT_H
#define CHAT_H

#include <QWidget>
#include<QTcpSocket>
namespace Ui {
class chat;
}

class chat : public QWidget
{
    Q_OBJECT

public:
    explicit chat(QTcpSocket *socket,QWidget *parent = nullptr);
    ~chat();

private slots:
    void on_sendButton_clicked();

    void on_clearButton_clicked();

    void clientReadHandler();

private:
    Ui::chat *ui;
    QTcpSocket *socket;
};

#endif // CHAT_H
cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QTcpsocket>
#include<QHostAddress>
#include<QMessageBox>
#include "chat.h"
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_cancelButton_clicked();

    void on_connectButton_clicked();

private:
    Ui::Widget *ui;
    QTcpSocket *socket;
    chat *ch;
};
#endif // WIDGET_H
cpp 复制代码
#include "chat.h"
#include "ui_chat.h"

chat::chat(QTcpSocket *s,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::chat)
{
    ui->setupUi(this);
    socket = s;
    connect(socket,&QTcpSocket::readyRead,this,&chat::clientReadHandler);
}

chat::~chat()
{
    delete ui;
}

void chat::on_sendButton_clicked()
{
    QByteArray ba;
    ba.append(ui->lineEdit->text().toLatin1());
    socket->write(ba);
}


void chat::on_clearButton_clicked()
{
    ui->lineEdit->clear();
}

void chat::clientReadHandler()
{
    ui->revlineEdit->setText(QString(socket->readAll()));
}
cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    socket = new QTcpSocket;
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_connectButton_clicked()
{
    QString ip = ui->iplineEdit->text();
    QString port = ui->portlineEdit->text();
    qDebug() << ip;
    qDebug() << port;

    socket->connectToHost(QHostAddress(ip),port.toShort());
    connect(socket,&QTcpSocket::connected,[this]{
        QMessageBox::information(this,"Tip","连接成功");
        this->hide();
        ch = new chat(socket);
        ch->show();
    });
    connect(socket,&QTcpSocket::disconnected,[this]{
        QMessageBox::information(this,"Tip","连接失败!");
    });
}

void Widget::on_cancelButton_clicked()
{
    this->close();
}


相关推荐
秋名山小桃子16 分钟前
Kunlun 2280服务器(ARM)Raid卡磁盘盘符漂移问题解决
运维·服务器
与君共勉1213817 分钟前
Nginx 负载均衡的实现
运维·服务器·nginx·负载均衡
努力学习的小廉24 分钟前
深入了解Linux —— make和makefile自动化构建工具
linux·服务器·自动化
MZWeiei27 分钟前
Zookeeper基本命令解析
大数据·linux·运维·服务器·zookeeper
7yewh43 分钟前
嵌入式Linux QT+OpenCV基于人脸识别的考勤系统 项目
linux·开发语言·arm开发·驱动开发·qt·opencv·嵌入式linux
Arenaschi1 小时前
在Tomcat中部署应用时,如何通过域名访问而不加端口号
运维·服务器
小张认为的测试1 小时前
Linux性能监控命令_nmon 安装与使用以及生成分析Excel图表
linux·服务器·测试工具·自动化·php·excel·压力测试
waicsdn_haha1 小时前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
良许Linux1 小时前
0.96寸OLED显示屏详解
linux·服务器·后端·互联网
蜜獾云1 小时前
docker 安装雷池WAF防火墙 守护Web服务器
linux·运维·服务器·网络·网络安全·docker·容器