SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Reply
 
Thread Tools Display Modes
Old 15/04/2012, 03:33 PM   #21
Crimson
Little Clucker
 
Join Date: May 2011
Posts: 32
Reputation: 0
Default Re: AW: Socket plugin (TCP/UDP) v0.1a

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...
Crimson is offline   Reply With Quote
Old 15/04/2012, 03:56 PM   #22
AlexLS95
Little Clucker
 
Join Date: Sep 2009
Posts: 31
Reputation: 9
Default AW: Socket plugin (TCP/UDP) v0.1a

ahhh thank you. I didn't know that. I ever tried ports < 1000.
AlexLS95 is offline   Reply With Quote
Old 15/04/2012, 04:45 PM   #23
dEzIRE
Little Clucker
 
Join Date: Mar 2009
Posts: 8
Reputation: 0
Default Re: Socket plugin (TCP/UDP) v0.1a

never mind... it works perfectly.
thanks!
dEzIRE is offline   Reply With Quote
Old 15/04/2012, 05:40 PM   #24
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,642
Reputation: 2475
Default Re: AW: Socket plugin (TCP/UDP) v0.1a

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.
__________________
Vince is offline   Reply With Quote
Old 15/04/2012, 06:48 PM   #25
TheArcher
High-roller
 
TheArcher's Avatar
 
Join Date: Dec 2009
Location: Home
Posts: 2,458
Reputation: 247
Default Re: AW: Socket plugin (TCP/UDP) v0.1a

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
__________________
TheArcher is offline   Reply With Quote
Old 15/04/2012, 07:06 PM   #26
AndreT
Gangsta
 
AndreT's Avatar
 
Join Date: Jul 2011
Posts: 992
Reputation: 410
Default Re: Socket plugin (TCP/UDP) v0.1a

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.
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.

Last edited by AndreT; 13/05/2012 at 06:55 AM.
AndreT is offline   Reply With Quote
Old 18/04/2012, 02:43 PM   #27
dEzIRE
Little Clucker
 
Join Date: Mar 2009
Posts: 8
Reputation: 0
Default Re: Socket plugin (TCP/UDP) v0.1a

the plugin crashes the server when it receives data on Linux (Centos).
on windows it works perfectly...
dEzIRE is offline   Reply With Quote
Old 18/04/2012, 02:55 PM   #28
Meta
Gangsta
 
Meta's Avatar
 
Join Date: Feb 2010
Location: Germany
Posts: 650
Reputation: 47
Default AW: Socket plugin (TCP/UDP) v0.1a

Nice
I tested it for BlueG and it ran so smooth
Meta is offline   Reply With Quote
Old 23/04/2012, 02:21 PM   #29
pownayge
Little Clucker
 
Join Date: Nov 2009
Posts: 3
Reputation: 0
Default Re: Socket plugin (TCP/UDP) v0.1a

The method
Code:
socket_send(Socket:id, data[])
Isn't working for me on Windows 7 x64 :/, any solutions?
__________________
Wanting to create a RolePlay server. PM if interested.
pownayge is offline   Reply With Quote
Old 24/04/2012, 02:03 PM   #30
T101
Little Clucker
 
T101's Avatar
 
Join Date: Apr 2012
Posts: 29
Reputation: 4
Default Re: Socket plugin (TCP/UDP) v0.1a

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
T101 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Socket Plugin] Retrieving input stream iRage Scripting Help 0 08/01/2014 09:11 AM
Question about socket plugin Kirollos Scripting Help 6 13/07/2012 08:33 PM
Socket Plugin 147147147 Espaņol/Spanish 5 07/03/2012 10:22 PM


All times are GMT. The time now is 06:54 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.