Qt Socket 多线程操作
来源:互联网 发布:矢仓枫子 知乎 编辑:程序博客网 时间:2024/06/03 12:34
Qt 关于Socket 多线程通信
最近有个项目需要用到Qt中的socket通信,于是就查阅网上的一些资料和QT API的文档,过程虽然比较艰难,但成果确实~,你懂得,现在就和大家分享它们的用法.
首先Qt关于关于Socket需要使用QTcpServer和QTcpSocket,其中QTcpServer做为服务端,而QTcpSocket则作为客户端.通过查看QTcpServer的API,它可以通过两个信号来监测客户端的连接.incomingConnection和newConnection.本文主要介绍incomingConnection的用法,关于newConnection的用法,可以度娘.
1 incomingConnection,它的定义如下:
void QTcpServer::incomingConnection(qintptr socketDescriptor)
This virtual function is called by QTcpServer when a new connection is available. The socketDescriptor argument is the native socket descriptor for the accepted connection.
The base implementation creates a QTcpSocket, sets the socket descriptor and then stores the QTcpSocket in an internal list of pending connections. Finally newConnection() is emitted.
Reimplement this function to alter the server's behavior when a connection is available.
If this server is using QNetworkProxy then the socketDescriptor may not be usable with native socket functions, and should only be used with QTcpSocket::setSocketDescriptor().
Note: If you want to handle an incoming connection as a new QTcpSocket object in another thread you have to pass the socketDescriptor to the other thread and create the QTcpSocket object there and use its setSocketDescriptor() method.
通过定义,我们需要重写该函数:
class myserver :public QTcpServer{protected: virtual void incomingConnection(qintptr socketDescriptor);};当有新的建立连接时,我们需要起一个线程.
void myserver::incomingConnection(qintptr socketDescriptor){ qDebug()<<"New Connect is connect"<<socketDescriptor; socketThread * thread=new socketThread(); thread->write_ptr(socketDescriptor); thread->start();}
关于线程的定义如下:
class socketThread :public QThread{public: //定义自己需要的方法或变量 qintptr ptr; QTcpSocket * socket;//客户端的定义 void write_ptr(qintptr p){ ptr=p; } protected: virtual void run()}线程起来之后,则需要在起来的线程中完成相应的初始化:
void socketThread::run(){ socket=new QTcpSocket(); socket->setSocketDescriptor(ptr);//客户端的初始化 if(socket->waitForConnected(10000)){ qDebug()<<"Connect Success"; } else{ qDebug()<<"Connect Fail"; } while(true){//完成需要的功能 }}最后,需要服务器监听IP的端口:
myserver * server; server=new myserver(); server->listen(QHostAddress::AnyIPv4,6665);当有新的连接请求时,就会进入到incomingConnection代码中.
至此,关于Socket的多线程的操作流程完毕.这里面还涉及到线程间的资源共享,这个大家可以度娘,度娘的资源还是比较多的.
以上是关于一个项目的使用总结,里面有什么不对或有误的地方,请大家指出,我们一起讨论学习.
- Qt Socket 多线程操作
- qt 多线程socket问题
- 多线程socket操作
- Qt 多线程操作
- 关于socket的多线程操作。
- Qt中采用多线程实现Socket编程
- Qt中采用多线程实现Socket编程
- Qt中采用多线程实现Socket编程
- Qt中采用多线程实现Socket编程
- Qt中使用windows socket api vc多线程socket
- [Qt学习篇]Qthread实现多线程操作
- QT4中构建多线程的服务器and QT'Socket
- QT4中构建多线程的服务器and QT'Socket
- Qt中采用多线程Socket编程的问题
- QT4中构建多线程的服务器and QT'Socket
- QT编程问题小结(编译、多线程、UDP Socket等)
- QT4中构建多线程的服务器and QT'Socket
- qt socket
- java web开发(四) 接口开发补坑1
- android bionic linker debug enable
- 视频笔记
- ? 枚举类实现接口
- 小白教你学习c++(6)几种常见的类型转换
- Qt Socket 多线程操作
- 【BFS+状压】HDU_5025_ Saving Tang Monk
- ORACLE 中的 ROW_NUMBER() OVER() 分析函数的用法
- String,StringBuffer与StringBuilder的区别
- 树链剖分
- 【转】前端框架基础:Babel & package.json & npm & nodemon
- 一位数组>>>>>>>>>>约瑟夫环
- 树状数组实现 区间修改+区间查询
- HDU-2017 多校训练赛4-1004-Dirt Ratio