1

I can't find any information on this and I was wondering if this was even possible? I have a Minecraft server that I want to ping to find out if it is on at a specific port. I want to do this in Javascript but from what I can see that you cant really do that or it hasn't been done before. Are there any plugins or 3rd party Javascript vendors that can accomplish this?

For example:

mc.mydomain.net:25565

Javascript pings the server and it changes the text from online to offline, depending on if it can connect.

Server is: Online

Or

Server is: Offline
4

4 回答 4

2

If it happened that the Minecraft server actually speak plains HTTP on that port (which is unlikely), then it could work.

Otherwise, no, it can't be done, at least not with current specifications.

Browsers can only talk HTTP (i.e. to web servers) and WebSockets, and the SSL variants thereof. I don't know whether the upcoming WebRTC protocol would help.

The alternative is to use Flash - AIUI that has a plain TCP socket capability that can be exposed to JS code that might help in these circumstances.

于 2012-06-27T19:46:11.400 回答
2

First of all you can't ping ports, as Ping is using ICMP which doesn't have the concept of ports. Ports belong to the transport layer protocols like TCP and UDP.

So, a solution can be the use of a server side language like PHP to perform the query and then you can make an AJAX request to this page in order to retrieve the result. Here is an example in PHP:

<?php                                                                                                                                       
  error_reporting(E_ERROR);                                                                                                                   
  $fp = fsockopen('mc.mydomain.net', 25565, $errno, $errstr, 1);                                                                                
  if (!$fp) {                                                                                                                                 
    echo 'no';                                                                                                                                
  } else {                                                                                                                                    
    echo 'yes';                                                                                                                               
    fclose($fp);                                                                                                                              
  }                                                                                                                                           
?>

Moreover, to keep your requests fast, you can imagine to cache the result of the above query into a file or a database and refresh its value every couple of minutes (for example by using a cron job) and then serve the cached result to the AJAX request.

于 2012-06-27T20:13:25.103 回答
1

From http://www.gnucitizen.org/static/blog/2006/08/jsportscanner.js , the first hit when you search for "javascript portscan":

var AttackAPI = {
    version: '0.1',
    author: 'Petko Petkov (architect)',
    homepage: 'http://www.gnucitizen.org'};

AttackAPI.PortScanner = {};
AttackAPI.PortScanner.scanPort = function (callback, target, port, timeout) {
    var timeout = (timeout == null)?100:timeout;
    var img = new Image();

    img.onerror = function () {
        if (!img) return;
        img = undefined;
        callback(target, port, 'open');
    };

    img.onload = img.onerror;
    img.src = 'http://' + target + ':' + port;

    setTimeout(function () {
        if (!img) return;
        img = undefined;
        callback(target, port, 'closed');
    }, timeout);
};
AttackAPI.PortScanner.scanTarget = function (callback, target, ports, timeout)
{
    for (index = 0; index < ports.length; index++)
        AttackAPI.PortScanner.scanPort(callback, target, ports[index], timeout);
};
于 2012-06-27T20:19:25.893 回答
0

You can only send HTTP(S) or WS(S) request to the domain you are on with JavaScript. A Ping is much too low-level.

If the minecraft server supports HTTP, you can try to use that.

于 2012-06-27T19:47:38.390 回答