PHP and HTML: socket_connect() [function.socket-co

2019-03-06 04:25发布

问题:

I am working on a php file that connects to my game server and executes a command. The users enter their username on a HTML form that sends them and the username to a php file that connects to the server. The port is forwarded and the server is ready to receive the info, but I keep getting this error:

Warning: socket_connect() [function.socket-connect]: unable to connect [110]: Connection timed out in /home/moocraft/public_html/test/vote.php on line 8 error: could not connect to host

Here is the HTML file:

<html>
<body>

<form action="vote.php" method="post">
Minecraft Username: <input type="text" name="fname" />
<input type="submit" />
</form>

</body>
</html> 

Here is the PHP file:

<?php
 $HOST = "207.210.254.141"; //the ip of the bukkit server
 $password = "examplepassword1";
 $player = $_POST["fname"];
 //Can't touch this:
 $sock = socket_create(AF_INET, SOCK_STREAM, 0)
 or die("error: could not create socket\n");
 $succ = socket_connect($sock, $HOST, 4445) 
 or die("error: could not connect to host\n");
 //Authentification
 socket_write($sock, $command = md5($password)."<Password>", strlen($command) + 1)
 or die("error: failed to write to socket\n");
 //Begin custom code here.
 socket_write($sock, $command = "/Command/ExecuteConsoleCommand:give $player 264 5;", strlen($command) + 1) //Writing text/command we want to send to the server
 or die("error: failed to write to socket\n");
 socket_write($sock, $command = "$player voted for MOOcraft and earned 5 diamonds. Vote at moocraft.org;", strlen($command) + 1)
 or die("error: failed to write to socket\n");
 ?>

I can't figure out why I keep getting this. Any help is greatly appreciated.

回答1:

Try something simple like:

$fp = fsockopen("207.210.254.141", 4445, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)
\n"; } else { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: 207.210.254.141\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); }

And see what do you get, it might be due to the reason that your server is taking too long to respond