Check connection to IP

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2366
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Check connection to IP

Post by evstevemd »

If you have a specific IP then hard code it an try to connect. If it succeeds go ahead and do your stuffs. Else do not. The easiest way is to have server, simple server that spews back some response and that server should be accessible only within VPN. Then test connection to that server.

there are many classes to accomplish that!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Wanderer82
Super wx Problem Solver
Super wx Problem Solver
Posts: 268
Joined: Tue Jul 26, 2016 2:00 pm

Re: Check connection to IP

Post by Wanderer82 »

And this exactly is what I don't know how to do. How do I try to connect to the IP and check whether I got an answer? I'm still kind of a novice in C programming 😇
User avatar
[email protected]
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 221
Joined: Wed Jul 29, 2020 6:06 pm

Re: Check connection to IP

Post by [email protected] »

I found this source code for "ping" functionality which claims to work cross-platform:
https://github.com/sryze/ping/blob/master/src/ping.c

With a little bit of refactoring it should be possible to turn it into a universally usable function.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2366
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Check connection to IP

Post by evstevemd »

Hi,
Wanderer82 wrote: Mon Aug 01, 2022 11:29 am And this exactly is what I don't know how to do. How do I try to connect to the IP and check whether I got an answer? I'm still kind of a novice in C programming 😇
This is how I would quickly do:
1. Install this server and run it on the IP that I expect to test. It is simple and free. Check it out here: https://github.com/typicode/json-server#getting-started
2. I will try to connect to this server and see if I get reponse 200 when it is accessible or timeout otherwise. I would have tried wxHttp but there are advanced classes that IMHO you do not need in this simple case. See wxWiki for wxHttp here: https://wiki.wxwidgets.org/WxHTTP
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Wanderer82
Super wx Problem Solver
Super wx Problem Solver
Posts: 268
Joined: Tue Jul 26, 2016 2:00 pm

Re: Check connection to IP

Post by Wanderer82 »

That all looks quite a big efffort for such an easy task. I think I'd rather create a thread to ping the ip and I time this thread to 2 seconds. If by then there was no answer from the ping (thread return value) I'll just say that there seems to be no connection. That should be possible?
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2366
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Check connection to IP

Post by evstevemd »

Wanderer82 wrote: Wed Aug 03, 2022 7:45 pm That all looks quite a big efffort for such an easy task.
unfortunately, bad design always have such consequences!
Wanderer82 wrote: Wed Aug 03, 2022 7:45 pm I think I'd rather create a thread to ping the ip and I time this thread to 2 seconds. If by then there was no answer from the ping (thread return value) I'll just say that there seems to be no connection. That should be possible?
I wish it was that easier! You will have to messup with wxExecute and wxProcess. Since you are novice a you call it, It is going to be a tough job!
The best thing and simplest I could comeup with in my mind is that one above!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Wanderer82
Super wx Problem Solver
Super wx Problem Solver
Posts: 268
Joined: Tue Jul 26, 2016 2:00 pm

Re: Check connection to IP

Post by Wanderer82 »

Actually I have just managed to do it. But I don't use "ping" but "wxDirExists" in a thread to check whether the user directory (folder) on the server exists. I could also do it by using "ping". The thing is that I check it inside a thread. After executing the thread my main program sleeps for 1.5 seconds. I have a variable which is set to 1 (meaning "no connection") before starting the thread. If during these 1.5 seconds wxDirExists becomes "true", the variable is changed to 0 inside the thread and my main program continues. If after these 1.5 seconds I don't have an answer from wxDirExists (which could take up to 20 seconds while it is looking for the folder when there is no connection), the variable remains 1, which means that there is no connection and I quit the printer installation. I don't care about the thread going on to try to find the folder as either after ~20 seconds or after I close my entire application the thread will be terminated anyway. Although: If there was a better solution to correctly terminate the thread or the function after 1.5 seconds, that would surely be better. I wonder why there is no way to cancel checking for a folder after some time has passed. But maybe there is another way...

Code: Select all

#include <thread>

int IP_check;

void threadIP_Check()
{
    if(wxDirExists("\\\\UNT-Server01\\userfiles\" + _User.Username))
        IP_check = 0;
}

void Printer_Install()
{
    IP_check = 1;

    thread funcTest1(threadIP_Check);
    funcTest1.detach();
    Sleep(1500);

    if(IP_check == 1)
    {
        wxBusyInfo wait2("There is no connection to the server. Printer installation is aborted...");
        Sleep(4000);
        return;
    }
    
    else
    {
    	//Install printer
    }
Wanderer82
Super wx Problem Solver
Super wx Problem Solver
Posts: 268
Joined: Tue Jul 26, 2016 2:00 pm

Re: Check connection to IP

Post by Wanderer82 »

Maybe someone could say something about my solution concerning the thread not being terminated actively. Is this a problem and if it is, how could I do it better? I have read that one shouldn't "kill" a thread. But maybe this is only true for threads that are critical. The mere check of the existence of a directory shouldn't be a big problem, yes?
Post Reply