QT实现TCP服务器客户端搭建的代码,现象

1.效果

2.服务器:

   2.1:ui界面

2.2:头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QList>
#include <QMessageBox>
#include <QDebug>

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_pushButton_clicked();

    void newConnection_slot();

    void readyRead_slot();

private:
    Ui::Widget *ui;
    QTcpServer *server;
    QList<QTcpSocket *> clientList;
};
#endif // WIDGET_H

2.3:cpp文件

main:

#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

函数封装:

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    server=new QTcpServer(this);

    connect(server,&QTcpServer::newConnection,this,&Widget::newConnection_slot);
}

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


void Widget::on_pushButton_clicked()
{
    if(ui->pushButton->text()=="启动")
    {
        ui->pushButton->setText("关闭");
        quint16 port=ui->lineEdit->text().toUInt();
        if(!server->listen(QHostAddress::Any,port))
        {
            QMessageBox::information(this,"失败","监听失败");
        }else
        {
            QMessageBox::information(this,"成功","服务器启动成功");
        }
    }else{
        for(int i=0;i<clientList.size();i++)
        {
            clientList.removeAt(i);
        }
        server->close();
        ui->pushButton->setText("启动");
    }
}

void Widget::newConnection_slot()
{
    qDebug()<<"您有新的客户端发来了连接请求了";
    QTcpSocket *s=server->nextPendingConnection();
    clientList.push_back(s);
    connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);

}

void Widget::readyRead_slot()
{
   qDebug()<<"有新的客户端信息发来了";
   for(int i=0;i<clientList.size();i++)
   {
      if(clientList[i]->state()==0)
      {
          clientList.removeAt(i);
      }
   }

   for(int i=0;i<clientList.size();i++)
   {
       if(clientList[i]->bytesAvailable()!=0)
       {
           QByteArray msg=clientList[i]->readAll();
           ui->msgWidget->addItem(QString::fromLocal8Bit(msg));
           for(int j=0;j<clientList.size();j++)
           {
               clientList[j]->write(msg);
           }
       }
   }
}























2.4:pro文件

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#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

3.客户端:

   3.1:ui界面

3.2:头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpSocket>
#include <QMessageBox>

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_pushButton_2_clicked();
    void connect_slot();
    void readyRead_slot();

    void on_pushButton_1_clicked();

    void on_pushButton_3_clicked();
    void disconnected_slot();

private:
    Ui::Widget *ui;
    QTcpSocket *socket;
    QString userName;
};
#endif // WIDGET_H

3.3:cpp文件

main:

#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

函数封装:

#include "widget.h"
#include "ui_widget.h"

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

    connect(socket,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);

    connect(socket,&QTcpSocket::disconnected,this,&Widget::disconnected_slot);
}

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


void Widget::on_pushButton_2_clicked()
{
    userName=ui->lineEdit_2->text();
    QString ip=ui->lineEdit_3->text();
    quint16 port=ui->lineEdit_4->text().toUInt();
    socket->connectToHost(ip,port);
}

void Widget::connect_slot()
{
    QMessageBox::information(this,"成功","您已经成功进入聊天室");

    QString msg=userName +":进入聊天室";
    socket->write(msg.toLocal8Bit());
}

void Widget::readyRead_slot()
{
    QByteArray msg=socket->readAll();
    ui->listWidget->addItem(QString::fromLocal8Bit(msg));
}

void Widget::on_pushButton_1_clicked()
{
    QString msg =userName+":"+ui->lineEdit->text();
    socket->write(msg.toLocal8Bit());
    ui->lineEdit->clear();
}

void Widget::on_pushButton_3_clicked()
{
    QString msg=userName+":离开聊天室";
    socket->write(msg.toLocal8Bit());
    socket->disconnectFromHost();
}

void Widget::disconnected_slot()
{
    QMessageBox::information(this,"提示","退出成功");
}



3.4:pro文件

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#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

### 回答1: 在使用Qt搭建服务器客户端实现直播功能时,主要需要以下几个步骤: 1. 服务器端的搭建:首先,需要通过Qt创建一个服务器应用程序,为客户端提供直播数据流的传输。使用Qt的网络库类,如QTcpServer和QTcpSocket,可以实现服务器的监听和接受客户端连接请求的功能。服务器端还需使用Qt的多线程类,如QThread,来处理多个客户端连接和直播数据的发送。 2. 客户端搭建:通过Qt创建一个客户端应用程序,用于接收服务器端传来的直播数据流并进行播放。使用Qt的网络库类,如QTcpSocket,客户端能够连接服务器,并接受直播数据流。客户端还可以使用Qt的多媒体类,如QMediaPlayer,来播放从服务器传来的直播数据。 3. 直播数据的传输:一般使用TCP协议来传输直播数据。服务器端将直播数据通过TCP socket发送给已连接的客户端,而客户端则通过TCP socket接收和解析这些数据,并使用多媒体类来播放直播内容。 4. 直播功能的实现:根据需求,还可以在程序中加入一些附加功能,例如客户端的聊天室、礼物赠送、直播间管理等功能。这些功能可以通过Qt的信号与槽机制、套接字通信及数据库的操作等方式来实现。 总体而言,通过使用Qt的网络库类和多媒体类,可以较为方便地搭建服务器客户端实现直播功能。在搭建过程中,需要注意处理多个连接和数据流传输的并发性,同时也需要考虑网络稳定性和性能优化等因素。 ### 回答2: 搭建服务器客户端实现直播,我们可以使用Qt网络模块来实现。 首先,在服务器端,我们需要创建一个TCP服务器。我们可以使用`QTcpServer`类来创建一个服务器,然后使用`listen`函数指定服务器的地址和端口号。当有客户端连接到服务器时,服务器会自动调用`newConnection`信号槽。 在`newConnection`槽函数中,我们可以创建一个`QTcpSocket`对象来处理与客户端的通信。我们可以使用`write`函数向客户端发送直播数据,例如视频流。同时,我们还可以使用`readyRead`信号槽来接收客户端发送的消息或命令。 在客户端,我们需要创建一个TCP客户端来连接到服务器。我们可以使用`QTcpSocket`类来创建一个客户端,然后使用`connectToHost`函数指定服务器的地址和端口号。当成功连接到服务器后,我们可以使用`connected`信号槽来进行后续操作。 在客户端中,我们可以使用`readyRead`信号槽来接收服务器发送的直播数据。我们还可以使用`write`函数向服务器发送消息或命令。 当我们的服务器客户端完成基本的连接和通信设置后,我们可以在服务器端使用多线程来处理多个客户端的连接和直播数据的传输。这样,我们就可以实现多个客户端同时观看直播。 总结起来,通过使用Qt的网络模块,我们可以搭建一个简单的服务器客户端系统来实现直播功能。服务器端负责接收客户端连接,并发送直播数据,而客户端则负责连接服务器并接收直播数据。这样我们就可以实现通过搭建服务器客户端实现直播的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值