[Plugin] Socket plugin (v0.2b released)
#21

Quote:
Originally Posted by AlexLS95
View Post
Great Plugin! But I've got a problem. With Windows 7 x64 it works really well but on our Linux Ubuntu (v10.04.4) Server everytime I start the server I got this error:

"socket_listen(): Socket has failed to bind on port 776."

*code*

Sorry but I nerver worked with sockets so I don't know how to use them. Can someone help me?
Have you tried a different port? 0 to 1024 are reserved ports if I remember correctly...
Reply
#22

ahhh thank you. I didn't know that. I ever tried ports < 1000.
Reply
#23

never mind... it works perfectly.
thanks!
Reply
#24

Quote:
Originally Posted by TheArcher
View Post
In 2010 StrickenKid made a socket plugin but it hasn't many functions + this uses TCP & UDP + that plugin was out-dated and unstable (beta version)
True. Hopefully this one performs better. Now I will be able to send mail directly from the server without first having to redirect it to a PHP mail() script.
Reply
#25

Quote:
Originally Posted by Vince
View Post
True. Hopefully this one performs better. Now I will be able to send mail directly from the server without first having to redirect it to a PHP mail() script.
I didn't have such an idea XD. Thanks
Reply
#26

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_INETSOCK_STREAMgetprotobyname('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($socket5) 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$welcomestrlen($welcome));
    while(
true)
    {
        
// Try to read the input
        
$input socket_read($spawn1024PHP_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$responsestrlen($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.
Reply
#27

the plugin crashes the server when it receives data on Linux (Centos).
on windows it works perfectly...
Reply
#28

Nice
I tested it for BlueG and it ran so smooth
Reply
#29

The method
Code:
socket_send(Socket:id, data[])
Isn't working for me on Windows 7 x64 :/, any solutions?
Reply
#30

Great job, thanks alot for it.

Quote:
Originally Posted by BlueG
View Post
If there is further interest I'm going to add support for SSL sockets.
Please add it asap, cause it'd be pretty much needed for example secure IRC connections.
Thanks in advance.

Regards,
T101
Reply
#31

How can I use it for an e-mail sending system?
Reply
#32

Quote:
Originally Posted by Francis.
Посмотреть сообщение
How can I use it for an e-mail sending system?
Yeah
Reply
#33

I wouldn't recommend using it for mailing. As Slice once pointed out, there's a huge list of rules that a mail sending script has to follow. PHP's mail() is very advanced when it comes to this and it would be practically very difficult for someone to recreate its functionality in PAWN.
Reply
#34

Thanks a lot man, just fix the problem on "Linux" servers.
Reply
#35

Quote:
Originally Posted by AndreT
Посмотреть сообщение
I wouldn't recommend using it for mailing. As Slice once pointed out, there's a huge list of rules that a mail sending script has to follow. PHP's mail() is very advanced when it comes to this and it would be practically very difficult for someone to recreate its functionality in PAWN.
I did it with the other socket plugin before. Even though it was bugged, it did work. I am familiar with the SMTP.
Reply
#36

Would I be able to use this to make a live chat feed of my server chat?
Reply
#37

itґs very nice but i donґt now how to use that for anything, can someone give me some (extreme) examples what a plugin like this is good for?
Reply
#38

Check the first post, BlueG has posted an example IRC client script using this plugin. You can pretty much rely on your own imagination when it comes to this and you know that TCP is one of the protocols that the whole Internet relies on.

BlueG has already mentioned transferring data between your gameserver and webserver. While this has been possible before using SA-MP's HTTP() function since 0.3b(?), the sockets communication makes it much easier. This was not possible earlier with StrickenKid's plugin because it is bugged.

Also, what Luis said is possible as well. So are things like shared chat between servers and so on. One could make a simple C++/VB application that allows people to chat with people in the server... the list of possibilities is neverending, use your imagination.
Reply
#39

oh thats very cool! but very extreme examples? i read s.th from mauzen about server clustering, how to do this in theoretical?

/ot: i love my english but still learning
Reply
#40

Would you be able to give me a small example on what I could do to make a live chat, would I be able to make it show on my User Control Panel?
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)