Game Development Community

How to ping an IP Address

by Ryan Santos · in Torque 3D Professional · 11/30/2010 (8:52 am) · 8 replies

Hi,

Is there sample code available to ping an IP Address? The IP Address is the IP of my security camera and I just want to check if it is still available or disconnected then restart my camera control if the security camera IP cannot be ping'ed.

Regards,
Ryan

#1
11/30/2010 (2:35 pm)
Don't know of any readily available sample code but one way to implement it would be to use TCPObject to send an ICMP echo request packet and listen for the echo reply. That should be very easy to implement.
#2
11/30/2010 (2:42 pm)
Well, brainfart. This is bogus. ICMP transports directly on IP so using TCPObject is not an option. Would have to be done directly in C++.
#3
12/01/2010 (12:58 am)
I thought that the NetXXX classes do it because there is a Ping object in there. So those NetXXX classes work mostly on torque servers/games only?
#4
12/01/2010 (1:15 am)

These are for ping/ack packets specific to Torque's networking protocol, i.e. not the ICMP pinging you are looking for.

If you're at the C++ level anyways, there's probably APIs that help you construct and send the relevant ICMP packets over an open connection. I *think* there's some stuff in WinSock, for example.

Cheaper path would probably be to run the platform's ping command (asynchronously of course) and use it's return value.
#5
12/01/2010 (2:03 am)
I already have sample code that uses IcmpSendEcho but I would like to use existing torque code because it would be guaranteed to work in all platforms. And you are right, an easier way out would be to do
system( "ping -n 5 xxxx" );
in windows
or
system( "ping -c 5 xxxx" );
in linux.
This system call to ping would work in both windows and linux. But in windows it produces a command prompt which does not look good for a full screen game. I have not tested running this system call in a GUI linux app though. So I guess my remaining solution is to code it in a windows OS specific way by using IcmpSendEcho from ICMP.DLL
#6
12/01/2010 (2:32 am)
Thanks Rene for saving me the effort of using the NetXXXX classes. Hehehe
#7
12/03/2010 (5:07 pm)
I suggest this because a lot of people already have web servers set up performing a variety of tasks for their Torque game, so it might be easy to implement. It also makes the call platform-independent as a side-effect of using an external server perform the routine.

One might use the HTTPObject to call a PHP script on another server and have that script ping for you and return the result to Torque. This is fairly simple with the HTTP call in Torque.

Pinging in PHP has lots of examples available to look at if you need to learn to write the script:

www.codediesel.com/php/ping-a-server-using-php/

www.phptoys.com/e107_plugins/content/content.php?content.41

www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=8&txtCodeId=1786


#8
12/06/2010 (12:57 am)
Thanks for the suggestion but if I were to go for the php approach, I would have it implement a full connect command rather than a simple ping :) However in a casino environment where you are cut off from the outside world and most of the communications are device communications, I would prefer a stand-alone approach. But thanks for the suggestion, most of the time people deal with web servers as you said.