Solved Software crash to use ICMP packages
-
Hello All.
First of all I implement ping with this and it works fine on a console aplication.
This is the ping.h
#ifndef PING_H #define PING_H //LIBS += -lwsock32 //LIBS += -liphlpapi #include <winsock2.h> #include <iphlpapi.h> #include <icmpapi.h> //Ping return: 0 if everthing ok, 1 if something wrong and 2 if there is bad IP int Ping (char ip [], int* status, int *delay); #endif // PING_H
this is the ping.cpp
#include "ping.h" int Ping (char ip [], int* status, int *delay) { // Declare and initialize variables HANDLE hIcmpFile; unsigned long ipaddr = INADDR_NONE; DWORD dwRetVal = 0; char SendData[] = "ªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªª"; LPVOID ReplyBuffer = NULL; DWORD ReplySize = 0; ipaddr = inet_addr(ip); if (ipaddr == INADDR_NONE) { //printf("wrong IP"); return 2; } hIcmpFile = IcmpCreateFile(); if (hIcmpFile == INVALID_HANDLE_VALUE) { //printf("\tUnable to open handle.\n"); //printf("IcmpCreatefile returned error: %ld\n", GetLastError() ); IcmpCloseHandle(hIcmpFile); return 1; } ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData); ReplyBuffer = (VOID*) malloc(ReplySize); if (ReplyBuffer == NULL) { //printf("\tUnable to allocate memory\n"); IcmpCloseHandle(hIcmpFile); free(ReplyBuffer); return 1; } dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 2000); //Reply Buffer will have this struct /*typedef struct icmp_echo_reply { IPAddr Address; ULONG Status; ULONG RoundTripTime; USHORT DataSize; USHORT Reserved; PVOID Data; struct ip_option_information Options; } ICMP_ECHO_REPLY, *PICMP_ECHO_REPLY; */ if (dwRetVal != 0) { PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer; struct in_addr ReplyAddr; ReplyAddr.S_un.S_addr = pEchoReply->Address; *status= pEchoReply->Status; *delay= pEchoReply->RoundTripTime; IcmpCloseHandle(hIcmpFile); free(ReplyBuffer); return 0; } else { IcmpCloseHandle(hIcmpFile); free(ReplyBuffer); return 1; } }
This shown how its works on console
But in a GUI application
what can it be?
This works with a button clicked and pingDelay and samples are defined in MainWindows.h. The qcustomplot works fine.
void MainWindow::on_ButtonPing_clicked() { int iterador=0; //samples number double maxValue=0; //MaxPingDelay int status; int delay; QByteArray cadena; cadena=ui->lineEdit->text().toLatin1(); while(!stopPing) { Ping(cadena.data(), &status, &delay); if(iterador==100) { pingDelay.pop_front(); pingDelay.push_back((double) delay); maxValue=0; } else { pingDelay.append((double) delay); samples.append(iterador); iterador++; } if(iterador!=0) { for(int i=0;i<pingDelay.size();i++) { if(pingDelay.at(i) > pingDelay.at(i+1) && pingDelay.at(i)>maxValue) { maxValue=pingDelay.at(i); } } } // create graph and assign data to it: ui->widget->addGraph(); ui->widget->graph(0)->setData(samples, pingDelay); // give the axes some labels: ui->widget->xAxis->setLabel("samples"); ui->widget->yAxis->setLabel("Ping delay"); // set axes ranges, so we see all data: ui->widget->xAxis->setRange(0, pingDelay.size()); ui->widget->yAxis->setRange(0, maxValue); ui->widget->replot(); QThread::msleep(500); //500 milisegundos delay } stopPing=false; }
And the condition to stop the ping is here, in another button clicked
void MainWindow::on_ButtonStopPing_clicked() { stopPing=true; }
-
Hi
You should use the debugger and see what line that make it crash.Note calling
QThread::msleep();
in the main gui thread is often not a good idea since it
delay redrawing and pauses event handling. -
Thanks for the reply.
The infinite loop ping sequence works fine in console application. The graph plot works fine with a sequence coming up from (for example) USB serial data. Both crash at the running time on windows.
is there a way to test an application on windows when its running (I means without code that it'd be a debug sentence)? Can it be that this real-time processing doesnt work fine in a standard desktop computer on windows? I tried it with two pcs with windows 10 and similar characteristic.
The last thing Im thinking to do is get in with raw socket and set up the ip and icmp header by myself, send it and wait until the echo comes but also I want to know whats happening with it.
-
Hi
When it not possible to actual debug an application where crash happens, you can include a small logging system and add debug output to code so you can see how far it comes.
It should not matter if console or not. the code will still work as normally even if you block the event loop for period of time.I think its something else.
-
Hello.
Thanks. The problem was fixed. I used QTimer to set a signal every 1000ms to send the ICMP packages, process it and plot it. It looks like the system couldnt works sending networks packages and plotting at the same times it manages the GUI, due to it could send a continuous icmp package and process it using console application without a crash and plot a continuous data stream without crash but with both It doesnt.
I dont know why there was that problem. Maybe it has to be because the IRQ 0 interrupt that manages the multitasking couldnt handle the complete process and it lost the thread. With the timer and the signal (using QTimer class), it can be fixed.
-
@Julian
Ok super.
Using a timer, you made it more event loop friendly. it might have made app
run more smoothly.