偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

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

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

例如windows下面的qt-mt320.lib,其他平臺(tái)libqt-mt

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

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

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

Qt的signal/slot的事件機(jī)制都是基于主程序的線程的,因此所有的事件都是阻塞型的(blocking),也就是說(shuō)除非你處理完某個(gè)slot事件,不然不會(huì)有下個(gè)事件被觸發(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 

.

一個(gè)例子:

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

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

   Project Settings里面C/C++標(biāo)簽頁(yè)面添加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)容介紹到這,希望本文對(duì)你有所幫助。

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

2011-06-13 10:03:19

Qt 多線程 編程

2011-06-30 17:31:32

Qt 多線程 信號(hào)

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多線程多線程概念多線程入門(mén)

2021-03-01 11:20:13

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

2011-06-13 16:51:19

Qt Socket

2011-06-02 17:27:49

iphone 多線程

2023-11-01 11:20:57

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ù)棧公眾號(hào)