Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Pacp - Send Data



  • Hi,
    I use "pcap".
    I wanna send and receive data.
    But I have an error while sending the packet and "pcap_sendpacket" function return "-1".

    myproject.pro

    INCLUDEPATH += D:/WpdPack/Include
    LIBS += -L"D:/WpdPack/Lib" -lwpcap -lPacket
    code_text
    

    main.cpp

    QThread thread;
        thread.setObjectName("PCAPNetworkThread");
        mySerialPort.moveToThread(&thread);
        QObject::connect(&thread, SIGNAL(started()), &pcapNetwork, SLOT(openLanPort()));
        thread.start();
    

    PCAPNetwork.cs

    void PCAPNetwork::openLanPort()
    {
        pcap_if_t *alldevs;
        pcap_if_t *d;
        int inum;
        int i=0;    
        char errbuf[PCAP_ERRBUF_SIZE];
        if (pcap_findalldevs( &alldevs, errbuf) == -1)
        {
            //Error in pcap_findalldevs
            return;
        }
    
        /* Print the list */
        for(d=alldevs; d; d=d->next)
        {
    //print name and description        
        }
    
        if(i==0)
        {
            //No interfaces found! Make sure WinPcap is installed
            return;
        }
    
        //Enter the interface number
        inum=1;//I do not know how can I select adapter with mac address
    
        if(inum < 1 || inum > i)
        {
            //Interface number out of range
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return;
        }
    
        /* Jump to the selected adapter */
        for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
    
        /* Open the device */
        if ( (adhandle= pcap_open_live(d->name,     
                                       65536, 
                                       1,              
                                       1000,  
                                       NULL  
                                       ) ) == NULL)
        {
            //Unable to open the adapter.
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return;
        }
        //listening
        pcap_freealldevs(alldevs);
         
        commandData();
    
        pcap_loop(adhandle, 0, packet_handler, NULL);
    }
    void PCAPNetwork::commandData(){   
        QByteArray data;
        data[0]=commands->COMMANDS_DATA;
        sendData(data);
    }
    void PCAPNetwork::sendData(const QByteArray &data)
    {
    const u_char* p;
    std::memcpy(&p,data.constData(),data.size());
    if(pcap_sendpacket(adhandle, 
                           p, 
                           sizeof(p)
                           ) != 0 )
        {
            //Error sending the packet
    qDebug()<<"Error: "<<pcap_geterr( adhandle );
            return;
        }
    
    }
    

  • Qt Champions 2019

    @neda said in Pacp - Send Data:

    fprintf( stderr,"\nError sending the packet: \n", pcap_geterr( adhandle ));

    What does this line print out?
    And I'm not sure what this has to do with Qt.



  • @jsulm said in Pacp - Send Data:

    What does this line print out?

    PacketSendPacket failed

    And I'm not sure what this has to do with Qt.

    I did not find a document or a good sample to use "pcap" in Qt.


  • Lifetime Qt Champion

    @neda
    Hi
    And you did install winpcap/Win10Pcap so the driver is running?
    Also, you are using visual studio compiler ?
    Im not sure it works with mingw.



  • @mrjj said in Pacp - Send Data:

    Also, you are using visual studio compiler ?

    Qt 5.12.5 MSVC2017 32bit

    And you did install winpcap/Win10Pcap so the driver is running?

    I installed "http://www.win10pcap.org/download/Win10Pcap-v10.2-5002.msi".

    Now "pcap_sendpacket" return 0, It's OK.
    But I do not have a correct answer from the device.

    I have a "QByteArray" Data for send such as:

    "\x02\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
    

    I don't know this code is true or not.

    void PCAPNetwork::sendData(const QByteArray &data)
    {
    const u_char* p= reinterpret_cast<const uchar *>(data.constData());
    //std::memcpy(&p,data.constData(),data.size());
    pcap_sendpacket(adhandle, 
                                            p, 
                                            sizeof(p)
                                            );
    

  • Lifetime Qt Champion

    @neda
    Hi
    from a quick glance - it looks ok.
    Should not truncate values.



  • I think I was wrong in reading data.
    The device sends a lot of data in real-time.
    I read them by this code.

    void PCAPNetwork::packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
    {
    

    How can I convert "pkt_data" to decimal?


  • Lifetime Qt Champion

    @neda said in Pacp - Send Data:

    u_char

    But is this not a byte already ?

    If you place cursor on u_char and press F2, what is it then defined as?



  • @mrjj

    u_char = unsigned char

    qDebug()<<"data: "<<pkt_data;
    

    output:

    data:  0x46c16bc
    

    I don't know how can I convert it to decimal.


  • Lifetime Qt Champion

    @neda
    Is it zero terminated ?

    That is the address it writes out. Not the actual data i think.

    You can convert it to bytearray
    QByteArray databuf = QByteArray(reinterpret_cast<char*>(pkt_data), header->len);
    (i think but not 100% sure header->len is data len. check docs)



  • @mrjj said in Pacp - Send Data:

    Not the actual data i think.

    You right.
    The device sends a lot of data in real-time.
    How can I read the actual data?


  • Lifetime Qt Champion

    @neda
    The data is in pkt_data as far as i can see from docs
    so try
    QByteArray databuf = QByteArray(reinterpret_cast<char*>(pkt_data), header->len);
    qDebug()<<"data: "<<databuf ;


Log in to reply