关于QT多线程子线程使用信号和

主线程最后必须调用exit()和wait()函数

源码地址 http://download.csdn.net/download/u010261063/10185891


#ifndef QSYSENDTHREAD_H
#define QSYSENDTHREAD_H

#include <QThread>
#include<QTcpSocket>
#include<common.h>
#include<QTimer>
class QSYSendThread : public QThread
{
    Q_OBJECT
public:
    explicit QSYSendThread(QObject *parent = 0);

signals:

public slots:
    void ClientClosed();
    void ClientError(QAbstractSocket::SocketError socketError);
    void TimeOut();
public:
    void run();
private:
    QTcpSocket *m_client;
    QString m_conectIP;
    bool m_bconnect;
    uint m_connectPort;
    QTimer *m_pTimer;
};

#endif // QSYSENDTHREAD_H


源文件

#include "qsysendthread.h"

QSYSendThread::QSYSendThread(QObject *parent) : QThread(parent)
{

}
void QSYSendThread::run()
{
    m_conectIP = g_EnvVariant.m_conectToIp;
    m_connectPort = g_EnvVariant.m_conectToPort;
    m_client = new QTcpSocket();
    m_client->connectToHost(m_conectIP,m_connectPort);
    connect(m_client,QTcpSocket::disconnected,this,QSYSendThread::ClientClosed,Qt::DirectConnection);
    connect(m_client,QTcpSocket::readyRead,this,QSYSendThread::ClientClosed,Qt::DirectConnection);
    if(m_client->state()==QAbstractSocket::ConnectedState)
    {
        cout<<"connnect to server fail !!! ";
    }
    else
    {
        cout<<"connnect to server successful !!! ";
    }
    m_pTimer = new QTimer();
    m_pTimer->start(2000);
    connect(m_pTimer,QTimer::timeout,this,QSYSendThread::TimeOut,Qt::DirectConnection);
    exec();
    delete m_client;
    delete m_pTimer;
}
void QSYSendThread::ClientClosed()
{
    cout<<"server is closed !!! ";
}
void QSYSendThread::ClientError(QAbstractSocket::SocketError /*socketError*/)
{

}
void QSYSendThread::TimeOut()
{

    if(m_client->state()==QAbstractSocket::UnconnectedState)
    {
        m_client->connectToHost(m_conectIP,m_connectPort);
        cout<<"connnect to server  !!! ";
    }
    cout<<"timeout!! ";
}



以下是使用Python实现多线程域名搜集的示例代码: ```python import threading import queue import socket # 定义线程数 THREAD_NUM = 10 # 定义目标域名 TARGET_DOMAIN = 'example.com' # 定义常见的域名前缀 SUB_DOMAINS = ['www', 'mail', 'ftp', 'vpn', 'admin', 'blog', 'test'] # 定义队列用于存储域名 sub_domains_queue = queue.Queue() # 定义锁,用于线程安全 lock = threading.Lock() # 定义函数,用于检查域名是否存在 def check_sub_domain(sub_domain): try: # 尝试解析域名 ip = socket.gethostbyname(sub_domain + '.' + TARGET_DOMAIN) with lock: # 如果解析成功,则将域名加入队列 sub_domains_queue.put(sub_domain) print('[*] Found sub domain:', sub_domain, ip) except: pass # 定义函数,用于创建并启动线程 def create_and_start_threads(): threads = [] for i in range(THREAD_NUM): t = threading.Thread(target=worker) threads.append(t) t.start() for t in threads: t.join() # 定义函数,用于线程工作 def worker(): while True: try: # 从队列中获取域名 sub_domain = sub_domains_queue.get(timeout=1.0) check_sub_domain(sub_domain) except: break # 主函数 def main(): # 将常见的域名前缀与目标域名拼接,加入队列 for sub_domain in SUB_DOMAINS: sub_domains_queue.put(sub_domain) # 创建并启动线程 create_and_start_threads() if __name__ == '__main__': main() ``` 以上代码使用多线程技术,将常见的域名前缀与目标域名拼接后,加入队列。然后创建多个线程,从队列中获取域名,尝试解析域名,如果解析成功,则将域名加入队列。最终输出所有解析成功的域名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Car12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值