{"id":81554,"date":"2026-07-25T02:58:53","date_gmt":"2026-07-24T18:58:53","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/81554.html"},"modified":"2026-07-25T02:58:53","modified_gmt":"2026-07-24T18:58:53","slug":"%e3%80%90c-qt%e3%80%91qt-%e5%ae%9e%e7%8e%b0-udp-%e6%b5%8b%e8%af%95%e5%b7%a5%e5%85%b7%ef%bc%9a%e5%ae%a2%e6%88%b7%e7%ab%af%e5%8f%91%e9%80%81%e3%80%81%e6%9c%8d%e5%8a%a1%e5%99%a8%e7%9b%91%e5%90%ac","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/81554.html","title":{"rendered":"\u3010C++\/Qt\u3011Qt \u5b9e\u73b0 UDP \u6d4b\u8bd5\u5de5\u5177\uff1a\u5ba2\u6237\u7aef\u53d1\u9001\u3001\u670d\u52a1\u5668\u76d1\u542c\u4e0e\u6d88\u606f\u6536\u53d1"},"content":{"rendered":"<p>\u5728\u7f51\u7edc\u8c03\u8bd5\u5de5\u5177\u4e2d&#xff0c;TCP\u3001HTTP\u3001WebSocket\u3001MQTT \u8fd9\u4e9b\u6a21\u5757\u90fd\u6709\u6bd4\u8f83\u660e\u663e\u7684\u201c\u8fde\u63a5\u201d\u6982\u5ff5&#xff0c;\u800c UDP \u6a21\u5757\u548c\u5b83\u4eec\u4e0d\u592a\u4e00\u6837\u3002UDP \u662f\u65e0\u8fde\u63a5\u901a\u4fe1&#xff0c;\u4e0d\u9700\u8981\u5148\u5efa\u7acb\u8fde\u63a5&#xff0c;\u53ea\u8981\u77e5\u9053\u76ee\u6807 IP \u548c\u76ee\u6807\u7aef\u53e3&#xff0c;\u5c31\u53ef\u4ee5\u76f4\u63a5\u53d1\u9001\u6570\u636e\u3002<\/p>\n<p>\u672c\u6587\u7ed3\u5408\u6211\u7684 Qt \u7f51\u7edc\u8c03\u8bd5\u5de5\u5177\u9879\u76ee\u4e2d\u7684 UDP \u6a21\u5757\u8fdb\u884c\u6574\u7406&#xff0c;\u9879\u76ee\u4e2d UDP \u88ab\u62c6\u6210\u4e24\u4e2a\u9875\u9762&#xff1a;<\/p>\n<p> FormUDPClient   \/\/ UDP \u5ba2\u6237\u7aef\u6a21\u5757&#xff0c;\u4e3b\u8981\u8d1f\u8d23\u5411\u6307\u5b9a IP \u548c\u7aef\u53e3\u53d1\u9001\u6570\u636e<br \/>\nFormUDPServer   \/\/ UDP \u670d\u52a1\u5668\u6a21\u5757&#xff0c;\u4e3b\u8981\u8d1f\u8d23\u7ed1\u5b9a\u672c\u5730\u7aef\u53e3\u3001\u63a5\u6536\u6570\u636e\u548c\u56de\u590d\u5ba2\u6237\u7aef <\/p>\n<p>\u6574\u4f53\u601d\u8def\u4e0d\u662f\u5199\u4e00\u4e2a\u5b64\u7acb Demo&#xff0c;\u800c\u662f\u628a UDP \u653e\u8fdb\u6574\u4e2a\u7f51\u7edc\u8c03\u8bd5\u52a9\u624b\u4e2d&#xff0c;\u548c TCP\u3001HTTP\u3001WebSocket \u7b49\u6a21\u5757\u4fdd\u6301\u7c7b\u4f3c\u7684\u754c\u9762\u98ce\u683c&#xff1a;\u53c2\u6570\u8f93\u5165\u3001\u542f\u52a8\u76d1\u542c\u3001\u53d1\u9001\u6570\u636e\u3001\u63a5\u6536\u6570\u636e\u663e\u793a\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u9519\u8bef\u63d0\u793a\u548c\u914d\u7f6e\u4fdd\u5b58\u3002<\/p>\n<h2>\u4e00\u3001UDP \u6a21\u5757\u6574\u4f53\u642d\u5efa\u601d\u8def<\/h2>\n<p>\u5728\u6211\u7684\u9879\u76ee\u4e2d&#xff0c;UDP \u6a21\u5757\u5206\u6210\u4e86\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e24\u4e2a\u7c7b&#xff0c;\u800c\u4e0d\u662f\u628a\u6240\u6709\u529f\u80fd\u90fd\u5806\u5728\u4e00\u4e2a\u7a97\u53e3\u91cc\u3002<\/p>\n<p>\u8fd9\u6837\u62c6\u5206\u7684\u597d\u5904\u662f\u6bd4\u8f83\u6e05\u695a&#xff1a;<\/p>\n<p>FormUDPClient&#xff1a;\u8d1f\u8d23\u201c\u4e3b\u52a8\u53d1\u9001\u201d<br \/>\nFormUDPServer&#xff1a;\u8d1f\u8d23\u201c\u7ed1\u5b9a\u76d1\u542c &#043; \u63a5\u6536\u6570\u636e &#043; \u56de\u590d\u5ba2\u6237\u7aef\u201d <\/p>\n<p>UDP \u548c TCP \u6700\u5927\u7684\u533a\u522b\u662f&#xff1a;UDP \u4e0d\u9700\u8981 connectToHost()&#xff0c;\u4e5f\u6ca1\u6709 newConnection() \u8fd9\u79cd\u8fde\u63a5\u7ba1\u7406\u6d41\u7a0b\u3002UDP \u7684\u6838\u5fc3\u4e3b\u8981\u5c31\u662f\u4e24\u4e2a\u51fd\u6570&#xff1a;<\/p>\n<p>bind()           \/\/ \u670d\u52a1\u5668\u7ed1\u5b9a\u672c\u5730 IP \u548c\u7aef\u53e3&#xff0c;\u7528\u6765\u63a5\u6536\u6570\u636e<br \/>\nwriteDatagram() \/\/ \u5ba2\u6237\u7aef\u6216\u670d\u52a1\u5668\u5411\u76ee\u6807\u5730\u5740\u53d1\u9001 UDP \u6570\u636e\u62a5 <\/p>\n<p>\u5728\u670d\u52a1\u5668\u7aef&#xff0c;\u9700\u8981\u51c6\u5907\u4e00\u4e2a QUdpSocket \u5bf9\u8c61&#xff0c;\u5e76\u4e14\u8bb0\u5f55\u670d\u52a1\u5668\u662f\u5426\u6b63\u5728\u8fd0\u884c&#xff1a;<\/p>\n<p>\/\/ UDP \u670d\u52a1\u5668\u5957\u63a5\u5b57&#xff0c;\u8d1f\u8d23\u7ed1\u5b9a\u7aef\u53e3\u3001\u63a5\u6536\u6570\u636e\u548c\u53d1\u9001\u56de\u590d<br \/>\nQUdpSocket *udpServerSocket;<\/p>\n<p>\/\/ \u6807\u8bb0\u5f53\u524d UDP \u670d\u52a1\u5668\u662f\u5426\u5df2\u7ecf\u542f\u52a8\u76d1\u542c<br \/>\nbool serverRunning; <\/p>\n<p>\u5982\u679c\u670d\u52a1\u5668\u6536\u5230\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u6570\u636e&#xff0c;\u8fd8\u9700\u8981\u4fdd\u5b58\u5ba2\u6237\u7aef\u7684\u5730\u5740\u548c\u7aef\u53e3&#xff0c;\u65b9\u4fbf\u540e\u9762\u70b9\u51fb\u201c\u53d1\u9001\u201d\u6309\u94ae\u65f6\u53ef\u4ee5\u56de\u590d\u7ed9\u6700\u8fd1\u4e00\u6b21\u901a\u4fe1\u7684\u5ba2\u6237\u7aef&#xff1a;<\/p>\n<p>\/\/ \u4fdd\u5b58\u6700\u8fd1\u4e00\u6b21\u53d1\u9001\u6570\u636e\u8fc7\u6765\u7684\u5ba2\u6237\u7aef\u5730\u5740<br \/>\nQHostAddress lastClientAddress;<\/p>\n<p>\/\/ \u4fdd\u5b58\u6700\u8fd1\u4e00\u6b21\u53d1\u9001\u6570\u636e\u8fc7\u6765\u7684\u5ba2\u6237\u7aef\u7aef\u53e3<br \/>\nquint16 lastClientPort; <\/p>\n<p>\u5ba2\u6237\u7aef\u8fd9\u8fb9\u76f8\u5bf9\u7b80\u5355&#xff0c;\u4e3b\u8981\u662f\u51c6\u5907\u4e00\u4e2a QUdpSocket&#xff0c;\u7136\u540e\u901a\u8fc7 writeDatagram() \u76f4\u63a5\u53d1\u6d88\u606f&#xff1a;<\/p>\n<p>\/\/ UDP \u5ba2\u6237\u7aef\u5957\u63a5\u5b57&#xff0c;\u8d1f\u8d23\u53d1\u9001\u6570\u636e\u548c\u63a5\u6536\u670d\u52a1\u5668\u56de\u5305<br \/>\nQUdpSocket *udpClientSocket; <\/p>\n<p>\u6240\u4ee5\u6574\u4e2a UDP \u6a21\u5757\u53ef\u4ee5\u7406\u89e3\u6210\u4e0b\u9762\u8fd9\u4e2a\u6d41\u7a0b&#xff1a;<\/p>\n<p>UDP Server&#xff1a;<br \/>\n\u521b\u5efa QUdpSocket<br \/>\n        \u2193<br \/>\n\u8bfb\u53d6\u754c\u9762 IP \u548c\u7aef\u53e3<br \/>\n        \u2193<br \/>\nInputValidator \u53c2\u6570\u6821\u9a8c<br \/>\n        \u2193<br \/>\nbind() \u542f\u52a8\u76d1\u542c<br \/>\n        \u2193<br \/>\nreadyRead \u4fe1\u53f7\u89e6\u53d1\u63a5\u6536\u51fd\u6570<br \/>\n        \u2193<br \/>\nreadDatagram() \u8bfb\u53d6\u5ba2\u6237\u7aef\u6570\u636e<\/p>\n<p>UDP Client&#xff1a;<br \/>\n\u521b\u5efa QUdpSocket<br \/>\n        \u2193<br \/>\n\u8bfb\u53d6\u76ee\u6807 IP\u3001\u76ee\u6807\u7aef\u53e3\u3001\u53d1\u9001\u5185\u5bb9<br \/>\n        \u2193<br \/>\nInputValidator \u53c2\u6570\u6821\u9a8c<br \/>\n        \u2193<br \/>\nwriteDatagram() \u53d1\u9001\u6570\u636e<br \/>\n        \u2193<br \/>\nreadyRead \u63a5\u6536\u670d\u52a1\u5668\u56de\u590d <\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4&#xff0c;UDP \u6a21\u5757\u771f\u6b63\u7684\u6838\u5fc3\u4e0d\u662f\u4ee3\u7801\u6709\u591a\u590d\u6742&#xff0c;\u800c\u662f\u628a\u201c\u754c\u9762\u8f93\u5165\u3001\u53c2\u6570\u6821\u9a8c\u3001\u5957\u63a5\u5b57\u64cd\u4f5c\u3001\u65e5\u5fd7\u663e\u793a\u3001\u9519\u8bef\u5904\u7406\u3001\u914d\u7f6e\u4fdd\u5b58\u201d\u8fd9\u4e9b\u6b65\u9aa4\u6309\u987a\u5e8f\u4e32\u8d77\u6765\u3002<\/p>\n<h2>\u4e8c\u3001UDP Server&#xff1a;\u542f\u52a8\u76d1\u542c\u7684\u6d41\u7a0b<\/h2>\n<p>UDP \u670d\u52a1\u5668\u6a21\u5757\u7684\u5165\u53e3\u662f\u201c\u542f\u52a8\u76d1\u542c\u201d\u6309\u94ae\u3002\u70b9\u51fb\u6309\u94ae\u540e&#xff0c;\u7a0b\u5e8f\u4f1a\u5148\u4ece\u754c\u9762\u4e2d\u8bfb\u53d6\u672c\u5730 IP \u548c\u7aef\u53e3&#xff0c;\u7136\u540e\u8fdb\u884c\u53c2\u6570\u6821\u9a8c&#xff0c;\u6821\u9a8c\u901a\u8fc7\u540e\u518d\u8c03\u7528 bind()\u3002<\/p>\n<p>\u8fd9\u4e2a\u5730\u65b9\u8981\u6ce8\u610f&#xff1a;UDP Server \u7684\u914d\u7f6e\u5e94\u8be5\u5728\u76d1\u542c\u6210\u529f\u4e4b\u540e\u518d\u4fdd\u5b58\u3002\u56e0\u4e3a\u7528\u6237\u8f93\u5165\u4e86 IP \u548c\u7aef\u53e3&#xff0c;\u4e0d\u4ee3\u8868\u8fd9\u4e2a\u7aef\u53e3\u4e00\u5b9a\u80fd\u7ed1\u5b9a\u6210\u529f\u3002\u6bd4\u5982\u7aef\u53e3\u88ab\u5360\u7528\u3001IP \u4e0d\u5408\u6cd5&#xff0c;\u90fd\u4f1a\u5bfc\u81f4\u76d1\u542c\u5931\u8d25\u3002<\/p>\n<p>\u6838\u5fc3\u903b\u8f91\u53ef\u4ee5\u6574\u7406\u6210\u4e0b\u9762\u8fd9\u6837&#xff1a;<\/p>\n<p>void FormUDPServer::startListen()<br \/>\n{<br \/>\n    \/\/ 1. \u4ece\u754c\u9762\u8bfb\u53d6\u672c\u5730 IP \u548c\u7aef\u53e3<br \/>\n    QString localIp &#061; ui-&gt;lineEditLocalIp-&gt;text().trimmed();<br \/>\n    QString localPortText &#061; ui-&gt;lineEditLocalPort-&gt;text().trimmed();<\/p>\n<p>    \/\/ 2. \u4f7f\u7528\u9879\u76ee\u4e2d\u7684 InputValidator \u505a\u53c2\u6570\u6821\u9a8c<br \/>\n    \/\/ \u8fd9\u91cc\u7684\u76ee\u7684\u4e0d\u662f\u76f4\u63a5 bind&#xff0c;\u800c\u662f\u5148\u4fdd\u8bc1\u7528\u6237\u8f93\u5165\u683c\u5f0f\u6b63\u786e<br \/>\n    if (!InputValidator::isValidIp(localIp)) {<br \/>\n        ErrorHandler::showError(&#034;\u672c\u5730 IP \u5730\u5740\u683c\u5f0f\u4e0d\u6b63\u786e&#034;);<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    if (!InputValidator::isValidPort(localPortText)) {<br \/>\n        ErrorHandler::showError(&#034;\u672c\u5730\u7aef\u53e3\u683c\u5f0f\u4e0d\u6b63\u786e&#034;);<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    \/\/ 3. \u5c06\u7aef\u53e3\u4ece QString \u8f6c\u6210 quint16<br \/>\n    quint16 localPort &#061; localPortText.toUShort();<\/p>\n<p>    \/\/ 4. \u6839\u636e\u754c\u9762\u8f93\u5165\u521b\u5efa\u76d1\u542c\u5730\u5740<br \/>\n    QHostAddress address(localIp);<\/p>\n<p>    \/\/ 5. \u8c03\u7528 bind() \u542f\u52a8 UDP \u76d1\u542c<br \/>\n    bool ok &#061; udpServerSocket-&gt;bind(address, localPort);<\/p>\n<p>    if (!ok) {<br \/>\n        \/\/ 6. \u76d1\u542c\u5931\u8d25\u65f6&#xff0c;\u4ea4\u7ed9 ErrorHandler \u6216\u65e5\u5fd7\u533a\u57df\u663e\u793a\u9519\u8bef\u4fe1\u606f<br \/>\n        ErrorHandler::showError(&#034;UDP \u670d\u52a1\u5668\u542f\u52a8\u5931\u8d25&#xff1a;&#034; &#043;<br \/>\n                                udpServerSocket-&gt;errorString());<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    \/\/ 7. bind \u6210\u529f\u540e&#xff0c;\u8bf4\u660e\u670d\u52a1\u5668\u771f\u6b63\u542f\u52a8\u4e86<br \/>\n    serverRunning &#061; true;<\/p>\n<p>    \/\/ 8. \u76d1\u542c\u6210\u529f\u540e\u518d\u4fdd\u5b58\u914d\u7f6e&#xff0c;\u907f\u514d\u4fdd\u5b58\u65e0\u6548\u53c2\u6570<br \/>\n    saveConfig();<\/p>\n<p>    \/\/ 9. \u8f93\u51fa\u8fd0\u884c\u65e5\u5fd7<br \/>\n    appendLog(QString(&#034;UDP \u670d\u52a1\u5668\u542f\u52a8\u6210\u529f&#xff0c;\u76d1\u542c\u5730\u5740&#xff1a;%1:%2&#034;)<br \/>\n              .arg(localIp)<br \/>\n              .arg(localPort));<br \/>\n} <\/p>\n<p>\u8fd9\u6bb5\u6d41\u7a0b\u91cc\u9762\u6700\u5173\u952e\u7684\u662f&#xff1a;<\/p>\n<p>udpServerSocket-&gt;bind(address, localPort); <\/p>\n<p>\u8fd9\u4e00\u6b65\u76f8\u5f53\u4e8e\u544a\u8bc9\u7cfb\u7edf&#xff1a;<\/p>\n<p>\u6211\u8981\u5728\u8fd9\u4e2a IP \u548c\u8fd9\u4e2a\u7aef\u53e3\u4e0a\u63a5\u6536 UDP \u6570\u636e\u3002 <\/p>\n<p>\u5982\u679c\u8fd9\u4e00\u6b65\u6210\u529f&#xff0c;\u90a3\u4e48\u522b\u4eba\u5411\u8fd9\u4e2a\u5730\u5740\u548c\u7aef\u53e3\u53d1\u9001 UDP \u6570\u636e\u65f6&#xff0c;\u670d\u52a1\u5668\u5c31\u53ef\u4ee5\u6536\u5230\u3002<\/p>\n<p>\u8fd9\u91cc\u4e3a\u4ec0\u4e48\u8981\u5148\u7528 InputValidator&#xff1f;<\/p>\n<p>\u56e0\u4e3a\u754c\u9762\u8f93\u5165\u7684\u6570\u636e\u90fd\u662f\u5b57\u7b26\u4e32&#xff0c;\u6bd4\u5982 IP\u3001\u7aef\u53e3\u90fd\u662f\u7528\u6237\u624b\u52a8\u8f93\u5165\u7684&#xff0c;\u5982\u679c\u4e0d\u6821\u9a8c&#xff0c;\u76f4\u63a5\u4f20\u7ed9 bind()&#xff0c;\u7a0b\u5e8f\u5f88\u5bb9\u6613\u51fa\u73b0\u9519\u8bef\u3002\u4f60\u7684\u9879\u76ee\u91cc\u628a\u53c2\u6570\u6821\u9a8c\u5355\u72ec\u5c01\u88c5\u51fa\u6765&#xff0c;\u8fd9\u662f\u6bd4\u7b80\u5355 Demo \u66f4\u50cf\u5b8c\u6574\u9879\u76ee\u7684\u5730\u65b9\u3002<\/p>\n<p>\u542f\u52a8\u76d1\u542c\u7684\u903b\u8f91\u53ef\u4ee5\u6982\u62ec\u6210&#xff1a;<\/p>\n<p>\u8bfb\u53d6\u754c\u9762\u53c2\u6570<br \/>\n        \u2193<br \/>\n\u6821\u9a8c IP \u548c\u7aef\u53e3<br \/>\n        \u2193<br \/>\n\u8c03\u7528 bind()<br \/>\n        \u2193<br \/>\n\u5224\u65ad\u662f\u5426\u542f\u52a8\u6210\u529f<br \/>\n        \u2193<br \/>\n\u6210\u529f\u540e\u4fdd\u5b58\u914d\u7f6e\u5e76\u8f93\u51fa\u65e5\u5fd7 <\/p>\n<h2>\u4e09\u3001UDP Server&#xff1a;\u63a5\u6536\u5ba2\u6237\u7aef\u6570\u636e\u5e76\u8bb0\u5f55\u6765\u6e90<\/h2>\n<p>UDP \u670d\u52a1\u5668\u542f\u52a8\u76d1\u542c\u4e4b\u540e&#xff0c;\u5e76\u4e0d\u662f\u4e00\u76f4\u624b\u52a8\u5faa\u73af\u7b49\u5f85\u6570\u636e&#xff0c;\u800c\u662f\u4f9d\u9760 Qt \u7684\u4fe1\u53f7\u69fd\u673a\u5236\u3002<\/p>\n<p>\u670d\u52a1\u5668\u5957\u63a5\u5b57\u6536\u5230\u6570\u636e\u65f6&#xff0c;\u4f1a\u89e6\u53d1 readyRead \u4fe1\u53f7&#xff1a;<\/p>\n<p>connect(udpServerSocket, &amp;QUdpSocket::readyRead,<br \/>\n        this, &amp;FormUDPServer::readData); <\/p>\n<p>\u8fd9\u884c\u4ee3\u7801\u7684\u610f\u601d\u662f&#xff1a;<\/p>\n<p>\u53ea\u8981 udpServerSocket \u6536\u5230 UDP \u6570\u636e&#xff0c;<br \/>\n\u5c31\u81ea\u52a8\u8fdb\u5165 FormUDPServer::readData() \u51fd\u6570\u5904\u7406\u3002 <\/p>\n<p>\u5728\u63a5\u6536\u51fd\u6570\u91cc\u9762&#xff0c;\u901a\u5e38\u8981\u7528 while \u5faa\u73af\u8bfb\u53d6\u3002\u56e0\u4e3a UDP \u6570\u636e\u662f\u4e00\u4e2a\u4e2a\u6570\u636e\u62a5&#xff0c;\u6709\u65f6\u5019\u53ef\u80fd\u4e00\u6b21\u6536\u5230\u591a\u4e2a\u6570\u636e\u5305&#xff0c;\u4e0d\u80fd\u53ea\u8bfb\u4e00\u6b21\u5c31\u7ed3\u675f\u3002<\/p>\n<p>void FormUDPServer::readData()<br \/>\n{<br \/>\n    \/\/ \u53ea\u8981\u8fd8\u6709\u6ca1\u8bfb\u53d6\u5b8c\u7684\u6570\u636e\u62a5&#xff0c;\u5c31\u7ee7\u7eed\u8bfb\u53d6<br \/>\n    while (udpServerSocket-&gt;hasPendingDatagrams()) {<\/p>\n<p>        \/\/ 1. \u6839\u636e\u5f53\u524d\u6570\u636e\u5305\u5927\u5c0f\u521b\u5efa\u7f13\u51b2\u533a<br \/>\n        QByteArray datagram;<br \/>\n        datagram.resize(udpServerSocket-&gt;pendingDatagramSize());<\/p>\n<p>        \/\/ 2. \u7528\u6765\u4fdd\u5b58\u5ba2\u6237\u7aef\u7684 IP \u548c\u7aef\u53e3<br \/>\n        QHostAddress clientAddress;<br \/>\n        quint16 clientPort;<\/p>\n<p>        \/\/ 3. \u8bfb\u53d6 UDP \u6570\u636e&#xff0c;\u540c\u65f6\u62ff\u5230\u53d1\u9001\u65b9\u5730\u5740\u548c\u7aef\u53e3<br \/>\n        udpServerSocket-&gt;readDatagram(datagram.data(),<br \/>\n                                      datagram.size(),<br \/>\n                                      &amp;clientAddress,<br \/>\n                                      &amp;clientPort);<\/p>\n<p>        \/\/ 4. \u4fdd\u5b58\u6700\u8fd1\u4e00\u6b21\u5ba2\u6237\u7aef\u4fe1\u606f&#xff0c;\u65b9\u4fbf\u670d\u52a1\u5668\u540e\u7eed\u56de\u590d<br \/>\n        lastClientAddress &#061; clientAddress;<br \/>\n        lastClientPort &#061; clientPort;<\/p>\n<p>        \/\/ 5. \u5c06\u6536\u5230\u7684\u5b57\u8282\u6570\u636e\u8f6c\u6210\u5b57\u7b26\u4e32\u663e\u793a<br \/>\n        QString msg &#061; QString::fromUtf8(datagram);<\/p>\n<p>        \/\/ 6. \u5728\u65e5\u5fd7\u7a97\u53e3\u663e\u793a\u6536\u5230\u7684\u6570\u636e<br \/>\n        appendLog(QString(&#034;\u6536\u5230\u5ba2\u6237\u7aef [%1:%2] \u7684\u6570\u636e&#xff1a;%3&#034;)<br \/>\n                  .arg(clientAddress.toString())<br \/>\n                  .arg(clientPort)<br \/>\n                  .arg(msg));<br \/>\n    }<br \/>\n} <\/p>\n<p>\u8fd9\u4e2a\u51fd\u6570\u91cc\u6700\u91cd\u8981\u7684\u662f&#xff1a;<\/p>\n<p>readDatagram(datagram.data(), datagram.size(), &amp;clientAddress, &amp;clientPort); <\/p>\n<p>\u5b83\u4e0d\u53ea\u662f\u8bfb\u53d6\u5185\u5bb9&#xff0c;\u8fd8\u80fd\u62ff\u5230\u53d1\u9001\u65b9\u7684 IP \u548c\u7aef\u53e3\u3002<\/p>\n<p>\u8fd9\u4e00\u70b9\u5bf9 UDP \u5f88\u5173\u952e\u3002\u56e0\u4e3a UDP \u6ca1\u6709\u8fde\u63a5\u5173\u7cfb&#xff0c;\u4e0d\u50cf TCP \u90a3\u6837\u6bcf\u4e2a\u5ba2\u6237\u7aef\u90fd\u6709\u4e00\u4e2a\u8fde\u63a5\u5bf9\u8c61\u3002UDP \u6536\u5230\u6570\u636e\u65f6&#xff0c;\u5fc5\u987b\u901a\u8fc7 readDatagram() \u81ea\u5df1\u83b7\u53d6\u5bf9\u65b9\u5730\u5740\u3002<\/p>\n<p>\u8fd9\u91cc\u4fdd\u5b58&#xff1a;<\/p>\n<p>lastClientAddress &#061; clientAddress;<br \/>\nlastClientPort &#061; clientPort; <\/p>\n<p>\u662f\u4e3a\u4e86\u8ba9\u670d\u52a1\u5668\u77e5\u9053\u201c\u6700\u8fd1\u662f\u8c01\u7ed9\u6211\u53d1\u4e86\u6d88\u606f\u201d\u3002\u8fd9\u6837\u670d\u52a1\u5668\u70b9\u51fb\u53d1\u9001\u6309\u94ae\u65f6&#xff0c;\u5c31\u53ef\u4ee5\u628a\u6570\u636e\u53d1\u56de\u7ed9\u8fd9\u4e2a\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u6240\u4ee5\u670d\u52a1\u5668\u63a5\u6536\u6570\u636e\u7684\u601d\u8def\u662f&#xff1a;<\/p>\n<p>readyRead \u4fe1\u53f7\u89e6\u53d1<br \/>\n        \u2193<br \/>\n\u5224\u65ad\u662f\u5426\u6709\u5f85\u5904\u7406\u6570\u636e<br \/>\n        \u2193<br \/>\nreadDatagram() \u8bfb\u53d6\u6570\u636e<br \/>\n        \u2193<br \/>\n\u8bb0\u5f55\u5ba2\u6237\u7aef IP \u548c\u7aef\u53e3<br \/>\n        \u2193<br \/>\n\u663e\u793a\u5230\u65e5\u5fd7\u533a <\/p>\n<p>\u8fd9\u4e2a\u8bbe\u8ba1\u6bd4\u8f83\u9002\u5408\u7f51\u7edc\u8c03\u8bd5\u5de5\u5177&#xff0c;\u56e0\u4e3a\u65e5\u5fd7\u91cc\u9762\u80fd\u6e05\u695a\u770b\u5230\u6570\u636e\u6765\u6e90&#xff0c;\u6bd4\u5982&#xff1a;<\/p>\n<p>\u6536\u5230\u5ba2\u6237\u7aef [192.168.1.10:50523] \u7684\u6570\u636e&#xff1a;hello udp <\/p>\n<p>\u8fd9\u6837\u8c03\u8bd5\u7684\u65f6\u5019\u5c31\u77e5\u9053\u662f\u54ea\u53f0\u673a\u5668\u3001\u54ea\u4e2a\u7aef\u53e3\u53d1\u6765\u7684\u6570\u636e\u3002<\/p>\n<h2>\u56db\u3001UDP Client&#xff1a;\u53d1\u9001\u6570\u636e\u4e0e\u63a5\u6536\u670d\u52a1\u5668\u56de\u590d<\/h2>\n<p>UDP \u5ba2\u6237\u7aef\u6a21\u5757\u7684\u6838\u5fc3\u662f\u53d1\u9001\u6570\u636e\u3002<\/p>\n<p>\u5ba2\u6237\u7aef\u548c TCP Client \u4e0d\u4e00\u6837&#xff0c;\u5b83\u4e0d\u9700\u8981\u5148\u8fde\u63a5\u670d\u52a1\u5668\u3002\u70b9\u51fb\u53d1\u9001\u6309\u94ae\u65f6&#xff0c;\u7a0b\u5e8f\u53ea\u9700\u8981\u8bfb\u53d6\u76ee\u6807 IP\u3001\u76ee\u6807\u7aef\u53e3\u548c\u53d1\u9001\u5185\u5bb9&#xff0c;\u7136\u540e\u8c03\u7528 writeDatagram()\u3002<\/p>\n<p>\u5728\u9879\u76ee\u91cc&#xff0c;UDP Client \u53ef\u4ee5\u91c7\u7528\u201c\u61d2\u521b\u5efa\u201d\u7684\u65b9\u5f0f&#xff1a;\u4e5f\u5c31\u662f\u7b2c\u4e00\u6b21\u53d1\u9001\u6570\u636e\u65f6&#xff0c;\u5982\u679c udpClientSocket \u8fd8\u6ca1\u6709\u521b\u5efa&#xff0c;\u5c31\u5148\u521b\u5efa\u5b83&#xff0c;\u5e76\u8fde\u63a5 readyRead \u4fe1\u53f7&#xff0c;\u65b9\u4fbf\u540e\u7eed\u63a5\u6536\u670d\u52a1\u5668\u56de\u590d\u3002<\/p>\n<p>void FormUDPClient::sendData()<br \/>\n{<br \/>\n    \/\/ 1. \u5982\u679c\u5ba2\u6237\u7aef socket \u8fd8\u6ca1\u6709\u521b\u5efa&#xff0c;\u5c31\u5148\u521b\u5efa<br \/>\n    if (udpClientSocket &#061;&#061; nullptr) {<br \/>\n        udpClientSocket &#061; new QUdpSocket(this);<\/p>\n<p>        \/\/ \u6536\u5230\u670d\u52a1\u5668\u56de\u590d\u65f6&#xff0c;\u8fdb\u5165 readData() \u5904\u7406<br \/>\n        connect(udpClientSocket, &amp;QUdpSocket::readyRead,<br \/>\n                this, &amp;FormUDPClient::readData);<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u4ece\u754c\u9762\u8bfb\u53d6\u76ee\u6807 IP\u3001\u76ee\u6807\u7aef\u53e3\u548c\u53d1\u9001\u5185\u5bb9<br \/>\n    QString targetIp &#061; ui-&gt;lineEditTargetIp-&gt;text().trimmed();<br \/>\n    QString targetPortText &#061; ui-&gt;lineEditTargetPort-&gt;text().trimmed();<br \/>\n    QString sendText &#061; ui-&gt;textEditSend-&gt;toPlainText();<\/p>\n<p>    \/\/ 3. \u53c2\u6570\u6821\u9a8c&#xff1a;IP\u3001\u7aef\u53e3\u3001\u5185\u5bb9\u90fd\u4e0d\u80fd\u4e3a\u7a7a\u6216\u683c\u5f0f\u9519\u8bef<br \/>\n    if (!InputValidator::isValidIp(targetIp)) {<br \/>\n        ErrorHandler::showError(&#034;\u76ee\u6807 IP \u5730\u5740\u683c\u5f0f\u4e0d\u6b63\u786e&#034;);<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    if (!InputValidator::isValidPort(targetPortText)) {<br \/>\n        ErrorHandler::showError(&#034;\u76ee\u6807\u7aef\u53e3\u683c\u5f0f\u4e0d\u6b63\u786e&#034;);<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    if (sendText.isEmpty()) {<br \/>\n        ErrorHandler::showError(&#034;\u53d1\u9001\u5185\u5bb9\u4e0d\u80fd\u4e3a\u7a7a&#034;);<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    \/\/ 4. \u8f6c\u6362\u7aef\u53e3\u548c\u53d1\u9001\u5185\u5bb9<br \/>\n    quint16 targetPort &#061; targetPortText.toUShort();<br \/>\n    QByteArray data &#061; sendText.toUtf8();<\/p>\n<p>    \/\/ 5. \u53d1\u9001 UDP \u6570\u636e\u62a5<br \/>\n    qint64 len &#061; udpClientSocket-&gt;writeDatagram(data,<br \/>\n                                                QHostAddress(targetIp),<br \/>\n                                                targetPort);<\/p>\n<p>    \/\/ 6. \u5224\u65ad\u662f\u5426\u53d1\u9001\u6210\u529f<br \/>\n    if (len &#061;&#061; -1) {<br \/>\n        ErrorHandler::showError(&#034;UDP \u6570\u636e\u53d1\u9001\u5931\u8d25&#xff1a;&#034; &#043;<br \/>\n                                udpClientSocket-&gt;errorString());<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    \/\/ 7. \u53d1\u9001\u6210\u529f\u540e\u4fdd\u5b58\u914d\u7f6e\u5e76\u5199\u65e5\u5fd7<br \/>\n    saveConfig();<\/p>\n<p>    appendLog(QString(&#034;\u53d1\u9001\u5230 [%1:%2]&#xff1a;%3&#034;)<br \/>\n              .arg(targetIp)<br \/>\n              .arg(targetPort)<br \/>\n              .arg(sendText));<br \/>\n} <\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u7684\u6838\u5fc3\u662f&#xff1a;<\/p>\n<p>udpClientSocket-&gt;writeDatagram(data,<br \/>\n                               QHostAddress(targetIp),<br \/>\n                               targetPort); <\/p>\n<p>\u5b83\u7684\u542b\u4e49\u5f88\u76f4\u63a5&#xff1a;<\/p>\n<p>\u628a data \u8fd9\u6bb5\u6570\u636e\u53d1\u9001\u5230 targetIp \u7684 targetPort \u7aef\u53e3\u3002 <\/p>\n<p>UDP \u7684\u53d1\u9001\u903b\u8f91\u6bd4 TCP \u7b80\u5355\u5f88\u591a&#xff0c;\u56e0\u4e3a\u5b83\u4e0d\u7528\u7ef4\u62a4\u8fde\u63a5\u72b6\u6001\u3002\u4f46\u662f\u8fd9\u91cc\u4e5f\u6709\u4e00\u4e2a\u5bb9\u6613\u8bef\u89e3\u7684\u70b9&#xff1a;<\/p>\n<p>writeDatagram() \u8fd4\u56de\u6210\u529f&#xff0c;\u53ea\u80fd\u8bf4\u660e\u6570\u636e\u4ece\u672c\u673a\u53d1\u51fa\u53bb\u4e86&#xff0c;<br \/>\n\u4e0d\u80fd\u4fdd\u8bc1\u5bf9\u65b9\u4e00\u5b9a\u6536\u5230\u4e86\u3002 <\/p>\n<p>\u8fd9\u662f UDP \u534f\u8bae\u672c\u8eab\u7684\u7279\u70b9\u3002\u5b83\u66f4\u50cf\u201c\u628a\u6d88\u606f\u53d1\u51fa\u53bb\u201d&#xff0c;\u800c\u4e0d\u662f\u50cf TCP \u90a3\u6837\u5148\u5efa\u7acb\u8fde\u63a5\u3001\u518d\u4fdd\u8bc1\u53ef\u9760\u4f20\u8f93\u3002<\/p>\n<p>\u5982\u679c\u670d\u52a1\u5668\u6536\u5230\u6d88\u606f\u5e76\u56de\u590d&#xff0c;\u5ba2\u6237\u7aef\u540c\u6837\u901a\u8fc7 readyRead \u63a5\u6536&#xff1a;<\/p>\n<p>void FormUDPClient::readData()<br \/>\n{<br \/>\n    while (udpClientSocket-&gt;hasPendingDatagrams()) {<\/p>\n<p>        QByteArray datagram;<br \/>\n        datagram.resize(udpClientSocket-&gt;pendingDatagramSize());<\/p>\n<p>        QHostAddress serverAddress;<br \/>\n        quint16 serverPort;<\/p>\n<p>        \/\/ \u8bfb\u53d6\u670d\u52a1\u5668\u8fd4\u56de\u7684\u6570\u636e<br \/>\n        udpClientSocket-&gt;readDatagram(datagram.data(),<br \/>\n                                      datagram.size(),<br \/>\n                                      &amp;serverAddress,<br \/>\n                                      &amp;serverPort);<\/p>\n<p>        QString msg &#061; QString::fromUtf8(datagram);<\/p>\n<p>        appendLog(QString(&#034;\u6536\u5230\u670d\u52a1\u5668 [%1:%2] \u56de\u590d&#xff1a;%3&#034;)<br \/>\n                  .arg(serverAddress.toString())<br \/>\n                  .arg(serverPort)<br \/>\n                  .arg(msg));<br \/>\n    }<br \/>\n} <\/p>\n<p>\u5ba2\u6237\u7aef\u7684\u6574\u4f53\u6d41\u7a0b\u5c31\u662f&#xff1a;<\/p>\n<p>\u70b9\u51fb\u53d1\u9001<br \/>\n        \u2193<br \/>\n\u521b\u5efa QUdpSocket<br \/>\n        \u2193<br \/>\n\u8bfb\u53d6\u76ee\u6807 IP\u3001\u7aef\u53e3\u3001\u5185\u5bb9<br \/>\n        \u2193<br \/>\n\u53c2\u6570\u6821\u9a8c<br \/>\n        \u2193<br \/>\nwriteDatagram() \u53d1\u9001<br \/>\n        \u2193<br \/>\nreadyRead \u63a5\u6536\u670d\u52a1\u5668\u56de\u590d <\/p>\n<p>\u8fd9\u6837 UDP Client \u548c UDP Server \u5c31\u53ef\u4ee5\u4e92\u76f8\u6d4b\u8bd5&#xff1a;\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u53d1\u9001\u6d88\u606f&#xff0c;\u670d\u52a1\u5668\u6536\u5230\u540e\u663e\u793a\u6765\u6e90&#xff0c;\u4e5f\u53ef\u4ee5\u518d\u56de\u590d\u5ba2\u6237\u7aef\u3002<\/p>\n<h2>\u4e94\u3001\u914d\u7f6e\u4fdd\u5b58\u3001\u65e5\u5fd7\u8f93\u51fa\u548c\u9519\u8bef\u5904\u7406<\/h2>\n<p>\u4e00\u4e2a\u7f51\u7edc\u8c03\u8bd5\u5de5\u5177\u5982\u679c\u53ea\u662f\u80fd\u53d1\u6570\u636e&#xff0c;\u5176\u5b9e\u8fd8\u4e0d\u591f\u3002\u771f\u6b63\u7528\u8d77\u6765\u6bd4\u8f83\u8212\u670d\u7684\u5730\u65b9\u5728\u4e8e&#xff1a;\u914d\u7f6e\u80fd\u4fdd\u5b58\u3001\u9519\u8bef\u80fd\u63d0\u793a\u3001\u901a\u4fe1\u8fc7\u7a0b\u80fd\u770b\u89c1\u3002<\/p>\n<p>\u6240\u4ee5UDP \u6a21\u5757\u91cc\u9664\u4e86 QUdpSocket&#xff0c;\u8fd8\u7ed3\u5408\u4e86\u51e0\u4e2a\u9879\u76ee\u4e2d\u901a\u7528\u7684\u8f85\u52a9\u6a21\u5757&#xff1a;<\/p>\n<p>InputValidator&#xff1a;\u8d1f\u8d23\u6821\u9a8c IP\u3001\u7aef\u53e3\u3001\u53d1\u9001\u5185\u5bb9\u7b49\u53c2\u6570 ErrorHandler&#xff1a;\u8d1f\u8d23\u7edf\u4e00\u5904\u7406\u9519\u8bef\u63d0\u793a QSettings&#xff1a;\u8d1f\u8d23\u4fdd\u5b58\u4e0a\u4e00\u6b21\u4f7f\u7528\u7684\u914d\u7f6e \u65e5\u5fd7\u7a97\u53e3&#xff1a;\u8d1f\u8d23\u663e\u793a\u542f\u52a8\u3001\u53d1\u9001\u3001\u63a5\u6536\u3001\u5931\u8d25\u7b49\u901a\u4fe1\u72b6\u6001<\/p>\n<p>\u65e5\u5fd7\u51fd\u6570\u53ef\u4ee5\u5c01\u88c5\u6210\u7edf\u4e00\u5f62\u5f0f&#xff1a;<\/p>\n<p>void FormUDPServer::appendLog(const QString &amp;msg)<br \/>\n{<br \/>\n    \/\/ \u83b7\u53d6\u5f53\u524d\u65f6\u95f4&#xff0c;\u65b9\u4fbf\u5b9a\u4f4d\u901a\u4fe1\u53d1\u751f\u7684\u65f6\u95f4\u70b9<br \/>\n    QString time &#061; QDateTime::currentDateTime()<br \/>\n                       .toString(&#034;yyyy-MM-dd hh:mm:ss&#034;);<\/p>\n<p>    \/\/ \u8ffd\u52a0\u5230\u65e5\u5fd7\u663e\u793a\u533a\u57df<br \/>\n    ui-&gt;textEditLog-&gt;append(QString(&#034;[%1] %2&#034;).arg(time, msg));<br \/>\n} <\/p>\n<p>\u8fd9\u6837\u4e0d\u7ba1\u662f\u670d\u52a1\u5668\u542f\u52a8\u6210\u529f\u3001\u7ed1\u5b9a\u5931\u8d25\u3001\u6536\u5230\u6570\u636e&#xff0c;\u8fd8\u662f\u5ba2\u6237\u7aef\u53d1\u9001\u5931\u8d25&#xff0c;\u90fd\u53ef\u4ee5\u7edf\u4e00\u663e\u793a\u3002<\/p>\n<p>\u4f8b\u5982&#xff1a;<\/p>\n<p>[2026-05-17 10:30:12] UDP \u670d\u52a1\u5668\u542f\u52a8\u6210\u529f&#xff0c;\u76d1\u542c\u5730\u5740&#xff1a;127.0.0.1:8888<br \/>\n[2026-05-17 10:30:20] \u6536\u5230\u5ba2\u6237\u7aef [127.0.0.1:50523] \u7684\u6570\u636e&#xff1a;hello<br \/>\n[2026-05-17 10:30:25] \u53d1\u9001\u5230 [127.0.0.1:8888]&#xff1a;test udp <\/p>\n<p>\u914d\u7f6e\u4fdd\u5b58\u5efa\u8bae\u6309\u7167\u201c\u64cd\u4f5c\u6210\u529f\u540e\u4fdd\u5b58\u201d\u7684\u601d\u8def\u5904\u7406\u3002<\/p>\n<p>UDP Server \u8fd9\u91cc&#xff0c;\u5e94\u8be5\u662f&#xff1a;<\/p>\n<p>bind() \u6210\u529f<br \/>\n        \u2193<br \/>\nserverRunning &#061; true<br \/>\n        \u2193<br \/>\nsaveConfig() <\/p>\n<p>\u56e0\u4e3a\u53ea\u6709\u7ed1\u5b9a\u6210\u529f&#xff0c;\u624d\u80fd\u8bf4\u660e\u8fd9\u4e2a IP \u548c\u7aef\u53e3\u662f\u6709\u6548\u7684\u3002<\/p>\n<p>UDP Client \u8fd9\u91cc&#xff0c;\u5e94\u8be5\u662f&#xff1a;<\/p>\n<p>writeDatagram() \u53d1\u9001\u6210\u529f<br \/>\n        \u2193<br \/>\nsaveConfig() <\/p>\n<p>\u56e0\u4e3a\u53d1\u9001\u6210\u529f\u540e&#xff0c;\u8bf4\u660e\u8fd9\u6b21\u76ee\u6807 IP\u3001\u76ee\u6807\u7aef\u53e3\u548c\u53d1\u9001\u5185\u5bb9\u81f3\u5c11\u5b8c\u6210\u4e86\u4e00\u6b21\u6709\u6548\u63d0\u4ea4\u3002<\/p>\n<p>\u5173\u95ed\u670d\u52a1\u5668\u76d1\u542c\u65f6&#xff0c;\u53ef\u4ee5\u8fd9\u6837\u5904\u7406&#xff1a;<\/p>\n<p>void FormUDPServer::stopListen()<br \/>\n{<br \/>\n    \/\/ \u5982\u679c\u670d\u52a1\u5668\u6ca1\u6709\u8fd0\u884c&#xff0c;\u5c31\u4e0d\u9700\u8981\u91cd\u590d\u5173\u95ed<br \/>\n    if (!serverRunning) {<br \/>\n        appendLog(&#034;UDP \u670d\u52a1\u5668\u5f53\u524d\u672a\u542f\u52a8&#034;);<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    \/\/ \u5173\u95ed UDP socket&#xff0c;\u91ca\u653e\u7aef\u53e3<br \/>\n    udpServerSocket-&gt;close();<\/p>\n<p>    \/\/ \u4fee\u6539\u8fd0\u884c\u72b6\u6001<br \/>\n    serverRunning &#061; false;<\/p>\n<p>    appendLog(&#034;UDP \u670d\u52a1\u5668\u5df2\u5173\u95ed\u76d1\u542c&#034;);<br \/>\n} <\/p>\n<p>\u8fd9\u4e00\u90e8\u5206\u867d\u7136\u4ee3\u7801\u4e0d\u590d\u6742&#xff0c;\u4f46\u662f\u5f88\u91cd\u8981\u3002\u56e0\u4e3a\u7f51\u7edc\u8c03\u8bd5\u5de5\u5177\u7ecf\u5e38\u4f1a\u9047\u5230\u7aef\u53e3\u5360\u7528\u3001\u53c2\u6570\u8f93\u5165\u9519\u8bef\u3001\u76ee\u6807\u4e3b\u673a\u4e0d\u53ef\u8fbe\u3001\u91cd\u590d\u542f\u52a8\u76d1\u542c\u7b49\u95ee\u9898\u3002\u5982\u679c\u6ca1\u6709\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8f93\u51fa&#xff0c;\u7528\u6237\u53ea\u4f1a\u611f\u89c9\u201c\u6ca1\u53cd\u5e94\u201d\u3002<\/p>\n<p>\u6240\u4ee5 UDP \u6a21\u5757\u6700\u7ec8\u53ef\u4ee5\u603b\u7ed3\u6210\u4e00\u53e5\u8bdd&#xff1a;<\/p>\n<p>UDP \u901a\u4fe1\u672c\u8eab\u53ea\u9700\u8981 bind()\u3001readDatagram()\u3001writeDatagram()&#xff0c;<br \/>\n\u4f46\u4f5c\u4e3a\u4e00\u4e2a\u5b8c\u6574\u7684 Qt \u7f51\u7edc\u8c03\u8bd5\u5de5\u5177&#xff0c;\u8fd8\u9700\u8981\u53c2\u6570\u6821\u9a8c\u3001\u9519\u8bef\u63d0\u793a\u3001\u65e5\u5fd7\u8f93\u51fa\u548c\u914d\u7f6e\u4fdd\u5b58\u3002 <\/p>\n<p>\u6574\u4e2a\u6a21\u5757\u7684\u642d\u5efa\u6d41\u7a0b\u53ef\u4ee5\u603b\u7ed3\u4e3a&#xff1a;<\/p>\n<p>FormUDPServer&#xff1a;<br \/>\n\u521d\u59cb\u5316 QUdpSocket<br \/>\n        \u2193<br \/>\n\u542f\u52a8\u76d1\u542c\u65f6\u6821\u9a8c IP \u548c\u7aef\u53e3<br \/>\n        \u2193<br \/>\nbind() \u6210\u529f\u540e\u4fdd\u5b58\u914d\u7f6e<br \/>\n        \u2193<br \/>\nreadyRead \u89e6\u53d1 readDatagram()<br \/>\n        \u2193<br \/>\n\u8bb0\u5f55\u5ba2\u6237\u7aef\u5730\u5740\u548c\u7aef\u53e3<br \/>\n        \u2193<br \/>\n\u65e5\u5fd7\u663e\u793a\u63a5\u6536\u5185\u5bb9<\/p>\n<p>FormUDPClient&#xff1a;<br \/>\n\u61d2\u521b\u5efa QUdpSocket<br \/>\n        \u2193<br \/>\n\u53d1\u9001\u524d\u6821\u9a8c\u76ee\u6807 IP\u3001\u7aef\u53e3\u3001\u5185\u5bb9<br \/>\n        \u2193<br \/>\nwriteDatagram() \u53d1\u9001\u6570\u636e<br \/>\n        \u2193<br \/>\n\u53d1\u9001\u6210\u529f\u540e\u4fdd\u5b58\u914d\u7f6e<br \/>\n        \u2193<br \/>\nreadyRead \u63a5\u6536\u670d\u52a1\u5668\u56de\u590d<br \/>\n        \u2193<br \/>\n\u65e5\u5fd7\u663e\u793a\u901a\u4fe1\u8fc7\u7a0b <\/p>\n<p>\u901a\u8fc7\u8fd9\u4e2a UDP \u6a21\u5757&#xff0c;\u6574\u4e2a NDATools \u7f51\u7edc\u8c03\u8bd5\u5de5\u5177\u5c31\u4e0d\u53ea\u662f\u652f\u6301 TCP \u8fd9\u79cd\u9762\u5411\u8fde\u63a5\u7684\u901a\u4fe1&#xff0c;\u4e5f\u80fd\u652f\u6301 UDP \u8fd9\u79cd\u65e0\u8fde\u63a5\u7684\u6570\u636e\u62a5\u901a\u4fe1\u3002TCP \u66f4\u9002\u5408\u5f3a\u8c03\u8fde\u63a5\u7ba1\u7406\u548c\u7a33\u5b9a\u4f20\u8f93&#xff0c;UDP \u66f4\u9002\u5408\u8f7b\u91cf\u7ea7\u3001\u4f4e\u5ef6\u8fdf\u3001\u5e7f\u64ad\u6216\u5c40\u57df\u7f51\u6d88\u606f\u6d4b\u8bd5\u3002\u628a UDP Client \u548c UDP Server \u5206\u5f00\u5b9e\u73b0&#xff0c;\u4e5f\u8ba9\u9879\u76ee\u7ed3\u6784\u66f4\u52a0\u6e05\u695a&#xff0c;\u540e\u9762\u7ee7\u7eed\u6269\u5c55\u5e7f\u64ad\u3001Hex \u6536\u53d1\u3001\u81ea\u52a8\u53d1\u9001\u6d4b\u8bd5\u7b49\u529f\u80fd\u65f6\u4f1a\u66f4\u65b9\u4fbf\u3002<\/p>\n<p>0voice \u00b7 GitHub<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7f51\u7edc\u8c03\u8bd5\u5de5\u5177\u4e2d&#xff0c;TCP\u3001HTTP\u3001WebSocket\u3001MQTT \u8fd9\u4e9b\u6a21\u5757\u90fd\u6709\u6bd4\u8f83\u660e\u663e\u7684\u201c\u8fde\u63a5\u201d\u6982\u5ff5&#xff0c;\u800c UDP \u6a21\u5757\u548c\u5b83\u4eec\u4e0d\u592a\u4e00\u6837\u3002UDP \u662f\u65e0\u8fde\u63a5\u901a\u4fe1&#xff0c;\u4e0d\u9700\u8981\u5148\u5efa\u7acb\u8fde\u63a5&#xff0c;\u53ea\u8981\u77e5\u9053\u76ee\u6807 IP \u548c\u76ee\u6807\u7aef\u53e3&#xff0c;\u5c31\u53ef\u4ee5\u76f4\u63a5\u53d1\u9001\u6570\u636e\u3002 \u672c\u6587\u7ed3\u5408\u6211\u7684 Qt \u7f51\u7edc\u8c03\u8bd5\u5de5\u5177\u9879\u76ee\u4e2d\u7684 UDP \u6a21\u5757\u8fdb\u884c\u6574\u7406&#xff0c;\u9879\u76ee\u4e2d UDP \u88ab\u62c6\u6210\u4e24\u4e2a\u9875\u9762&#xff1a; FormUDPClient   \/\/ UDP \u5ba2\u6237\u7aef\u6a21\u5757<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[218,193,43],"topic":[],"class_list":["post-81554","post","type-post","status-publish","format-standard","hentry","category-server","tag-qt","tag-udp","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010C++\/Qt\u3011Qt \u5b9e\u73b0 UDP \u6d4b\u8bd5\u5de5\u5177\uff1a\u5ba2\u6237\u7aef\u53d1\u9001\u3001\u670d\u52a1\u5668\u76d1\u542c\u4e0e\u6d88\u606f\u6536\u53d1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/81554.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010C++\/Qt\u3011Qt \u5b9e\u73b0 UDP \u6d4b\u8bd5\u5de5\u5177\uff1a\u5ba2\u6237\u7aef\u53d1\u9001\u3001\u670d\u52a1\u5668\u76d1\u542c\u4e0e\u6d88\u606f\u6536\u53d1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5728\u7f51\u7edc\u8c03\u8bd5\u5de5\u5177\u4e2d&#xff0c;TCP\u3001HTTP\u3001WebSocket\u3001MQTT \u8fd9\u4e9b\u6a21\u5757\u90fd\u6709\u6bd4\u8f83\u660e\u663e\u7684\u201c\u8fde\u63a5\u201d\u6982\u5ff5&#xff0c;\u800c UDP \u6a21\u5757\u548c\u5b83\u4eec\u4e0d\u592a\u4e00\u6837\u3002UDP \u662f\u65e0\u8fde\u63a5\u901a\u4fe1&#xff0c;\u4e0d\u9700\u8981\u5148\u5efa\u7acb\u8fde\u63a5&#xff0c;\u53ea\u8981\u77e5\u9053\u76ee\u6807 IP \u548c\u76ee\u6807\u7aef\u53e3&#xff0c;\u5c31\u53ef\u4ee5\u76f4\u63a5\u53d1\u9001\u6570\u636e\u3002 \u672c\u6587\u7ed3\u5408\u6211\u7684 Qt \u7f51\u7edc\u8c03\u8bd5\u5de5\u5177\u9879\u76ee\u4e2d\u7684 UDP \u6a21\u5757\u8fdb\u884c\u6574\u7406&#xff0c;\u9879\u76ee\u4e2d UDP \u88ab\u62c6\u6210\u4e24\u4e2a\u9875\u9762&#xff1a; FormUDPClient  \/\/ UDP \u5ba2\u6237\u7aef\u6a21\u5757\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/81554.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-24T18:58:53+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/81554.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/81554.html\",\"name\":\"\u3010C++\/Qt\u3011Qt \u5b9e\u73b0 UDP \u6d4b\u8bd5\u5de5\u5177\uff1a\u5ba2\u6237\u7aef\u53d1\u9001\u3001\u670d\u52a1\u5668\u76d1\u542c\u4e0e\u6d88\u606f\u6536\u53d1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-24T18:58:53+00:00\",\"dateModified\":\"2026-07-24T18:58:53+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/81554.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/81554.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/81554.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010C++\/Qt\u3011Qt \u5b9e\u73b0 UDP \u6d4b\u8bd5\u5de5\u5177\uff1a\u5ba2\u6237\u7aef\u53d1\u9001\u3001\u670d\u52a1\u5668\u76d1\u542c\u4e0e\u6d88\u606f\u6536\u53d1\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010C++\/Qt\u3011Qt \u5b9e\u73b0 UDP \u6d4b\u8bd5\u5de5\u5177\uff1a\u5ba2\u6237\u7aef\u53d1\u9001\u3001\u670d\u52a1\u5668\u76d1\u542c\u4e0e\u6d88\u606f\u6536\u53d1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/81554.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010C++\/Qt\u3011Qt \u5b9e\u73b0 UDP \u6d4b\u8bd5\u5de5\u5177\uff1a\u5ba2\u6237\u7aef\u53d1\u9001\u3001\u670d\u52a1\u5668\u76d1\u542c\u4e0e\u6d88\u606f\u6536\u53d1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5728\u7f51\u7edc\u8c03\u8bd5\u5de5\u5177\u4e2d&#xff0c;TCP\u3001HTTP\u3001WebSocket\u3001MQTT \u8fd9\u4e9b\u6a21\u5757\u90fd\u6709\u6bd4\u8f83\u660e\u663e\u7684\u201c\u8fde\u63a5\u201d\u6982\u5ff5&#xff0c;\u800c UDP \u6a21\u5757\u548c\u5b83\u4eec\u4e0d\u592a\u4e00\u6837\u3002UDP \u662f\u65e0\u8fde\u63a5\u901a\u4fe1&#xff0c;\u4e0d\u9700\u8981\u5148\u5efa\u7acb\u8fde\u63a5&#xff0c;\u53ea\u8981\u77e5\u9053\u76ee\u6807 IP \u548c\u76ee\u6807\u7aef\u53e3&#xff0c;\u5c31\u53ef\u4ee5\u76f4\u63a5\u53d1\u9001\u6570\u636e\u3002 \u672c\u6587\u7ed3\u5408\u6211\u7684 Qt \u7f51\u7edc\u8c03\u8bd5\u5de5\u5177\u9879\u76ee\u4e2d\u7684 UDP \u6a21\u5757\u8fdb\u884c\u6574\u7406&#xff0c;\u9879\u76ee\u4e2d UDP \u88ab\u62c6\u6210\u4e24\u4e2a\u9875\u9762&#xff1a; FormUDPClient  \/\/ UDP \u5ba2\u6237\u7aef\u6a21\u5757","og_url":"https:\/\/www.wsisp.com\/helps\/81554.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-24T18:58:53+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/81554.html","url":"https:\/\/www.wsisp.com\/helps\/81554.html","name":"\u3010C++\/Qt\u3011Qt \u5b9e\u73b0 UDP \u6d4b\u8bd5\u5de5\u5177\uff1a\u5ba2\u6237\u7aef\u53d1\u9001\u3001\u670d\u52a1\u5668\u76d1\u542c\u4e0e\u6d88\u606f\u6536\u53d1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-24T18:58:53+00:00","dateModified":"2026-07-24T18:58:53+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/81554.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/81554.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/81554.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010C++\/Qt\u3011Qt \u5b9e\u73b0 UDP \u6d4b\u8bd5\u5de5\u5177\uff1a\u5ba2\u6237\u7aef\u53d1\u9001\u3001\u670d\u52a1\u5668\u76d1\u542c\u4e0e\u6d88\u606f\u6536\u53d1"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/81554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=81554"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/81554\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=81554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=81554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=81554"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=81554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}