QT開發---網絡編程上

Qt Network 模塊

Qt Network 模塊提供了豐富的類用于實現各種網絡通信功能,涵蓋 TCP、UDP、HTTP、FTP 等多種協議。?

Qt 網絡類均為異步操作,通過信號槽處理結果,避免阻塞 UI 線程。

在使用QT進行網絡編程之前,就必須在 CMakeLists.txt 中顯式啟用并鏈接 Qt6::Network

CMake:

find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)

Qt Network 模塊最常用的核心類及其主要用途:

基礎網絡類

  • QHostAddress

    • 表示 IP 地址(支持 IPv4 和 IPv6),提供地址解析、轉換和比較功能
    • 常用場景:指定服務器地址、客戶端連接目標地址等
  • QNetworkInterface

    • 提供本地網絡接口(如網卡)的信息,包括 IP 地址、子網掩碼、MAC 地址等
    • 常用方法:allInterfaces()?獲取所有網絡接口,addressEntries()?獲取接口的 IP 地址列表

TCP 通信類

  • QTcpSocket

    • TCP 客戶端套接字,用于與 TCP 服務器建立連接并進行可靠的數據傳輸
    • 核心信號:connected()(連接成功)、readyRead()(收到數據)、disconnected()(連接斷開)
    • 核心方法:connectToHost()(連接服務器)、write()(發送數據)、readAll()(讀取數據)
  • QTcpServer

    • TCP 服務器類,用于監聽端口并接受客戶端連接
    • 核心信號:newConnection()(有新客戶端連接)
    • 核心方法:listen()(開始監聽)、nextPendingConnection()(獲取新連接的套接字)

UDP 通信類

  • QUdpSocket
    • 用于 UDP 協議的無連接數據報通信,適用于實時性要求高的場景(如語音、視頻)
    • 核心信號:readyRead()(收到數據報)
    • 核心方法:bind()(綁定端口)、writeDatagram()(發送數據報)、readDatagram()(接收數據報)

HTTP / 網絡請求類

  • QNetworkAccessManager

    • 管理網絡請求的核心類,支持 HTTP、HTTPS、FTP 等協議,可發送 GET、POST 等請求
    • 核心方法:get()(發送 GET 請求)、post()(發送 POST 請求)、head()(發送 HEAD 請求)
    • 核心信號:finished(QNetworkReply*)(請求完成)
  • QNetworkRequest

    • 封裝網絡請求的信息,如 URL、請求頭(Header)、超時設置等
    • 常用方法:setUrl()(設置請求 URL)、setHeader()(設置請求頭,如 Content-Type)
  • QNetworkReply

    • 表示網絡請求的響應,包含服務器返回的數據、狀態碼、錯誤信息等
    • 核心信號:readyRead()(有數據可讀取)、downloadProgress()(下載進度)
    • 核心方法:readAll()(讀取響應數據)、error()(獲取錯誤信息)、attribute()(獲取響應屬性,如狀態碼)

域名解析類

  • QHostInfo
    • 用于域名解析(將域名轉換為 IP 地址),支持異步和同步解析
    • 常用方法:lookupHost()(異步解析域名)、fromName()(同步解析域名)
    • 解析結果通過?QHostInfo?對象返回,包含解析到的?QHostAddress?列表

網絡配置類

  • QNetworkConfiguration

    • 表示網絡配置(如 Wi-Fi、有線網絡),包含網絡的狀態和類型信息
  • QNetworkConfigurationManager

    • 管理系統中的網絡配置,可獲取可用網絡、監聽網絡狀態變化
    • 核心信號:configurationChanged()(網絡配置變化)、onlineStateChanged()(在線狀態變化)

SSL/TLS 安全通信類

  • QSslSocket
    • 繼承自?QTcpSocket,支持 SSL/TLS 加密通信,用于 HTTPS、FTPS 等安全協議
    • 核心方法:startClientEncryption()(啟動客戶端加密)、setCaCertificates()(設置 CA 證書)

QHostAddress類

This class holds an IPv4 or IPv6 address in a platform- and protocol-independent manner.
QHostAddress is normally used with the QTcpSocket, QTcpServer, and QUdpSocket to connect to a host or to set up a server.
A host address is set with setAddress(), and retrieved with toIPv4Address(), toIPv6Address(), or toString(). You can check the type with protocol().

Note: Please note that QHostAddress does not do DNS lookups. QHostInfo is needed for that.

The class also supports common predefined addresses: Null, LocalHost, LocalHostIPv6, Broadcast, and Any.

該類以獨立于平臺和協議的方式保存IPv4或IPv6地址。

QHostAddress通常與QTcpSocket、QTcpServer和QUdpSocket一起使用,用于連接主機或設置服務器。

主機地址用setAddress()設置,用toIPv4Address()、toIPv6Address()或toString()檢索。您可以使用protocol()檢查類型。

注意:請注意,QHostAddress不做DNS查找。為此需要QHostInfo。

該類還支持常見的預定義地址:Null、LocalHost、LocalHostIPv6、Broadcast和Any。

TCP 通信類

QTcpSocket類

TCP (Transmission Control Protocol) is a reliable, stream-oriented, connection-oriented transport protocol. ?It is especially well suited for continuous transmission of data
QTcpSocket is a convenience subclass of QAbstractSocket that allows you to establish a TCP connection and transfer streams of data

TCP(傳輸控制協議)是一種可靠的、面向流的、面向連接的傳輸協議。它特別適合于數據的連續傳輸

QTcpSocket是QAbstractSocket的一個便捷的子類,它允許您建立TCP連接并傳輸數據流

連接狀態標志 :

peerAddress()	返回服務器的 IP 地址(QHostAddress 類型)
peerPort()	返回服務器的端口號(quint16 類型)
localAddress()	返回本地綁定的 IP 地址(QHostAddress 類型)
localPort()	返回本地綁定的端口號(quint16 類型)
connectToHost(const QString &host, quint16 port)連接到指定主機(支持域名或 IP)和端口(異步操作)
connectToHost(const QHostAddress &address, quint16 port)連接到指定 QHostAddress 表示的 IP 和端口
disconnectFromHost()主動斷開連接(發送 FIN 包,優雅關閉,觸發 disconnected 信號)
abort()	強制終止連接(立即關閉,不發送 FIN 包,適合超時或錯誤處理)
connected()	成功連接到服務器時觸發
disconnected()	與服務器斷開連接時觸發
readyRead()	有數據可讀取時觸發(最常用,需配合 readAll() 等方法讀取)
bytesWritten(qint64 bytes)	數據寫入底層緩沖區后觸發(bytes 為實際寫入的字節數)
errorOccurred(QAbstractSocket::SocketError error)	發生錯誤時觸發(error 為錯誤類型枚舉)
stateChanged(QAbstractSocket::SocketState state)	連接狀態變化時觸發(如從連接中變為已連接QAbstractSocket::SocketError 枚舉	
常見錯誤類型:
- ConnectionRefusedError:連接被拒絕
- HostNotFoundError:主機未找到
- TimeoutError:超時
- RemoteHostClosedError:服務器主動關閉

QTcpServer類

QTcpServer類使得接受傳入的TCP連接成為可能。您可以指定端口,也可以讓QTcpServer自動選擇一個。你可以監聽一個特定的地址,也可以監聽所有機器的地址。

調用listen()讓服務器偵聽傳入的連接。然后,每次客戶端連接到服務器時都會發出newConnection()信號。當使用addPendingConnection()函數將客戶端連接添加到掛起連接隊列中時,將發出pendingConnectionAvailable()信號。

調用nextPendingConnection()將掛起的連接作為已連接的QTcpSocket接受。該函數返回QAbstractSocket::ConnectedState中指向QTcpSocket的指針,您可以使用該指針與客戶端進行通信。

如果發生錯誤,serverError()返回錯誤的類型,并且可以調用errorString()來獲得關于發生的事情的人類可讀的描述。

當監聽連接時,服務器正在監聽的地址和端口可以作為serverAddress()和serverPort()使用。

調用close()使QTcpServer停止偵聽傳入的連接。

雖然QTcpServer主要是為與事件循環一起使用而設計的,但是不使用事件循環也可以使用它。在這種情況下,必須使用waitForNewConnection(),它會阻塞,直到連接可用或超時過期。

isListening()	返回服務器是否正在監聽端口(bool 類型)
serverAddress()	返回服務器監聽的 IP 地址(QHostAddress 類型,如 QHostAddress::Any)
serverPort()	返回服務器監聽的端口號(quint16 類型,未監聽時返回 0)
maxPendingConnections()	返回最大等待處理的連接數(默認 30),超過則拒絕新連接
setMaximumPendingConnections(int num)	設置最大等待處理的連接數
bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0)
開始監聽指定地址和端口
- address:監聽的 IP 地址(Any 表示所有網絡接口)
- port:端口號(0 表示隨機分配)
close()	停止監聽并關閉所有已建立的連接(觸發客戶端 disconnected 信號)
extPendingConnection()	獲取下一個等待處理的客戶端連接(返回 QTcpSocket*)
需手動管理該對象生命周期(建議設置父對象)
hasPendingConnections()	判斷是否有等待處理的客戶端連接(bool 類型)
pendingConnections()	返回所有等待處理的連接列表(QList<QTcpSocket*>)
newConnection()	有新客戶端連接請求且已建立連接時觸發(需調用 nextPendingConnection() 獲取連接)
acceptError(QAbstractSocket::SocketError error)	接受連接發生錯誤時觸發(如超過最大連接數)
serverError(QAbstractSocket::SocketError error)	服務器發生錯誤時觸發(如監聽失敗)
QAbstractSocket::SocketError 枚舉	
錯誤類型:
- AddressInUseError:地址已被占用
- PermissionDeniedError:權限不足
- InvalidAddressError:無效地址

成果展示:??

模擬網絡調試助手,實現TCP客戶端與服務端交互

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QTcpSocket>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();void connectTCPService();private slots://接收數據void receiveServerMessage();//連接按鍵void on_connect_clicked();//斷開連接按鍵void on_disconnect_clicked();//發送按鍵void on_send_clicked();//清除發送框文本void on_Sclear_clicked();//清楚接收框文本void on_Rclear_clicked();private:Ui::MainWindow *ui;QTcpSocket *tcpsocket;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QString>
#include <QDebug>
#include <QDateTime>
#include <QNetworkProxy>
#include <QPlainTextEdit>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//創建TCPsocket對象tcpsocket = new QTcpSocket(this);//當發出readyDead信號connect(tcpsocket,&QTcpSocket::readyRead,this,&MainWindow::receiveServerMessage);connect(tcpsocket, &QTcpSocket::connected, this, [](){qDebug() << "連接服務器成功!";});connect(tcpsocket, &QTcpSocket::errorOccurred, this, [this](QAbstractSocket::SocketError err){qDebug() << "連接失敗:" << tcpsocket->errorString();});
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::connectTCPService()
{//獲取端口號和IP地址QString ip = ui->ip->toPlainText();QString port = ui->port->toPlainText();if(ip.isEmpty() || port.isEmpty()){qDebug() << "ip or port is empty!";return;}//進行鏈接QHostAddress hostAddress(ip);if(tcpsocket->state() == QAbstractSocket::UnconnectedState){// 關鍵:禁用代理,避免代理類型錯誤tcpsocket->setProxy(QNetworkProxy::NoProxy);tcpsocket->connectToHost(hostAddress,(quint16)port.toUInt());}
}
void MainWindow::receiveServerMessage()
{//獲取系統當前時間QString currenttime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz");//獲取IPQString TcpIPaddress = tcpsocket->peerAddress().toString();//獲取端口號quint16 TcpPort = tcpsocket->peerPort();//讀取數據QByteArray readdata =  tcpsocket->readAll();//與服務器約定并統一使用 UTF-8 編碼,可徹底解決中文接收亂碼問題QString result = QString("[%1]# SEND ASCII/82 to ALL CLIENTS >>>\n%4").arg(currenttime).arg(QString::fromUtf8(readdata));QString PlainText = ui->receive_text->toPlainText();if(!PlainText.isEmpty()){PlainText.append("\n");}PlainText.append(result);ui->receive_text->setPlainText(PlainText);
}
void MainWindow::on_connect_clicked()
{qDebug() << "connectclicked";connectTCPService();
}void MainWindow::on_disconnect_clicked()
{qDebug() << "disconnectclicked";//斷開連接if(tcpsocket->state() == QAbstractSocket::ConnectedState){tcpsocket->disconnectFromHost();}
}void MainWindow::on_send_clicked()
{QString currenttime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz");// 獲取本地IP和端口(客戶端自身)QString localIP = tcpsocket->localAddress().toString();quint16 localPort = tcpsocket->localPort();// 讀取發送框內容QString sendText = ui->send_text->toPlainText().trimmed();if (sendText.isEmpty()){//空內容不發送,可添加提示return;}// 按UTF-8編碼轉換為字節流發送QByteArray sendData = sendText.toUtf8();qint64 bytesSent = tcpsocket->write(sendData);/*//發送成功后更新界面顯示if (bytesSent != -1){QString result = QString("[%1]# RECV ASCII/82 from %2 :%3 <<<%4").arg(currenttime).arg(localIP).arg(localPort).arg(sendText);QString PlainText = ui->receive_text->toPlainText();if (!PlainText.isEmpty()){PlainText.append("\n");}PlainText.append(result);ui->receive_text->setPlainText(PlainText);}else{//發送失敗提示QString errorMsg = QString("[%1]# \n發送失敗: %2").arg(currenttime).arg(tcpsocket->errorString());ui->receive_text->setPlainText(errorMsg);}*/
}
void MainWindow::on_Sclear_clicked()
{// 清空發送框ui->send_text->clear();
}void MainWindow::on_Rclear_clicked()
{// 清空發送框ui->receive_text->clear();
}

實現?TCP客戶端與服務端交互,對文本框的清除效果

博主已經將項目文件壓縮上傳,若有感興趣的同學,可以下載使用?

UDP 通信類

QUdpSocket類

UDP (User Datagram Protocol) is a lightweight, unreliable, datagram-oriented, connectionless protocol. It can be used when reliability isn't important. QUdpSocket is a subclass of QAbstractSocket that allows you to send and receive UDP datagrams.
The most common way to use this class is to bind to an address and port using bind(), then call writeDatagram() and readDatagram() / receiveDatagram() to transfer data. If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling connectToHost().
The socket emits the bytesWritten() signal every time a datagram is written to the network. If you just want to send datagrams, you don't need to call bind().
The readyRead() signal is emitted whenever datagrams arrive. In that case, hasPendingDatagrams() returns true. Call pendingDatagramSize() to obtain the size of the first pending datagram, and readDatagram() or receiveDatagram() to read it.

UDP(用戶數據報協議)是一種輕量級、不可靠、面向數據報、無連接的協議。它可以在可靠性不重要的情況下使用。QUdpSocket是QAbstractSocket的一個子類,它允許您發送和接收UDP數據報。

使用QUdpSocket類最常見的方法是使用bind()綁定到一個地址和端口,然后調用writeDatagram()和readDatagram() / receiveDatagram()來傳輸數據。如果你想使用標準的QIODevice函數read(), readLine(), write()等,你必須首先通過調用connectToHost()將套接字直接連接到對等體。

套接字每次將數據報寫入網絡時都會發出bytesWritten()信號。如果您只想發送數據報,則不需要調用bind()。

每當數據報到達時,就會發出readyRead()信號。在這種情況下,hasPendingDatagrams()返回true。調用pendingDatagramSize()來獲取第一個掛起數據報的大小,并調用readDatagram()或receiveDatagram()來讀取它。

綁定端口
bool bind(const QHostAddress &address, quint16 port);
用于將套接字綁定到特定的地址和端口,以便接收數據。發送數據
qint64 writeDatagram(const QByteArray &datagram, 
const QHostAddress &host, quint16 port);
向指定的主機和端口發送 UDP 數據報。接收數據
qint64 readDatagram(char *data, qint64 maxSize, 
QHostAddress *host = nullptr, quint16 *port = nullptr);
從套接字中讀取接收到的數據報,并獲取發送方的地址和端口QNetworkDatagram  QUdpSocket::receiveDatagram(qint64 maxSize = -1) 
從套接字接收一個 UDP 數據報,并將其封裝為 QNetworkDatagram 對象返回
QNetworkDatagram 對象,包含數據內容、發送方地址、端口等信息信號
readyRead(): 當有數據報到達時觸發
errorOccurred(QAbstractSocket::SocketError): 發生錯誤時觸發

成果展示:?

#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QNetworkProxy>
#include <QDebug>
#include <QDateTime>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//創建TCPsocket對象udpsocket = new QUdpSocket(this);//當有數據可讀時觸發connect(udpsocket, &QUdpSocket::readyRead, this, &MainWindow::receiveMessage);//錯誤處理connect(udpsocket, &QUdpSocket::errorOccurred, this, [this](QAbstractSocket::SocketError err){QString errorMsg = QString("錯誤: %1").arg(udpsocket->errorString());qDebug() << errorMsg;});
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::connectUDP()
{//獲取端口號和IP地址QString ip = ui->IP->text();QString port = ui->Port->text();if(ip.isEmpty()){qDebug() << "ip or port is empty!";return;}//進行鏈接QHostAddress hostAddress(ip);if(udpsocket->state() == QAbstractSocket::UnconnectedState){// 關鍵:禁用代理,避免代理類型錯誤udpsocket->setProxy(QNetworkProxy::NoProxy);bool result = udpsocket->bind(hostAddress, (quint16)port.toUInt());if(result){qDebug() <<"綁定成功";}else{QString errorMsg = QString("綁定失敗: %1").arg(udpsocket->errorString());qDebug() << errorMsg;}}
}void MainWindow::receiveMessage()
{// 循環處理所有待接收的數據報while (udpsocket->hasPendingDatagrams()){//獲取系統當前時間QString currenttime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz");// 獲取待接收數據報的大小qint64 datagramSize = udpsocket->pendingDatagramSize();QByteArray rawData(datagramSize, 0); // 用 QByteArray 替代 char*,自動管理內存QHostAddress senderAddress;quint16 senderPort;//讀取數據報qint64 bytesRead = udpsocket->readDatagram(rawData.data(), datagramSize, &senderAddress, &senderPort);this->senderAddress = senderAddress;this->senderPort = senderPort;//與服務器約定并統一使用 UTF-8 編碼,可徹底解決中文接收亂碼問題QString result = QString("[%1]# SEND ASCII/12 to %2 :%3 >>>\n%4").arg(currenttime).arg(senderAddress.toString()).arg(senderPort).arg(QString::fromUtf8(rawData));QString PlainText = ui->receive_txt->toPlainText();if(!PlainText.isEmpty()){PlainText.append("\n");}PlainText.append(result);ui->receive_txt->setPlainText(PlainText);}
}void MainWindow::on_connect_clicked()
{qDebug() << "connectclicked";connectUDP();
}void MainWindow::on_disconnect_clicked()
{qDebug() << "disconnectclicked";udpsocket->close();ui->receive_txt->setPlainText(QString("已解除綁定,停止監聽\n"));
}void MainWindow::on_send_clicked()
{QString currenttime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz");// 獲取本地IP和端口(客戶端自身)QString localIP = udpsocket->localAddress().toString();quint16 localPort = udpsocket->localPort();// 讀取發送框內容QString sendText = ui->send_txt->toPlainText().trimmed();if (sendText.isEmpty()){//空內容不發送,可添加提示return;}// 按UTF-8編碼轉換為字節流發送QByteArray sendData = sendText.toUtf8();qint64 result =udpsocket->writeDatagram(sendData, this->senderAddress, this->senderPort);if(result == -1){//發送失敗qDebug() << "發送失敗:" << udpsocket->errorString();}else{//發送成功qDebug() << "成功發送" << result << "字節到" << this->senderAddress << ":" << this->senderPort;}
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>#include <QUdpSocket>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();void connectUDP();//接收數據void receiveMessage();//目標IP----可更改為數組,容器等形式,實現向多個端口發送QHostAddress senderAddress;//目標端口quint16 senderPort;
private slots:void on_connect_clicked();void on_disconnect_clicked();void on_send_clicked();private:Ui::MainWindow *ui;QUdpSocket *udpsocket;};
#endif // MAINWINDOW_H

結語:

無論你是初學者還是有經驗的開發者,我希望我的博客能對你的學習之路有所幫助。如果你覺得這篇文章有用,不妨點擊收藏,或者留下你的評論分享你的見解和經驗,也歡迎你對我博客的內容提出建議和問題。每一次的點贊、評論、分享和關注都是對我的最大支持,也是對我持續分享和創作的動力

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/90819.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/90819.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/90819.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[spring6: Mvc-函數式編程]-源碼解析

接口 ServerRequest public interface ServerRequest {HttpMethod method();URI uri();UriBuilder uriBuilder();default String path() {return requestPath().pathWithinApplication().value();}default RequestPath requestPath() {return ServletRequestPathUtils.getPar…

Linux DNS 服務器正反向解析

一、環境說明與準備工作 1.基礎信息 本次實驗用兩臺 Linux 主機&#xff0c;分別作為 DNS 服務端和客戶端&#xff0c;具體信息如下&#xff1a;服務端IP客戶端IP網址192.168.120.130192.168.120.128www.zy.com2.準備工作 關閉安全軟件&#xff1a;服務端和客戶端都要關閉防火墻…

歷史數據分析——中證旅游

中證旅游板塊走勢從月線級別來看2015年5月到2024年9月&#xff0c;月線上走出了一個震蕩中樞的月線級別下跌段&#xff1b;目前月線級別底部放巨量&#xff0c;總體還在底部震蕩&#xff0c;后續上漲的概率較大。從周線級別來看從2022年12月到2024年9月整體是下跌走勢&#xff…

OpHReda精準預測酶最佳PH

1.顯著改進&#xff1a;OpHReda通過檢索嵌入數據增強機制&#xff0c;顯著提高了酶最佳pH預測的準確性&#xff0c;相比現有方法提升了55%的F1分數。2.多尺度殘差輕注意力模塊&#xff1a;該模塊結合了殘差學習和多尺度特征提取&#xff0c;增強了模型對酶序列中殘差級信息的捕…

醫護行業在未來會被AI淘汰嗎?

隨著AI的迅速發展&#xff0c;似乎所有職業都有被AI替代的風險&#xff0c;那麼醫療領域作為一個高技術依賴性的行業&#xff0c;有機會被淘汰嗎?我們今天就來說說&#xff0c;幾乎不可能被AI淘汰的職業---護理。一) AI在護理中扮演的角色i.) 臨床工作支持1. 健康監測自動化即…

大語言模型加速技術之KV Cache

大語言模型加速技術之KV CacheWhy we need KV Cache &#xff1f;Self-Attention Without CacheSelf-Attention With CacheHuggingface 官方代碼實現Why we need KV Cache &#xff1f; 生成式generative模型的推理過程很有特點&#xff0c;我們給一個輸入文本&#xff0c;模型…

代碼隨想錄算法訓練營第五十三天|圖論part4

110.字符串接龍 題目鏈接&#xff1a;110. 字符串接龍文章講解&#xff1a;代碼隨想錄思路&#xff1a; 把每個字符串看成圖的一個節點。 轉換為求無權圖兩節點的的最短路徑。求最短路徑用bfs #include <string> #include <vector> #include <iostream> #i…

Java進階4:泛型、序列化和反序列化

Java泛型 Java泛型是JDK5引入的一個新的特性&#xff0c;泛型提供了編譯時的類型安全檢測機制&#xff0c;這個機制運行程序員在編譯的時候檢測到非法的類型。泛型的本質是參數化類型&#xff0c;也就是所操作的數據類型被指定為一個參數。 泛型方法 可以寫一個泛型方法&#x…

RAG實戰指南 Day 24:上下文構建與提示工程

【RAG實戰指南 Day 24】上下文構建與提示工程 文章內容 開篇 歡迎來到"RAG實戰指南"系列的第24天&#xff01;今天我們將深入探討RAG系統中至關重要的上下文構建與提示工程技術。在檢索增強生成系統中&#xff0c;如何有效地組織檢索到的文檔片段&#xff0c;并將…

AWD的攻擊和防御手段

一、AWD相關介紹 AWD&#xff08;Attack With Defence&#xff09;是 CTF 線下賽中最接近真實攻防場景、觀賞性和對抗性最強的賽制之一。 賽制本質 人人對抗&#xff1a;所有戰隊互為攻擊者與防守者。 零和記分&#xff1a;你拿到的每一分都是別人的失分&#xff0c;總積分恒…

泛微OA8前臺SQL注入

漏洞URL&#xff1a; http://106.15.190.147/js/hrm/getdata.jsp?cmdgetSelectAllId&sql***注入點 在getdata.jsp中&#xff0c;直接將request對象交給 weaver.hrm.common.AjaxManager.getData(HttpServletRequest, ServletContext) : 方法處理 在getData方法中&#xff0…

Android 藍牙學習

在Android中&#xff0c;進行藍牙設備掃描startDiscovery需要申請位置權限&#xff0c;但有的時候并不需要申請位置權限&#xff0c;就有了android:usesPermissionFlags"neverForLocation"&#xff0c;設置這個就不用申請位置權限來進行藍牙掃描。 android:usesPerm…

Earth靶機攻略

一.環境準備 1.1Earth靶機環境準備 首先將我們解壓好的的Earth.ova放入虛擬機里&#xff0c;并配置環境 將網絡連接換成NET連接 二.信息搜集 2.1ip搜集 2.1.1使用netdiscover命令掃描靶機的ip地址,發現地址為192.168.182.137 2.2端口掃描 2.2.1使用nmap工具對目標機進行端…

java8 List常用基本操作(去重,排序,轉換等)

參考簡述網址: java8 List 根據對象某個字段或多個字段去重、篩選、List轉Map、排序、分組、統計計數等等 list簡單方法示例如下&#xff1a; 一、先定義一個訂單對象&#xff08;Order&#xff09; public class Order {private Long id;private Long userId;private Strin…

【linux】HAProxy七層代理

HAPorxy簡介HAProxy&#xff08;High Availability Proxy&#xff09;是一款高性能、開源的負載均衡器與反向代理服務器&#xff0c;主要用于 TCP 和 HTTP 協議的流量分發&#xff0c;廣泛應用于高并發、高可用的網絡架構中核心特性超高性能與低資源消耗基于單進程、多線程模型…

Android 的16 KB內存頁設備需要硬件支持嗎,還是只需要手機升級到Android15系統就可以

Android 的16 KB內存頁設備需要硬件支持嗎&#xff0c;還是只需要手機升級到Android15系統就可以&#xff1f; deepseek回答&#xff1a; DeepSeek R1 思考 3 秒 思考過程 好的&#xff0c;用戶現在問的是Android的16 KB內存頁設備是否需要硬件支持&#xff0c;還是只需要升級到…

相機內外參矩陣:從3D世界坐標到2D像素坐標變換

相機內外參矩陣&#xff1a;從3D世界坐標到2D像素坐標變換介紹**1. 內參矩陣&#xff08;Intrinsic Matrix, K&#xff09;****2. 外參矩陣&#xff08;Extrinsic Matrix, [R|t]&#xff09;****3. 完整投影過程&#xff08;世界坐標 → 像素坐標&#xff09;****步驟1&#xf…

哈希指針與數據結構:構建可信數字世界的基石

一、哈希指針的核心原理哈希指針是一種創新型數據結構&#xff0c;融合了傳統指針的定位功能與密碼學哈希的驗證能力&#xff1a;雙重功能&#xff1a;既存儲數據地址&#xff0c;又包含該數據的哈希值&#xff0c;實現數據定位與完整性驗證的統一。抗篡改機制&#xff1a;數據…

java實現一個方法,isTure則程序繼續往下,為false則return的鏈式寫法

以下是實現鏈式條件檢查的Java方法&#xff0c;采用函數式風格設計。代碼包含一個Chainable類&#xff0c;支持連續的check方法和多個終止操作&#xff08;如then, orElse等&#xff09;&#xff0c;滿足在條件為false時中斷鏈式調用并返回默認值的需求&#xff1a;import java…

數據結構學習之堆

本篇我們將學習新的數據結構——二叉樹。 作者的個人gitee&#xff1a;樓田莉子 (riko-lou-tian) - Gitee.com 目錄 樹的概念 樹形結構 非樹形結構 樹的相關術語 樹的表示 樹在實際生活上的應用 二叉樹 慢二叉樹 完全二叉樹 二叉樹的儲存結構 二叉樹的存儲結構 順序結構…