码迷,mamicode.com
首页 > 其他好文 > 详细

qt实现搜索LAN设备

时间:2014-06-04 19:32:54      阅读:591      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

先看效果:

bubuko.com,布布扣

 

bubuko.com,布布扣

 

bubuko.com,布布扣

 

bubuko.com,布布扣

 

代码上:

Work.cpp

bubuko.com,布布扣
#include <QApplication>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QVBoxLayout>

#include "Work.h"
#include "P2PSearchProtocol.h"
#include <QTextStream>

MyQPushButton *pQPushButton;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget *pQWidget = new QWidget;
    pQWidget->setWindowTitle("LAN Devices");
    pQWidget->resize(450, 200); 
    
    pQPushButton = new MyQPushButton();
    pQPushButton->setText("Search");
    QObject::connect( pQPushButton,SIGNAL(clicked()),pQPushButton,SLOT(HandleButton()) );
    
    QTableWidget *pQTableWidget = new QTableWidget;
    pQTableWidget->setColumnCount(3); 
    pQTableWidget->setRowCount(10);
    pQTableWidget->resize(400, 200); 
    QStringList header;
    header<<"ID"<<"Name"<<"IP";
    pQTableWidget->setHorizontalHeaderLabels(header);
    
    //pQTableWidget->setItem(0,0,new QTableWidgetItem("Jan"));
    //pQTableWidget->setItem(1,0,new QTableWidgetItem("Feb"));
    //pQTableWidget->setItem(2,0,new QTableWidgetItem("Mar"));

    QVBoxLayout *pQVBoxLayout = new QVBoxLayout;
    pQVBoxLayout->addWidget(pQPushButton);
    pQVBoxLayout->addWidget(pQTableWidget);
    
    pQPushButton->m_pQTableWidget=pQTableWidget;
    pQPushButton->m_pQWidget=pQWidget;
    
    pQWidget->setLayout(pQVBoxLayout);
    pQWidget->show();
    
    return a.exec();
}
bubuko.com,布布扣

Work.h

bubuko.com,布布扣
#include <QPushButton>
#include <QMessageBox> 
#include <string>

#include <SSTREAM>
#include <process.h>

#include <QDebug>
#include <QFile>
#include <QTextStream>

#include <QtNetwork>
#include <QtNetwork/qudpsocket.h>
#include <QtNetwork/qhostaddress.h>

#include <QTableWidget>
#include <QTableWidgetItem>
#include <QWidget>
#include <vector>

#include "P2PSearchProtocol.h"

typedef unsigned int (__stdcall *FuncThread)(void *);

class MyQPushButton:public QPushButton
{
Q_OBJECT

 public:
    std::vector<PSEARCH_RESP*>        m_vtRelt;
    QTableWidget*                     m_pQTableWidget;
    QWidget*                        m_pQWidget;
    QString                            m_qstrIP;

    public slots:
        void HandleButton()
        {
            //QMessageBox::about( 0, "Invoke Starting", "Invoke Starting..."); 
            unsigned ulThreadId;
            void * hThreadProc=
                (void *)_beginthreadex(NULL,0,(FuncThread)ThreadProc,this,0,&ulThreadId);
            if(NULL==hThreadProc)
            {
                QMessageBox::about( 0, "Thread Failed", "Creating the thread failed!"); 
                return;
            }

            QUdpSocket* pQUdpSocket=new QUdpSocket();
            
            QHostAddress* host=new QHostAddress("192.168.0.114");
            //pQUdpSocket->bind(*host,BROADCAST_SEND_PORT_L);
            QList<QHostAddress> list = QNetworkInterface::allAddresses();
            for(int nIter=0; nIter<list.count(); nIter++)
            {
              //if(!list[nIter].isLoopback())
               if (list[nIter].protocol() == QAbstractSocket::IPv4Protocol )
               {
                  m_qstrIP=list[nIter].toString();
                  m_pQWidget->setWindowTitle( "LAN Devices   ---   Your IP : "+ m_qstrIP );
                  host=new QHostAddress( m_qstrIP );
                  //QMessageBox::about( 0, "Your IP", m_qstrIP ); 
                  pQUdpSocket->bind(*host,BROADCAST_SEND_PORT_L);
                  break;
               }
            }
            pQUdpSocket->bind(QHostAddress::LocalHost, BROADCAST_SEND_PORT_L);
            
            SEARCH_REQ req={STARTCODE,CMD_GET};
            QByteArray datagram = (char*)&req;//"Broadcast message : Hello" + QByteArray::number(123);
            pQUdpSocket->writeDatagram(datagram.data(), datagram.size(),
                             QHostAddress::Broadcast, BROADCAST_SEND_PORT_L);
        
            QMessageBox::about( 0, "Send The Data", QString( "Sending Over" ) ); 
            int index=0;
            while(pQUdpSocket->hasPendingDatagrams())
            {
                 QByteArray datagramRecv;
                 datagramRecv.resize(pQUdpSocket->pendingDatagramSize());
                 QHostAddress sender;
                 quint16 senderPort;
                 pQUdpSocket->readDatagram(datagramRecv.data(), datagramRecv.size(),
                                         &sender, &senderPort);
                                         
                int row_count=m_pQTableWidget->rowCount();
                if(row_count<index+1)     m_pQTableWidget->setRowCount(row_count+10);
                
                PSEARCH_RESP ptr=(PSEARCH_RESP)datagramRecv.data();
                if(    sender.toString()==QString(m_qstrIP)    )
                {
                    //QMessageBox::about( 0, "Sender", QString::number( strlen(ptr->dwDeviceID) ) ); 
                    continue;
                }
                m_pQTableWidget->setItem(index,2,new QTableWidgetItem( sender.toString() ));
                m_pQTableWidget->setItem(index,1,new QTableWidgetItem( QString(L) ));
                m_pQTableWidget->setItem(index,0,new QTableWidgetItem( QString(QLatin1String( ptr->dwDeviceID )) ));
                index++;
            } 
            QMessageBox::about( 0, "Receive The Data", QString( "Receiving Over" ) ); 
            
        }
        
    private:
        static unsigned long __stdcall ThreadProc(void* pParam)
        {
        /*
            MyQPushButton* pMyQPushButton=(MyQPushButton*)pParam;
        
            QUdpSocket* pQUdpSocket=new QUdpSocket();
            QHostAddress* host=new QHostAddress("192.168.0.114");
            //pQUdpSocket->bind(QHostAddress::LocalHost, 7756);
            pQUdpSocket->bind(*host,BROADCAST_SEND_PORT_L);
            SEARCH_REQ req={STARTCODE,CMD_GET};
            QByteArray datagram = (char*)&req;//"Broadcast message : Hello" + QByteArray::number(123);
            pQUdpSocket->writeDatagram(datagram.data(), datagram.size(),
                             QHostAddress::Broadcast, BROADCAST_SEND_PORT_L);
        
            for(int i=0;i<5000;i++);
            int index=0;
            while(pQUdpSocket->hasPendingDatagrams())
            {
                 QByteArray datagramRecv;
                 datagramRecv.resize(pQUdpSocket->pendingDatagramSize());
                 QHostAddress sender;
                 quint16 senderPort;
                 pQUdpSocket->readDatagram(datagramRecv.data(), datagramRecv.size(),
                                         &sender, &senderPort);
                                         
                int row_count=pMyQPushButton->m_pQTableWidget->rowCount();
                if(row_count<index+1)     pMyQPushButton->m_pQTableWidget->setRowCount(row_count+10);
                
                PSEARCH_RESP ptr=(PSEARCH_RESP)datagramRecv.data();
                if(    sender.toString()==QString("192.168.0.114")    )
                {
                    //QMessageBox::about( 0, "Sender", QString::number( strlen(ptr->dwDeviceID) ) ); 
                    continue;
                }
                pMyQPushButton->m_pQTableWidget->setItem(index,2,new QTableWidgetItem( sender.toString() ));
                pMyQPushButton->m_pQTableWidget->setItem(index,1,new QTableWidgetItem( QString(‘L‘) ));
                pMyQPushButton->m_pQTableWidget->setItem(index,0,new QTableWidgetItem( QString(QLatin1String( ptr->dwDeviceID )) ));
                index++;
            } 
            */
            return 0;
        }
        
};
bubuko.com,布布扣

Compile_nmake.bat

D:
cd D:\Work\0529
qmake -project
qmake "QT += core network"
moc Work.h -o moc_Work.cpp
nmake
pause

 

环境:

vc6+qt-win-opensource-src-4.3.4.zip

qt实现搜索LAN设备,布布扣,bubuko.com

qt实现搜索LAN设备

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/tiancun/p/3759342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!