{"id":21624,"date":"2025-04-19T04:48:40","date_gmt":"2025-04-18T20:48:40","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/21624.html"},"modified":"2025-04-19T04:48:40","modified_gmt":"2025-04-18T20:48:40","slug":"%e3%80%90qt%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e3%80%91tcp%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%92%8c%e5%ae%a2%e6%88%b7%e7%ab%af%e9%80%9a%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/21624.html","title":{"rendered":"\u3010Qt\u7f51\u7edc\u7f16\u7a0b\u3011Tcp\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E4%B8%80%E3%80%81%E7%BC%96%E5%86%99%E6%80%9D%E8%B7%AF-toc\" style=\"margin-left:0px\">\u4e00\u3001\u7f16\u5199\u601d\u8def<\/p>\n<p id=\"1%E3%80%81%E6%9C%8D%E5%8A%A1%E5%99%A8-toc\" style=\"margin-left:40px\">1\u3001\u670d\u52a1\u5668<\/p>\n<p id=\"%EF%BC%881%EF%BC%89%E6%80%BB%E4%BD%93%E6%80%9D%E8%B7%AFwidget.c%EF%BC%88%E4%B8%BB%E7%BA%BF%E7%A8%8B%EF%BC%89-toc\" style=\"margin-left:80px\">\uff081\uff09\u603b\u4f53\u601d\u8defwidget.c\uff08\u4e3b\u7ebf\u7a0b\uff09<\/p>\n<p id=\"%EF%BC%882%EF%BC%89%E8%AF%A6%E7%BB%86%E6%B5%81%E7%A8%8Bwidget.c%EF%BC%88%E4%B8%BB%E7%BA%BF%E7%A8%8B%EF%BC%89-toc\" style=\"margin-left:80px\">\uff082\uff09\u8be6\u7ec6\u6d41\u7a0bwidget.c\uff08\u4e3b\u7ebf\u7a0b\uff09<\/p>\n<p id=\"%EF%BC%881%EF%BC%89%E6%80%BB%E4%BD%93%E6%80%9D%E8%B7%AFchat_thread.c%EF%BC%88%E5%A4%84%E7%90%86%E8%81%8A%E5%A4%A9%E9%80%BB%E8%BE%91%E7%BA%BF%E7%A8%8B%EF%BC%89-toc\" style=\"margin-left:80px\">\uff081\uff09\u603b\u4f53\u601d\u8defchat_thread.c\uff08\u5904\u7406\u804a\u5929\u903b\u8f91\u7ebf\u7a0b\uff09<\/p>\n<p id=\"%EF%BC%882%EF%BC%89%E8%AF%A6%E7%BB%86%E6%B5%81%E7%A8%8Bchat_thread.h%EF%BC%88%E5%A4%84%E7%90%86%E8%81%8A%E5%A4%A9%E9%80%BB%E8%BE%91%E7%BA%BF%E7%A8%8B%EF%BC%89-toc\" style=\"margin-left:80px\">\uff082\uff09\u8be6\u7ec6\u6d41\u7a0bchat_thread.h\uff08\u5904\u7406\u804a\u5929\u903b\u8f91\u7ebf\u7a0b\uff09<\/p>\n<p id=\"2%E3%80%81%E5%AE%A2%E6%88%B7%E7%AB%AF-toc\" style=\"margin-left:40px\">2\u3001\u5ba2\u6237\u7aef<\/p>\n<p id=\"%EF%BC%881%EF%BC%89%E6%80%BB%E4%BD%93%E6%80%9D%E8%B7%AFwidget.c%EF%BC%88%E4%B8%BB%E7%BA%BF%E7%A8%8B%EF%BC%89-toc\" style=\"margin-left:80px\">\uff081\uff09\u603b\u4f53\u601d\u8defwidget.c\uff08\u4e3b\u7ebf\u7a0b\uff09<\/p>\n<p id=\"%EF%BC%882%EF%BC%89%E8%AF%A6%E7%BB%86%E6%80%9D%E8%B7%AFwidget.c%EF%BC%88%E4%B8%BB%E7%BA%BF%E7%A8%8B%EF%BC%89-toc\" style=\"margin-left:80px\">\uff082\uff09\u8be6\u7ec6\u601d\u8defwidget.c\uff08\u4e3b\u7ebf\u7a0b\uff09<\/p>\n<p id=\"%EF%BC%881%EF%BC%89%E6%80%BB%E4%BD%93%E6%80%9D%E8%B7%AFchat_thread.c(%E5%A4%84%E7%90%86%E8%81%8A%E5%A4%A9%E9%80%BB%E8%BE%91%E7%BA%BF%E7%A8%8B)-toc\" style=\"margin-left:80px\">\uff081\uff09\u603b\u4f53\u601d\u8defchat_thread.c(\u5904\u7406\u804a\u5929\u903b\u8f91\u7ebf\u7a0b)<\/p>\n<p id=\"%EF%BC%882%EF%BC%89%E8%AF%A6%E7%BB%86%E6%B5%81%E7%A8%8Bchat_thread.c(%E5%A4%84%E7%90%86%E8%81%8A%E5%A4%A9%E9%80%BB%E8%BE%91%E7%BA%BF%E7%A8%8B)-toc\" style=\"margin-left:80px\">\uff082\uff09\u8be6\u7ec6\u6d41\u7a0bchat_thread.c(\u5904\u7406\u804a\u5929\u903b\u8f91\u7ebf\u7a0b)<\/p>\n<p id=\"%E4%BA%8C%E3%80%81%E5%AE%9E%E7%8E%B0%E6%95%88%E6%9E%9C-toc\" style=\"margin-left:0px\">\u4e8c\u3001\u5b9e\u73b0\u6548\u679c<\/p>\n<p id=\"1%E3%80%81%E6%9C%8D%E5%8A%A1%E5%99%A8-toc\" style=\"margin-left:40px\">1\u3001\u670d\u52a1\u5668<\/p>\n<p id=\"2%E3%80%81%E5%AE%A2%E6%88%B7%E7%AB%AF-toc\" style=\"margin-left:40px\">2\u3001\u5ba2\u6237\u7aef<\/p>\n<hr>\n<p><span style=\"color:#fefcd8\"><span style=\"background-color:#6eaad7\">\u5b8c\u6574\u4ee3\u7801\u8bf7\u5230\u6307\u5b9a\u94fe\u63a5\u4e0b\u8f7d\uff1a<\/span><\/span>Qt\u7f51\u7edc\u7f16\u7a0b-Tcp\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1: \u3010Qt\u7f51\u7edc\u7f16\u7a0b\u3011Tcp\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1<\/p>\n<h2 id=\"%E4%B8%80%E3%80%81%E7%BC%96%E5%86%99%E6%80%9D%E8%B7%AF\">\u4e00\u3001\u7f16\u5199\u601d\u8def<\/h2>\n<h3 id=\"1%E3%80%81%E6%9C%8D%E5%8A%A1%E5%99%A8\">1\u3001\u670d\u52a1\u5668<\/h3>\n<h4 id=\"%EF%BC%881%EF%BC%89%E6%80%BB%E4%BD%93%E6%80%9D%E8%B7%AFwidget.c%EF%BC%88%E4%B8%BB%E7%BA%BF%E7%A8%8B%EF%BC%89\">\uff081\uff09\u603b\u4f53\u601d\u8defwidget.c\uff08\u4e3b\u7ebf\u7a0b\uff09<\/h4>\n<li>\n<p>\u521d\u59cb\u5316\u754c\u9762<\/p>\n<p>\u521b\u5efa\u7a97\u53e3\u3001\u8f93\u5165\u6846\u3001\u6309\u94ae\u7b49\u57fa\u672cUI\u5143\u7d20\u3002<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa\u670d\u52a1\u5668\u5bf9\u8c61<\/p>\n<p>\u5b9e\u73b0 My_tcp_server \u5e76\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<\/p>\n<\/li>\n<li>\n<p>\u5904\u7406\u65b0\u5ba2\u6237\u7aef\u8fde\u63a5<\/p>\n<p>\u5f53\u6709\u65b0\u5ba2\u6237\u7aef\u8fde\u63a5\u65f6\uff0c\u521b\u5efa\u65b0\u7684 Chat_thread \u7ebf\u7a0b\u6765\u5904\u7406\u901a\u4fe1\u3002<\/p>\n<\/li>\n<li>\n<p>\u7ed1\u5b9a\u4fe1\u53f7\u69fd<\/p>\n<p>\u786e\u4fdd\u4e3b\u7ebf\u7a0b\u4e0e\u5ba2\u6237\u7aef\u5904\u7406\u7ebf\u7a0b\u95f4\u7684\u4fe1\u53f7\u69fd\u8fde\u63a5\uff0c\u4f7f\u7528 Qt::QueuedConnection \u5904\u7406\u8de8\u7ebf\u7a0b\u901a\u4fe1\u3002<\/p>\n<\/li>\n<li>\n<p>\u5904\u7406\u6d88\u606f\u4f20\u9012<\/p>\n<p>\u63a5\u6536\u548c\u53d1\u9001\u6d88\u606f\uff0c\u5e76\u5728\u754c\u9762\u4e0a\u66f4\u65b0\u663e\u793a\u3002<\/p>\n<\/li>\n<li>\n<p>\u670d\u52a1\u5668\u542f\u52a8\u4e0e\u5173\u95ed<\/p>\n<p>\u901a\u8fc7\u6309\u94ae\u63a7\u5236\u670d\u52a1\u5668\u7684\u542f\u52a8\u548c\u5173\u95ed\uff0c\u7ba1\u7406\u6240\u6709\u5ba2\u6237\u7aef\u7ebf\u7a0b\u7684\u5b89\u5168\u9000\u51fa\u3002<\/p>\n<\/li>\n<h4 id=\"%EF%BC%882%EF%BC%89%E8%AF%A6%E7%BB%86%E6%B5%81%E7%A8%8Bwidget.c%EF%BC%88%E4%B8%BB%E7%BA%BF%E7%A8%8B%EF%BC%89\">\uff082\uff09\u8be6\u7ec6\u6d41\u7a0bwidget.c\uff08\u4e3b\u7ebf\u7a0b\uff09<\/h4>\n<li>\n<p>\u521b\u5efa Qt \u754c\u9762\u53ca\u8bbe\u7f6e\u7a97\u53e3\u5c5e\u6027\uff1a \u9996\u5148\u901a\u8fc7 ui-&gt;setupUi(this); \u6765\u521d\u59cb\u5316\u7528\u6237\u754c\u9762\uff0c\u5e76\u8bbe\u7f6e\u7a97\u53e3\u6807\u9898\u3001\u5927\u5c0f\u7b49\u57fa\u672c\u5c5e\u6027\u3002\u8fd9\u662f Qt \u9879\u76ee\u7684\u5e38\u89c1\u6b65\u9aa4\uff0c\u901a\u8fc7 .ui \u6587\u4ef6\u751f\u6210\u7684\u7c7b\u8fdb\u884c\u754c\u9762\u7ba1\u7406\u3002<\/p>\n<p>   Widget::Widget(QWidget *parent)<br \/>\n &nbsp;  : QWidget(parent)<br \/>\n &nbsp;  , ui(new Ui::Widget) &nbsp;\/\/ \u521d\u59cb\u5316UI\u5bf9\u8c61<br \/>\n{<br \/>\n &nbsp; &nbsp;ui-&gt;setupUi(this); &nbsp;\/\/ \u8bbe\u7f6eUI\u754c\u9762<br \/>\n &nbsp; &nbsp;this-&gt;setWindowTitle(\\&#8221;&#8211;\u670d\u52a1\u5668&#8211;\\&#8221;); &nbsp;\/\/ \u8bbe\u7f6e\u7a97\u53e3\u6807\u9898<br \/>\n &nbsp; &nbsp;this-&gt;resize(1024, 960); &nbsp;\/\/ \u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<br \/>\n\u200b<br \/>\n &nbsp; &nbsp;ui-&gt;le_ip-&gt;setText(\\&#8221;127.0.0.1\\&#8221;);<br \/>\n &nbsp; &nbsp;ui-&gt;le_port-&gt;setText(\\&#8221;9999\\&#8221;);<br \/>\n} <\/p>\n<\/li>\n<li>\n<p>\u521d\u59cb\u5316\u670d\u52a1\u5668\u5bf9\u8c61 My_tcp_server \u5e76\u5904\u7406\u5ba2\u6237\u7aef\u8fde\u63a5\uff1a<\/p>\n<ul>\n<li>\n<p>\u521b\u5efa tcp_server \u5bf9\u8c61\u4ee5\u5904\u7406\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528 connect \u51fd\u6570\u8fde\u63a5 tcp_server \u7684 new_descriptor \u4fe1\u53f7\u548c\u533f\u540d\u69fd\u51fd\u6570\uff0c\u786e\u4fdd\u4e00\u65e6\u6709\u65b0\u5ba2\u6237\u7aef\u8fde\u63a5\uff0c\u4fbf\u521b\u5efa\u4e00\u4e2a Chat_thread \u6765\u5904\u7406\u8be5\u5ba2\u6237\u7aef\u3002<\/p>\n<\/li>\n<\/ul>\n<p>   this-&gt;tcp_server = new My_tcp_server(this);<br \/>\n\u200b<br \/>\nconnect(tcp_server, &amp;My_tcp_server::new_descriptor, this, [=](qintptr socketDescriptor){<br \/>\n &nbsp; &nbsp;QMessageBox::information(this, \\&#8221;\u63d0\u793a\\&#8221;, \\&#8221;\u65b0\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\uff01\\&#8221;, QMessageBox::Ok, QMessageBox::Information);<br \/>\n\u200b<br \/>\n &nbsp; &nbsp;ui-&gt;btn_send-&gt;setEnabled(true); &nbsp;\/\/ \u542f\u7528\u201c\u53d1\u9001\u6d88\u606f\u201d\u6309\u94ae<br \/>\n\u200b<br \/>\n &nbsp; &nbsp;\/\/ \u521b\u5efa\u65b0\u7ebf\u7a0b\u5904\u7406\u5ba2\u6237\u7aef<br \/>\n &nbsp; &nbsp;Chat_thread *chat_thread = new Chat_thread(socketDescriptor);<br \/>\n &nbsp; &nbsp;chat_thread-&gt;moveToThread(chat_thread); &nbsp;\/\/ \u5c06\u7ebf\u7a0b\u548c\u5bf9\u8c61\u7ed1\u5b9a\u5230\u540c\u4e00\u7ebf\u7a0b\uff0c\u9632\u6b62\u51b2\u7a81<br \/>\n\u200b<br \/>\n &nbsp; &nbsp;thread_list.append(chat_thread);<br \/>\n &nbsp; &nbsp;<br \/>\n &nbsp; &nbsp;\/\/ \u542f\u52a8\u7ebf\u7a0b\u5904\u7406\u5ba2\u6237\u7aef\u901a\u4fe1<br \/>\n &nbsp; &nbsp;chat_thread-&gt;start();<br \/>\n}); <\/li>\n<li>\n<p>\u7ba1\u7406\u5ba2\u6237\u7aef\u7ebf\u7a0b Chat_thread\uff1a<\/p>\n<ul>\n<li>\n<p>\u6bcf\u5f53\u6709\u65b0\u5ba2\u6237\u7aef\u8fde\u63a5\u65f6\uff0c\u521b\u5efa\u4e00\u4e2a Chat_thread \u5e76\u542f\u52a8\u5b83\u5904\u7406\u5ba2\u6237\u7aef\u901a\u4fe1\u3002\u901a\u8fc7 moveToThread \u5c06 Chat_thread \u7684\u6267\u884c\u7ebf\u7a0b\u4e0e\u8be5\u5bf9\u8c61\u4fdd\u6301\u4e00\u81f4\uff0c\u907f\u514d\u8de8\u7ebf\u7a0b\u51b2\u7a81\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528 connect \u7ed1\u5b9a\u7ebf\u7a0b\u4e2d\u7684\u4fe1\u53f7\uff08\u5982\u8fde\u63a5\u65ad\u5f00\u3001\u63a5\u6536\u6d88\u606f\uff09\u548c\u4e3b\u754c\u9762\u69fd\u51fd\u6570\uff0c\u786e\u4fdd\u5ba2\u6237\u7aef\u72b6\u6001\u80fd\u591f\u6b63\u786e\u663e\u793a\u3002<\/p>\n<\/li>\n<\/ul>\n<p>   Chat_thread *chat_thread = new Chat_thread(socketDescriptor);<br \/>\nchat_thread-&gt;moveToThread(chat_thread); &nbsp;\/\/ \u5c06\u7ebf\u7a0b\u4e0e\u5bf9\u8c61\u7ed1\u5b9a\u5728\u540c\u4e00\u7ebf\u7a0b<br \/>\n\u200b<br \/>\nthread_list.append(chat_thread);<br \/>\n\u200b<br \/>\n\/\/ \u8fde\u63a5\u4fe1\u53f7\u548c\u69fd<br \/>\nconnect(chat_thread, &amp;Chat_thread::break_connect, this, [=](){<br \/>\n &nbsp; &nbsp;ui-&gt;te_receive-&gt;append(currentTime + \\&#8221;\\\\n\u3010\u72b6\u6001\u3011\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5&#8230;\\&#8221;);<br \/>\n &nbsp; &nbsp;ui-&gt;btn_send-&gt;setEnabled(false); &nbsp;\/\/ \u7981\u7528\u201c\u53d1\u9001\u6d88\u606f\u201d\u6309\u94ae<br \/>\n});<br \/>\n\u200b<br \/>\nconnect(chat_thread, &amp;Chat_thread::recv_info, this, [=](QString data){<br \/>\n &nbsp; &nbsp;currentTime = QDateTime::currentDateTime().toString(\\&#8221;yyyy-MM-dd hh:mm:ss\\&#8221;);<br \/>\n &nbsp; &nbsp;ui-&gt;te_receive-&gt;append(currentTime + \\&#8221;form client\\\\n &nbsp;  \u3010\u6570\u636e\u3011 \\&#8221; + data); &nbsp;\/\/ \u5728\u6587\u672c\u6846\u4e2d\u663e\u793a\u6d88\u606f<br \/>\n});<br \/>\n\u200b<br \/>\nchat_thread-&gt;start(); &nbsp;\/\/ \u542f\u52a8\u7ebf\u7a0b <\/li>\n<li>\n<p>\u5904\u7406\u542f\u52a8\u548c\u5173\u95ed\u670d\u52a1\u5668\u7684\u6309\u94ae\u4e8b\u4ef6\uff1a<\/p>\n<ul>\n<li>\n<p>on_btn_connect_clicked() \u5904\u7406\u8fde\u63a5\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\uff0c\u542f\u52a8\u6216\u5173\u95ed\u670d\u52a1\u5668\u3002<\/p>\n<\/li>\n<li>\n<p>\u542f\u52a8\u65f6\uff0c\u68c0\u67e5 IP \u5730\u5740\u548c\u7aef\u53e3\u7684\u6709\u6548\u6027\uff0c\u6210\u529f\u540e\u5f00\u59cb\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<\/p>\n<\/li>\n<li>\n<p>\u5173\u95ed\u670d\u52a1\u5668\u65f6\uff0c\u505c\u6b62\u76d1\u542c\uff0c\u5e76\u786e\u4fdd\u6240\u6709\u5df2\u8fde\u63a5\u5ba2\u6237\u7aef\u7ebf\u7a0b\u5b89\u5168\u9000\u51fa\u3002<\/p>\n<\/li>\n<\/ul>\n<p>   void Widget::on_btn_connect_clicked()<br \/>\n{<br \/>\n &nbsp; &nbsp;if (!is_server_running)<br \/>\n &nbsp;  {<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u542f\u52a8\u670d\u52a1\u5668<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;QString ip_address = ui-&gt;le_ip-&gt;text().trimmed();<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;QString port_text = ui-&gt;le_port-&gt;text().trimmed();<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;if (!tcp_server-&gt;listen(QHostAddress(ip_address), port_text.toUInt()))<br \/>\n &nbsp; &nbsp; &nbsp;  {<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;QMessageBox::warning(this, \\&#8221;warning\\&#8221;, \\&#8221;\u670d\u52a1\u5668\u76d1\u542c\u5931\u8d25\\&#8221;);<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return;<br \/>\n &nbsp; &nbsp; &nbsp;  }<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;is_server_running = true;<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;ui-&gt;btn_connect-&gt;setText(\\&#8221;\u5173\u95ed\u670d\u52a1\u5668\\&#8221;);<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;ui-&gt;te_receive-&gt;append(currentTime + \\&#8221;\\\\n\u3010\u72b6\u6001\u3011\u670d\u52a1\u5668\u5f00\u59cb\u76d1\u542c&#8230;\\&#8221;);<br \/>\n &nbsp;  }<br \/>\n &nbsp; &nbsp;else<br \/>\n &nbsp;  {<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u505c\u6b62\u670d\u52a1\u5668\u5e76\u5173\u95ed\u6240\u6709\u5ba2\u6237\u7aef\u7ebf\u7a0b<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;tcp_server-&gt;close();<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;for (Chat_thread *thread : qAsConst(thread_list))<br \/>\n &nbsp; &nbsp; &nbsp;  {<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;thread-&gt;exit();<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;thread-&gt;wait();<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;thread-&gt;deleteLater();<br \/>\n &nbsp; &nbsp; &nbsp;  }<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;thread_list.clear();<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;is_server_running = false;<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp;ui-&gt;btn_connect-&gt;setText(\\&#8221;\u521b\u5efa\u670d\u52a1\u5668\\&#8221;);<br \/>\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ui-&gt;te_receive-&gt;append(currentTime + \\&#8221;\\\\n\u3010\u72b6\u6001\u3011\u670d\u52a1\u5668\u5df2\u505c\u6b62\u76d1\u542c&#8230;\\&#8221;);<br \/>\n &nbsp; &nbsp; &nbsp; }<br \/>\n &nbsp; } <\/li>\n<li>\n<p>\u5904\u7406\u53d1\u9001\u6d88\u606f\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\uff1a<\/p>\n<ul>\n<li>\n<p>\u5f53\u70b9\u51fb\u201c\u53d1\u9001\u6d88\u606f\u201d\u6309\u94ae\u65f6\uff0c\u89e6\u53d1 send_request \u4fe1\u53f7\uff0c\u5229\u7528\u4fe1\u53f7\u69fd\u673a\u5236\u5c06\u8f93\u5165\u7684\u6d88\u606f\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u3002\u9700\u8981\u786e\u4fdd\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u7684\u4fe1\u53f7\u69fd\u901a\u4fe1\u662f\u5f02\u6b65\u8fdb\u884c\u7684\uff08\u901a\u8fc7 Qt::QueuedConnection\uff09\u3002<\/p>\n<\/li>\n<\/ul>\n<p>void Widget::on_btn_send_clicked()<br \/>\n{<br \/>\n &nbsp; &nbsp;QString data = ui-&gt;te_send-&gt;toPlainText().toUtf8();<br \/>\n &nbsp; &nbsp;emit send_request(data); &nbsp;\/\/ \u53d1\u51fa send_request \u4fe1\u53f7<br \/>\n}&lt;\/<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb3.7k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf55\u6b21\u3002\u3010Qt\u7f51\u7edc\u7f16\u7a0b\u3011Tcp\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1_qt tcp\u5ba2\u6237\u7aef\u591a\u7ebf\u7a0b<\/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":[55,218,43,78],"topic":[],"class_list":["post-21624","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-qt","tag-43","tag-78"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Qt\u7f51\u7edc\u7f16\u7a0b\u3011Tcp\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1 - \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\/21624.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Qt\u7f51\u7edc\u7f16\u7a0b\u3011Tcp\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb3.7k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf55\u6b21\u3002\u3010Qt\u7f51\u7edc\u7f16\u7a0b\u3011Tcp\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1_qt tcp\u5ba2\u6237\u7aef\u591a\u7ebf\u7a0b\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/21624.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T20:48:40+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=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/21624.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/21624.html\",\"name\":\"\u3010Qt\u7f51\u7edc\u7f16\u7a0b\u3011Tcp\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T20:48:40+00:00\",\"dateModified\":\"2025-04-18T20:48:40+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/21624.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/21624.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/21624.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Qt\u7f51\u7edc\u7f16\u7a0b\u3011Tcp\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1\"}]},{\"@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":"\u3010Qt\u7f51\u7edc\u7f16\u7a0b\u3011Tcp\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1 - \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\/21624.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Qt\u7f51\u7edc\u7f16\u7a0b\u3011Tcp\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb3.7k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf55\u6b21\u3002\u3010Qt\u7f51\u7edc\u7f16\u7a0b\u3011Tcp\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1_qt tcp\u5ba2\u6237\u7aef\u591a\u7ebf\u7a0b","og_url":"https:\/\/www.wsisp.com\/helps\/21624.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T20:48:40+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/21624.html","url":"https:\/\/www.wsisp.com\/helps\/21624.html","name":"\u3010Qt\u7f51\u7edc\u7f16\u7a0b\u3011Tcp\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T20:48:40+00:00","dateModified":"2025-04-18T20:48:40+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/21624.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/21624.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/21624.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Qt\u7f51\u7edc\u7f16\u7a0b\u3011Tcp\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u901a\u4fe1"}]},{"@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\/21624","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=21624"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/21624\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=21624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=21624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=21624"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=21624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}