About the previous discussion... ports 0 - 1024, in most cases, require some sort of administrative rights on the system.
And BlueG, thanks a lot for this great plugin. I got the PHP-gameserver communications script that we spoke about earlier to work perfectly.
For anyone interested, here's something small I combined for use right now. The PHP code listens for connections incoming on port 7778.
pawn Code:
#include <a_samp>
#include <socket>
new Socket:Client;
public OnFilterScriptInit()
{
// Create the client socket (feed data to webserver)
Client = socket_create(TCP);
if(is_socket_valid(Client))
{
socket_connect(Client, "127.0.0.1", 7778);
socket_send(Client, "Hi, server!\n");
}
return 1;
}
public OnFilterScriptExit()
{
// Destroy the socket if it is valid!
if(is_socket_valid(Client))
socket_destroy(Client);
}
// Any response from the PHP script (through PHP func socket_write) will fire this callback:
public onSocketAnswer(Socket:id, data[], data_len)
{
printf("onSocketAnswer(%d, %s, %d)" _:id, data, data_len);
}
Note! When using socket_send, make sure to append \n when working according to this example, because the PHP code example below uses PHP_NORMAL_READ with socket_read (this means it will continue parsing, freezing rest of the script, until it finds a newline).
And a little PHP I've combined from various examples on the Internet. I haven't really gotten to finetuning it, nor have I mastered the background of socket operations. But this works fine for me.
PHP Code:
// Time limit and flushing setting
set_time_limit(0);
ob_implicit_flush();
// Create the socket and make it listen for 127.0.0.1 on port 7778.
$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp')) or die("Error in socket_create: " . socket_strerror(socket_last_error($socket)) . "\n");
socket_bind($socket, "127.0.0.1", 7778) or die("Error in socket_bind: " . socket_strerror(socket_last_error($socket)) . "\n");
socket_listen($socket, 5) or die("Error in socket_listen: " . socket_strerror(socket_last_error($socket)) . "\n");
while(true)
{
// Accept an incoming connection!
$spawn = socket_accept($socket);
// Send a welcoming notice
$welcome = "I'm your humble host today, dear gameserver!";
socket_write($spawn, $welcome, strlen($welcome));
while(true)
{
// Try to read the input
$input = socket_read($spawn, 1024, PHP_NORMAL_READ);
if($input === false)
{
// Return to the first loop and look for a new connection to accept.
break;
}
if($input == "Hi, server!")
{
$response = "Hello to you as well, dear Sir!";
socket_write($spawn, $response, strlen($response));
}
}
}
socket_close($socket);
I thought the second loop might be necessary so in case your gameserver disconnects from the PHP application, it will actually keep running the code and trying to establish a connection (socket_accept). Otherwise the PHP code will just reach its end.
I'm much of an experimenter so don't hit me if the code is buggy or something can be improved. I hope it gets the point of its abilities across.
So far I've been using HTTP for sending all sorts of player data to the server.
Updated 15/04: Using PHP_NORMAL_READ in PHP's socket_read and ending plugins socket_send with a \n to correct issues that I missed when writing code for this forum :<
Updated 12/05: The PHP script now uses getprotobyname('tcp'), otherwise some Linux installations might get trouble.