QT 多线程和 QSocket 网络编程实例解析

移动开发
Qt的signal/slot的事件机制都是基于主程序的线程的,因此所有的事件都是阻塞型的(blocking),也就是说除非你处理完某个slot事件,不然不会有下个事件被触发。

本文介绍的是QT 多线程QSocket 网络编程实例解析,要实现网络编程,不说这么多,先来看内容。

(1) 带后缀-mt的库才是支持多线程的.

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

(2)编译问题,要添加QT_THREAD_SUPPORT

(30针对线程里面而言,blocking(阻塞的) = synchronous(同步的 )

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

Qt的signal/slot的事件机制都是基于主程序的线程的,因此所有的事件都是阻塞型的(blocking),也就是说除非你处理完某个slot事件,不然不会有下个事件被触发。

(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标签页面添加qtmain.lib qt-mt320.lib

   Project Settings里面C/C++标签页面添加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().

小结:关于QT 多线程QSocket 网络编程实例解析的内容介绍到这,希望本文对你有所帮助。

责任编辑:zhaolei 来源: 互联网
相关推荐

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

多线程异步编程

2011-06-20 13:43:08

Qt Socket 线程

2009-03-12 10:52:43

Java线程多线程

2011-06-22 14:30:44

QT 多线程 线程

2011-06-30 17:40:07

Linux 多线程 Android

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

网络安全多线程代码

2011-07-01 10:35:20

QT 多线程 TCP

2023-11-01 11:20:57

2011-06-02 17:27:49

iphone 多线程

2011-06-13 16:51:19

Qt Socket

2024-03-27 08:47:10

Python多线程Threading

2011-06-22 10:12:08

Qt 线程

2023-06-06 08:17:52

多线程编程Thread类

2023-06-05 07:56:10

线程分配处理器
点赞
收藏

51CTO技术栈公众号