成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

QT 多線程和 QSocket 網(wǎng)絡(luò)編程實(shí)例解析

移動開發(fā)
Qt的signal/slot的事件機(jī)制都是基于主程序的線程的,因此所有的事件都是阻塞型的(blocking),也就是說除非你處理完某個slot事件,不然不會有下個事件被觸發(fā)。

本文介紹的是QT 多線程QSocket 網(wǎng)絡(luò)編程實(shí)例解析,要實(shí)現(xiàn)網(wǎng)絡(luò)編程,不說這么多,先來看內(nèi)容。

(1) 帶后綴-mt的庫才是支持多線程的.

例如windows下面的qt-mt320.lib,其他平臺libqt-mt

(2)編譯問題,要添加QT_THREAD_SUPPORT

(30針對線程里面而言,blocking(阻塞的) = synchronous(同步的 )

non-blocking (非阻塞的)  = asynchronous(異步的 )

Qt的signal/slot的事件機(jī)制都是基于主程序的線程的,因此所有的事件都是阻塞型的(blocking),也就是說除非你處理完某個slot事件,不然不會有下個事件被觸發(fā)。

(4)QSocket,QSocketNotifier不能和QThread一起使用

  1. QSocket is for non-blocking IO, it uses some polling like poll() or select() internally and notifies the actual code by emitting signals.  
  2. So QSocket is for use with only the event loop thread, for example in a client with only one socket or a server with very few connections.  
  3. If you want a threaded approach use QThread and QSocketDevice.  
  4. Put one SocketDevice into listening mode and on accept() create a Handler Thread for the socket file descriptor. 
  5. Use the second QSocketDevice constructor to initialise the Connection's socket device instance.  
  6. The server does  
  7. bind()  
  8. listen()  
  9. and  
  10. accept()  
  11. When accept returns it has the filedescriptor of the connection's socket which you can pass to another QSocketDevice constructor.  
  12. The client does connect() to establish the connection.  
  13. Both use readBlock/writeBlock/waitForMore to transfer data 

.

一個例子:

(1)用VC6.0新建個Win32 Console Application工程

(2)Project Settings里面Link標(biāo)簽頁面添加qtmain.lib qt-mt320.lib

   Project Settings里面C/C++標(biāo)簽頁面添加QT_THREAD_SUPPORT

(3)源代碼文件(main.cpp):

  1. #include <qthread.h> 
  2. class MyThread : public QThread   
  3. {      
  4. public:  
  5.     virtual void run();  
  6. };  
  7. void MyThread::run()  
  8. {  
  9.     for( int count = 0; count < 20; count++ )   
  10.     {  
  11.         sleep( 1 );  
  12.         qDebug( "Ping!" );  
  13.     }  
  14. }  
  15. int main()  
  16. {  
  17.     MyThread a;  
  18.     MyThread b;  
  19.     a.start();  
  20.     b.start();  
  21.     a.wait();  
  22.     b.wait();  

注釋:

This will start two threads, each of which writes Ping! 20 times to the screen and exits.

The wait() calls at the end of main() are necessary because exiting main() ends the program,

unceremoniously killing all other threads.

Each MyThread stops executing when it reaches the end of MyThread::run(),

just as an application does when it leaves main().

小結(jié):關(guān)于QT 多線程QSocket 網(wǎng)絡(luò)編程實(shí)例解析的內(nèi)容介紹到這,希望本文對你有所幫助。

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-06-13 10:03:19

Qt 多線程 編程

2011-06-30 17:31:32

Qt 多線程 信號

2011-06-16 10:38:13

Qt多線程編程

2023-06-13 13:39:00

多線程異步編程

2009-03-12 10:52:43

Java線程多線程

2011-06-30 17:40:07

Linux 多線程 Android

2011-06-20 13:43:08

Qt Socket 線程

2011-06-22 14:30:44

QT 多線程 線程

2011-07-01 11:18:50

Qt 多線程

2011-06-22 14:38:09

QT 多線程 線程安全

2013-07-16 10:12:14

iOS多線程多線程概念多線程入門

2021-03-01 11:20:13

網(wǎng)絡(luò)安全多線程代碼

2011-06-13 16:51:19

Qt Socket

2023-11-01 11:20:57

2011-06-02 17:27:49

iphone 多線程

2011-06-22 10:12:08

Qt 線程

2011-07-01 10:35:20

QT 多線程 TCP

2024-03-27 08:47:10

Python多線程Threading

2011-06-22 15:50:45

QT 線程

2011-06-30 18:15:36

Qt 線程 同步
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 亚洲免费在线视频 | 免费看一级毛片 | 色婷婷av一区二区三区软件 | 久久91精品久久久久久9鸭 | 国产精品成人久久久久a级 久久蜜桃av一区二区天堂 | 亚洲最新在线视频 | 中文字幕在线免费 | 欧美精品一区免费 | 亚洲一级在线 | 国产亚洲成av人片在线观看桃 | 亚洲欧美在线观看视频 | 久久久精 | 国产精品久久久久久52avav | 成人影院在线 | 精品久久久久久亚洲综合网 | 免费毛片www com cn| 99成人 | 麻豆精品国产91久久久久久 | 亚洲福利在线视频 | 视频一区二区中文字幕 | 午夜在线| 国产高清一区二区三区 | 成人午夜在线 | 玖玖综合网| 亚洲免费福利视频 | 午夜av一区二区 | 精品中文字幕一区 | 亚洲最大成人综合 | 久久中文一区二区 | 在线观看免费高清av | 伊人网在线播放 | 色综合视频 | 涩涩视频大全 | 最新国产精品精品视频 | 精品成人69xx.xyz| 国产成人jvid在线播放 | 亚洲一区二区三区在线播放 | 在线观看国产精品一区二区 | 成人国产a | 国产精品a一区二区三区网址 | 中文字幕在线免费 |