SA-MP Forums

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

Thread Tools Display Modes
Old 13/04/2012, 10:41 AM   #1
Beta Tester
BlueG's Avatar
Join Date: Sep 2007
Location: Los Angeles, CA
Posts: 58
Reputation: 308
Default Socket plugin (v0.2b released)

Sockets v0.2b

01/08/14: (v0.2b)
- finished SSL related code
- general bugfix
- added socket_sendto()
- fixed socket_send_array()
- added ssl_get_peer_certificate() 
24/06/12: (v0.1b)
- TCP server crash fixed (thanks to Wicko)
- general bugfixes
This plugin allows you to create multiple TCP or UDP sockets. Each socket receives data in a separate thread, which means that the server will not freeze. Once you destroy a socket, the thread will shutdown. If there is further interest I'm going to add support for SSL sockets.

The plugin has many uses, here are some ideas:
- IRC echo bot
- a new RCON tool including chat (based on a TCP server socket, kinda like GUIRCON)
- retrieve SA-MP server data through UDP
- communicate with a PHP script to log/receive data

PAWN Scripting (
pawn Code:
native Socket:socket_create(pType:TCP); // udp & tcp
native socket_bind(Socket:id, ip[]); // udp & tcp
native socket_connect(Socket:id, host[], port); // udp & tcp
native socket_listen(Socket:id, port); // udp & tcp
native socket_stop_listen(Socket:id); // udp & tcp
native socket_destroy(Socket:id); // udp & tcp
native socket_send(Socket:id, data[], len); // udp & tcp
native socket_sendto(Socket:id, const ip[], port, data[], len); // udp only
native socket_send_array(Socket:id, data[], size=sizeof(data)); // udp & tcp
native is_socket_valid(Socket:id); // udp & tcp
native socket_set_max_connections(Socket:id, max_remote_clients); // tcp only
native socket_close_remote_client(Socket:id, remote_clientid); // tcp only
native socket_sendto_remote_client(Socket:id, remote_clientid, data[]); // tcp only
native socket_remote_client_connected(Socket:id, remote_clientid); // tcp only
native get_remote_client_ip(Socket:id, remote_clientid, ip[]); // tcp only

// ssl stuff (requires OpenSSL)

native ssl_init(); // initialize the ssl library
native ssl_create_context(Socket:id, method);
native ssl_connect(Socket:id); // tcp (client only)
native ssl_load_cert_into_context(Socket:id, const certificate[], const private_key[]); // certificate & private_key might be the same .pem file
native ssl_shutdown(Socket:id) = socket_destroy;
native ssl_get_peer_certificate(Socket:id, method, subject[], issuer[], remote_clientid = 0xFFFF);
native ssl_set_accept_timeout(Socket:id, interval); // interval in miliseconds
native ssl_set_mode(Socket:id, mode); // see above SSL_modes (enum)

pawn Code:
// client & server (udp)
forward onUDPReceiveData(Socket:id, data[], data_len, remote_client_ip[], remote_client_port);
// client only (tcp)
forward onSocketAnswer(Socket:id, data[], data_len); // called when socket_connect() has been used and the server sends data
forward onSocketClose(Socket:id);
// server only (tcp)
forward onSocketReceiveData(Socket:id, remote_clientid, data[], data_len); // called when a remote client sends data
forward onSocketRemoteConnect(Socket:id, remote_client[], remote_clientid); // called when a remote client connects to our socket server
forward onSocketRemoteDisconnect(Socket:id, remote_clientid); // called when a remote client disconnects from our socket server

pawn Code:
// connect to an IRC server
new Socket:sock = socket_create(TCP);
if(is_socket_valid(sock)) {
    socket_connect(sock, "", 6667);
    socket_send(sock, "NICK TestBot\r\n"); // set the nickname
    socket_send(sock, "USER TestBot - - :TestBot\r\n"); // set the username & realname
    socket_send(sock, "MODE TestBot +B\r\n"); // tell the server it's a bot
    socket_send(sock, "JOIN #test_channel\r\n"); // join a specific channel

// this callback will now receive all incoming data
public onSocketAnswer(Socket:id, data[])

Downloads (v0.2a has been skipped)

Note: v0.2b includes SSL related code

Last edited by BlueG; 01/08/2014 at 11:38 AM.
BlueG is offline   Reply With Quote
Old 13/04/2012, 10:44 AM   #2
Shabi RoxX
Shabi RoxX's Avatar
Join Date: Jan 2012
Location: ____Pakistan______ Playing Since: 5-April-2008 Facebook:Shabi RoxX
Posts: 534
Reputation: 134
Default Re: Socket plugin

wow nice work keep it up......
Shabi RoxX is offline   Reply With Quote
Old 13/04/2012, 10:45 AM   #3
Mauzen's Avatar
Join Date: Jun 2007
Location: Western Germany
Posts: 4,877
Reputation: 1597
Default Re: Socket plugin

Great, this was one of the missing samp features.
So lets wait for the first cluster servers
Mauzen is offline   Reply With Quote
Old 13/04/2012, 10:51 AM   #4
Little Clucker
C0dy09's Avatar
Join Date: Oct 2011
Location: Germany
Posts: 36
Reputation: 0
Default AW: Socket plugin

wow very nice!
C0dy09 is offline   Reply With Quote
Old 13/04/2012, 11:00 AM   #5
Join Date: Oct 2009
Posts: 2,151
Reputation: 215
Default Re: Socket plugin

I waited so long for i can integrate server chat to my forum
Can someone provide example for sending and listening socket from server and web (PHP)
DRIFT_HUNTER is offline   Reply With Quote
Old 13/04/2012, 11:31 AM   #6
Join Date: May 2010
Location: Black Asylum
Posts: 2,938
Reputation: 538
Default Re: Socket plugin (TCP/UDP) v0.1a

Lovely, finally a great working socket plugin!

Thank you BlueG & Great Job.
Kar is offline   Reply With Quote
Old 13/04/2012, 11:46 AM   #7
AirKite's Avatar
Join Date: Feb 2009
Posts: 733
Reputation: 21
Default Re: Socket plugin (TCP/UDP) v0.1a

Good job!!!
AirKite is offline   Reply With Quote
Old 13/04/2012, 11:50 AM   #8
TheArcher's Avatar
Join Date: Dec 2009
Location: Home
Posts: 2,334
Reputation: 251
Default Re: Socket plugin (TCP/UDP) v0.1a

THANK YOU SO MUCH! This is an AMAZING release.

Edit: Is this faster as SampQuery by Wastie?
TheArcher is offline   Reply With Quote
Old 13/04/2012, 12:08 PM   #9
QuaTTrO's Avatar
Join Date: Aug 2009
Posts: 516
Reputation: 75
Default Re: Socket plugin (TCP/UDP) v0.1a

This is amazing. Thank you, i looking for something like this
QuaTTrO is offline   Reply With Quote
Old 13/04/2012, 12:14 PM   #10
zSuYaNw's Avatar
Join Date: Apr 2010
Location: Santa Catarina
Posts: 6,840
Reputation: 672
Default Re: Socket plugin (TCP/UDP) v0.1a

Amazing, nice work!
zSuYaNw is offline   Reply With Quote

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

All times are GMT. The time now is 02:40 AM.

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