473,566 Members | 3,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ping+Port Routine?

Hi! I'm trying to write a small application for an online gaming site
(flight sims), where people can add their local server to a list.

Basically, I just need to be able to loop/ping each respective server
- via a specific port (2934 or 2935) to see whether the server is
still "live", so the hosters don't have to manually update their
status on the site.

Everything I've seen seems overly complex. Efforts so far have
returned info via a traditional PING, but I can't seem to find an easy
way of getting the required *port* info. The script would have to run
from a regular hosted site, so I wouldn't have much access to the
server's "innards".

Any ideas?

TIA - Adam.
Sep 21 '05 #1
13 3223
Adam wrote:
Hi! I'm trying to write a small application for an online gaming site
(flight sims), where people can add their local server to a list.

Basically, I just need to be able to loop/ping each respective server
- via a specific port (2934 or 2935) to see whether the server is
still "live", so the hosters don't have to manually update their
status on the site.

Everything I've seen seems overly complex. Efforts so far have
returned info via a traditional PING, but I can't seem to find an easy
way of getting the required *port* info. The script would have to run
from a regular hosted site, so I wouldn't have much access to the
server's "innards".

Any ideas?

TIA - Adam.


Ping uses an internally defined port for responses - not just any port.
You can't ping a specific port - it wouldn't know how to respond.

Probably the easiest way would be to use fsockopen to open a socket to
the appropriate system/port.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Sep 21 '05 #2
On Wed, 21 Sep 2005 07:05:41 -0500, Jerry Stuckle wrote:
Adam wrote:
Hi! I'm trying to write a small application for an online gaming site
(flight sims), where people can add their local server to a list.

Basically, I just need to be able to loop/ping each respective server
- via a specific port (2934 or 2935) to see whether the server is
still "live", so the hosters don't have to manually update their
status on the site.

Everything I've seen seems overly complex. Efforts so far have
returned info via a traditional PING, but I can't seem to find an easy
way of getting the required *port* info. The script would have to run
from a regular hosted site, so I wouldn't have much access to the
server's "innards".

Any ideas?

TIA - Adam.


Ping uses an internally defined port for responses - not just any port.
You can't ping a specific port - it wouldn't know how to respond.

Probably the easiest way would be to use fsockopen to open a socket to
the appropriate system/port.


Thanks for that. I've managed to send an initial "handshake" packet
out to the game server using:

$handle = fsockopen("udp://$host", $port, $errno, $errstr, $timeout);

It triggers the game server to respond with a 67 byte long reply
(which I can see using a packet sniffer). So far so good.

However ... try as I might, I just can't read this data. I've tried
all sorts of combinations of "fread", "unpack" etc. eg:

$contents = fread($handle, 67);

The above seems to return nothing. How do I go about reading this
packet of data? Is the PHP code executing before the packet has had a
chance to arrive?

Thanks,

Adam.
Sep 22 '05 #3
Adam wrote:
On Wed, 21 Sep 2005 07:05:41 -0500, Jerry Stuckle wrote:

Thanks for that. I've managed to send an initial "handshake" packet
out to the game server using:

$handle = fsockopen("udp://$host", $port, $errno, $errstr, $timeout);

It triggers the game server to respond with a 67 byte long reply
(which I can see using a packet sniffer). So far so good.

However ... try as I might, I just can't read this data. I've tried
all sorts of combinations of "fread", "unpack" etc. eg:

$contents = fread($handle, 67);

The above seems to return nothing. How do I go about reading this
packet of data? Is the PHP code executing before the packet has had a
chance to arrive?

Thanks,

Adam.


From the PHP manual:

Warning:
UDP sockets will sometimes appear to have opened without an error, even
if the remote host is unreachable. The error will only become apparent
when you read or write data to/from the socket. The reason for this is
because UDP is a "connectionless " protocol, which means that the
operating system does not try to establish a link for the socket until
it actually needs to send or receive data.

So - just because you get a handle back doesn't mean you've successfully
opened the socket. You may not actually be opening it. You many need
to use a packet sniffer to see what's actually going across your link.

Otherwise, if just opening the socket gets you the response, I would
expect you to get the response. Are you sure you don't need to send
something first, i.e. a newline char?

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Sep 22 '05 #4
On Thu, 22 Sep 2005 11:23:15 -0500, Jerry Stuckle wrote:
Adam wrote:
On Wed, 21 Sep 2005 07:05:41 -0500, Jerry Stuckle wrote:

Thanks for that. I've managed to send an initial "handshake" packet
out to the game server using:

$handle = fsockopen("udp://$host", $port, $errno, $errstr, $timeout);

It triggers the game server to respond with a 67 byte long reply
(which I can see using a packet sniffer). So far so good.

However ... try as I might, I just can't read this data. I've tried
all sorts of combinations of "fread", "unpack" etc. eg:

$contents = fread($handle, 67);

The above seems to return nothing. How do I go about reading this
packet of data? Is the PHP code executing before the packet has had a
chance to arrive?

Thanks,

Adam.


From the PHP manual:

Warning:
UDP sockets will sometimes appear to have opened without an error, even
if the remote host is unreachable. The error will only become apparent
when you read or write data to/from the socket. The reason for this is
because UDP is a "connectionless " protocol, which means that the
operating system does not try to establish a link for the socket until
it actually needs to send or receive data.

So - just because you get a handle back doesn't mean you've successfully
opened the socket. You may not actually be opening it. You many need
to use a packet sniffer to see what's actually going across your link.

Otherwise, if just opening the socket gets you the response, I would
expect you to get the response. Are you sure you don't need to send
something first, i.e. a newline char?


Jerry - thanks for persevering with me on this one <g>. Yep - I'd read
that warning in the manual.

There's no problem with *writing* to the [opened] socket. Using a
packet sniffer, I can see the data go out and - what's more - I can
see a response packet appear on the client. This is what's happening:

1) Open socket (in client).
2) Write to socket (gets sent successfully to game server).
3) Client gets response back from server (socket still open).
4) Futile attempts to read the 67 bytes long incoming packet (!!).
5) Close socket (in client).

For step 4, I've tried fread, fgets .. all sorts - but it occurs to me
that this may be a PHP/OS related thing, as I've seen (in my Googling)
reference to a *read* socket bug in PHP in earlier builds for Win32.

Are you suggesting I send something [again] before steps 3,4? Is it a
*timing* problem? Either the PHP script isn't waiting long enough for
the incoming packet or the packet has beenand gone before the script
has had a chance to read it?

All examples I've tried using (eg. HTTP/port 80) seem to work fine - a
request gets sent and the response is processed properly and
displayed.

My setup is Apache/2.0.52 (Win32) PHP/4.3.9. I'll try running the
client script from a Linux machine.

Adam.
Sep 23 '05 #5
Adam wrote:
On Thu, 22 Sep 2005 11:23:15 -0500, Jerry Stuckle wrote:

Jerry - thanks for persevering with me on this one <g>. Yep - I'd read
that warning in the manual.

There's no problem with *writing* to the [opened] socket. Using a
packet sniffer, I can see the data go out and - what's more - I can
see a response packet appear on the client. This is what's happening:

1) Open socket (in client).
2) Write to socket (gets sent successfully to game server).
3) Client gets response back from server (socket still open).
4) Futile attempts to read the 67 bytes long incoming packet (!!).
5) Close socket (in client).

For step 4, I've tried fread, fgets .. all sorts - but it occurs to me
that this may be a PHP/OS related thing, as I've seen (in my Googling)
reference to a *read* socket bug in PHP in earlier builds for Win32.

Are you suggesting I send something [again] before steps 3,4? Is it a
*timing* problem? Either the PHP script isn't waiting long enough for
the incoming packet or the packet has beenand gone before the script
has had a chance to read it?

All examples I've tried using (eg. HTTP/port 80) seem to work fine - a
request gets sent and the response is processed properly and
displayed.

My setup is Apache/2.0.52 (Win32) PHP/4.3.9. I'll try running the
client script from a Linux machine.

Adam.


Hi, Adam,

OK, I didn't realize you were sending to the remote machine first. OK,
so we know the socket itself is open, you can write to it, and you get a
response back.

Next thing - fread stops as soon as a packet is available. This may or
may not be the entire message. An extreme example - say the remote
system is sending you 100K worth of data. This won't all come in one
packet - you probably will have at least dozens of them. fread() will
read one packet, and you'll have to keep to keep receiving until you get
all the data, and assemble the packets.

How does this apply in your case? Well, is it possible you're getting a
short (or empty) packet before the data? It's perfectly legal for the
remote to do so. If so, you'll have to loop on your fread() call until
you get all 67 bytes of data.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Sep 23 '05 #6
On Fri, 23 Sep 2005 10:07:59 -0500, Jerry Stuckle wrote:
Adam wrote:
On Thu, 22 Sep 2005 11:23:15 -0500, Jerry Stuckle wrote:

Jerry - thanks for persevering with me on this one <g>. Yep - I'd read
that warning in the manual.

There's no problem with *writing* to the [opened] socket. Using a
packet sniffer, I can see the data go out and - what's more - I can
see a response packet appear on the client. This is what's happening:

1) Open socket (in client).
2) Write to socket (gets sent successfully to game server).
3) Client gets response back from server (socket still open).
4) Futile attempts to read the 67 bytes long incoming packet (!!).
5) Close socket (in client).

For step 4, I've tried fread, fgets .. all sorts - but it occurs to me
that this may be a PHP/OS related thing, as I've seen (in my Googling)
reference to a *read* socket bug in PHP in earlier builds for Win32.

Are you suggesting I send something [again] before steps 3,4? Is it a
*timing* problem? Either the PHP script isn't waiting long enough for
the incoming packet or the packet has beenand gone before the script
has had a chance to read it?

All examples I've tried using (eg. HTTP/port 80) seem to work fine - a
request gets sent and the response is processed properly and
displayed.

My setup is Apache/2.0.52 (Win32) PHP/4.3.9. I'll try running the
client script from a Linux machine.

Adam.


Hi, Adam,

OK, I didn't realize you were sending to the remote machine first. OK,
so we know the socket itself is open, you can write to it, and you get a
response back.

Next thing - fread stops as soon as a packet is available. This may or
may not be the entire message. An extreme example - say the remote
system is sending you 100K worth of data. This won't all come in one
packet - you probably will have at least dozens of them. fread() will
read one packet, and you'll have to keep to keep receiving until you get
all the data, and assemble the packets.

How does this apply in your case? Well, is it possible you're getting a
short (or empty) packet before the data? It's perfectly legal for the
remote to do so. If so, you'll have to loop on your fread() call until
you get all 67 bytes of data.


Well ... the story continues <ggg>. Basically though, it appears that
whichever way I try to read the data, it hangs. This is using fread,
fgets, socket reads ... you name it. The *impression* I get is that
the packet has already been and gone - and all attempts to read the
socket fail - because the [remote] server is no longer sending.

I know (via the portsniffer) that the incoming packet is less than 100
bytes long. It's not a file (I don't think) so it won't have an eof
marker.

I've just installed PHP5 with socket support on a Linux machine to see
whether any of the socket_* commands or new PHP5 functions will work.

Watch this space <g> ...

Adam.
Sep 26 '05 #7
Still no luck :-((

I have Ethereal running on both client and server. Both versions
confirm that:

1) A request goes out to the server on port 2934. This is around 70
bytes: A header plus around 30 bytes of data.
2) The server responds with a welcome of around 120 bytes.
3) This packet duly arrives at the client.

OK - now to the code. I've tried every combination of socket connect &
write, streams .. you name it ...but here are my two main variations:
// Set up the connection =============== ====
$remote_host = 'udp://192.168.1.126';
$remote_port = 2934;
$timeout = 1;

// This is the outgoing msg (in hex) ==========
$hello =
"\x20\x00\x09\x 42\x46\x4f\x31\ x00\x00\x00\x00 \x01\x83\x05\x0 7\x0d\x12";

// This part embeds the client's IP address (in hex, backwards!)
// This is what the connecting flight sim appears to send
$hello .="\x03\x01\xa8 \xc0";

// The rest of the data packet:- (sim version etc).
$hello .= "\x03\x84\x0f\x 87\x12\x02\x00\ x00";

// The returned data (I should be so lucky!) =====
$ret = '';

// Open the socket =============== =
$socket = fsockopen($remo te_host, $remote_port, $errno, $errstr,
$timeout);
if (!$socket) {
echo "Connection Failed - Please check manually."; }
else {
// Send the packet ===
fputs($socket, "$hello");
// fputs($socket, "$hello\r\n "); // Try with a return? ===

echo "Sent to <b>$remote_host :$remote_port</b><br>";

// So far so good ... the packet gets received
// by the server (confirmed by sniffer) ...
// and a reply gets sent to the client IP+port ..

//... BUT ... neither of these appear to work =======
//$ret = fgets($socket, 64);
$ret = fread($socket, 2048);
// Tried all sorts values from 1 to 2048 =====

echo "[" . $ret . "]";
fclose($socket) ;
};
echo "<hr>Closed ";

That's it! H-E-L-P !!! I wonder whether you/anyonecan shed any light
on this!

Adam.
Sep 26 '05 #8
Adam wrote:
Still no luck :-((

I have Ethereal running on both client and server. Both versions
confirm that:

1) A request goes out to the server on port 2934. This is around 70
bytes: A header plus around 30 bytes of data.
2) The server responds with a welcome of around 120 bytes.
3) This packet duly arrives at the client.

OK - now to the code. I've tried every combination of socket connect &
write, streams .. you name it ...but here are my two main variations:
// Set up the connection =============== ====
$remote_host = 'udp://192.168.1.126';
$remote_port = 2934;
$timeout = 1;

// This is the outgoing msg (in hex) ==========
$hello =
"\x20\x00\x09\x 42\x46\x4f\x31\ x00\x00\x00\x00 \x01\x83\x05\x0 7\x0d\x12";

// This part embeds the client's IP address (in hex, backwards!)
// This is what the connecting flight sim appears to send
$hello .="\x03\x01\xa8 \xc0";

// The rest of the data packet:- (sim version etc).
$hello .= "\x03\x84\x0f\x 87\x12\x02\x00\ x00";

// The returned data (I should be so lucky!) =====
$ret = '';

// Open the socket =============== =
$socket = fsockopen($remo te_host, $remote_port, $errno, $errstr,
$timeout);
if (!$socket) {
echo "Connection Failed - Please check manually."; }
else {
// Send the packet ===
fputs($socket, "$hello");
// fputs($socket, "$hello\r\n "); // Try with a return? ===

echo "Sent to <b>$remote_host :$remote_port</b><br>";

// So far so good ... the packet gets received
// by the server (confirmed by sniffer) ...
// and a reply gets sent to the client IP+port ..

//... BUT ... neither of these appear to work =======
//$ret = fgets($socket, 64);
$ret = fread($socket, 2048);
// Tried all sorts values from 1 to 2048 =====

echo "[" . $ret . "]";
fclose($socket) ;
};
echo "<hr>Closed ";

That's it! H-E-L-P !!! I wonder whether you/anyonecan shed any light
on this!

Adam.

Adam,

Let's back up a little. I'm wondering if we're looking at the wrong
problem.

A couple of thoughts here. First of all, where are you running the
sniffer? Is it on this machine, or somewhere upstream?

Also, are you using a firewall or a router anyplace? If so, are they
set up properly to pass the incoming data onto your program?

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Sep 26 '05 #9
On Mon, 26 Sep 2005 06:59:08 -0500, Jerry Stuckle wrote:
Adam wrote:
Still no luck :-((

I have Ethereal running on both client and server. Both versions
confirm that:

1) A request goes out to the server on port 2934. This is around 70
bytes: A header plus around 30 bytes of data.
2) The server responds with a welcome of around 120 bytes.
3) This packet duly arrives at the client.

OK - now to the code. I've tried every combination of socket connect &
write, streams .. you name it ...but here are my two main variations:
// Set up the connection =============== ====
$remote_host = 'udp://192.168.1.126';
$remote_port = 2934;
$timeout = 1;

// This is the outgoing msg (in hex) ==========
$hello =
"\x20\x00\x09\x 42\x46\x4f\x31\ x00\x00\x00\x00 \x01\x83\x05\x0 7\x0d\x12";

// This part embeds the client's IP address (in hex, backwards!)
// This is what the connecting flight sim appears to send
$hello .="\x03\x01\xa8 \xc0";

// The rest of the data packet:- (sim version etc).
$hello .= "\x03\x84\x0f\x 87\x12\x02\x00\ x00";

// The returned data (I should be so lucky!) =====
$ret = '';

// Open the socket =============== =
$socket = fsockopen($remo te_host, $remote_port, $errno, $errstr,
$timeout);
if (!$socket) {
echo "Connection Failed - Please check manually."; }
else {
// Send the packet ===
fputs($socket, "$hello");
// fputs($socket, "$hello\r\n "); // Try with a return? ===

echo "Sent to <b>$remote_host :$remote_port</b><br>";

// So far so good ... the packet gets received
// by the server (confirmed by sniffer) ...
// and a reply gets sent to the client IP+port ..

//... BUT ... neither of these appear to work =======
//$ret = fgets($socket, 64);
$ret = fread($socket, 2048);
// Tried all sorts values from 1 to 2048 =====

echo "[" . $ret . "]";
fclose($socket) ;
};
echo "<hr>Closed ";

That's it! H-E-L-P !!! I wonder whether you/anyonecan shed any light
on this!

Adam.

Adam,

Let's back up a little. I'm wondering if we're looking at the wrong
problem.

A couple of thoughts here. First of all, where are you running the
sniffer? Is it on this machine, or somewhere upstream?


I've got Ethereal running on both machines:

1) Client: Linux - running the PHP script. No firewall.
2) Server: WinXP - running the sim in multiplayer mode. Firewall in
place (Norton PFW) but this server connects OK to other XP machines
running the sim in client mode.
Also, are you using a firewall or a router anyplace? If so, are they
set up properly to pass the incoming data onto your program?


The router only sits between the network and the ADSL.

Anyway - I can *see* the packets leaving/arriving on each machine - so
I think the firewall thing is OK. The packets appear addressed
properly, and contain data.

Having done a fair bit of googling around, it seems PHP's sockets
implementation is a bit quirky. All sorts of oddities come into the
equation - the "magic_quot es" setting, stream_set_bloc king and so on.

I could paste the dumps of each packet if that helps any.

Over to you, Mastah! <g>.

Adam.
Sep 26 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
15633
by: Sven Dzepina | last post by:
Hello people, I want to check with a php script if a port / serverservice is down. It would be great if I can check the ping time, too. But how I can realize that? Gretting from Germany.
8
4357
by: DCK | last post by:
Hello :) Into group-archive i found most e-mails, which touches PINGing. In my work i've used TELNET for testing if host is operational. Sometimes, for unknown reasons, workstation doesn't respond for PINGing. But in WinNT network, all hosts has a nbsession listening on port 139. I always use this script instead PING command (hope, will be...
1
29959
by: gallaczmit | last post by:
Will this code give me a true view of a computer's status? All I am looking for is to see if the computer is reachable or not. My end goal is to get a list of IP addresses from a MS SQL Server, see if the IP is reachable(if yes I will assume the pc is operational), update the database with the result and generate a web page displaying the...
0
7754
by: Ed | last post by:
I've attached some VB.NET code I've hacked together (some taken from MS examples & newsgroup postings) that will perform a ping or IcmpSendEcho using the icmp.dll (see this for more info: http://support.microsoft.com/default.aspx?scid=kb;en-us;170591 ). The problem I have is in order to perform a discovery/ping of an entire subnet...
8
18373
by: Nico Grubert | last post by:
Hi there, I could not find any "ping" Class or Handler in python (2.3.5) to ping a machine. I just need to "ping" a machine to see if its answering. What's the best way to do it? Kind regards, Nico
21
30491
by: Neel | last post by:
I am trying to "ping" a remote host in my C++/Redhat Linux code to check whether that host is connected or not. if (0 == system("ping -w 2 192.168.0.2)) But, in both cases (connected/disconnected), system call returns 0. Can someone please show what I am doing wrong? How to check the actual result of the ping status using C++ on Redhat...
6
3450
by: Dave Marden | last post by:
I currently use this routine in vbscript to ping computers and get the status of ping to determine whether to try to backup a machine, I am trying to change it to work with vb2003.net I am wondering if anyone has a ping function they could share with me. I have done some searching on this but cannot find anything specifically for vb2003. ...
1
3685
by: Andy Bates | last post by:
Hi - Can't see another newsgroup to post this in; so thought I'd post here. I have a C# application that relies on multicast UDP to detect how many PCs the application is executing on concurrently. This works okay providing the port is open but fails miserably if the port is blocked by a firewall. All applications provide a server...
1
72532
by: ScottZ | last post by:
With python 2.6 and wxpython I'm trying to create a system tray icon application based around an example found here: http://codeboje.de/MailSneaker-Part-3-SystemTrayTaskBar-Icons-with-Python-and-wxPython/ The application will simply change the systray icon based on if an ip address is online or not. The ping portion looks like this: if...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.