Raised This Month: $32 Target: $400
 8% 

[Socket] Detect when remote host is offline


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 10-31-2019 , 07:56   [Socket] Detect when remote host is offline
Reply With Quote #1

Hi folks, how i can detect when remote server is offline?

PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <sockets>

#define CONNECTION_TIMEOUT 10

new g_Host;
new 
g_Pass;

new 
g_Socket;
new 
g_Forward;
new 
g_Password[32];
new 
g_RconChallenge[32];
new 
g_ConnectionTime;

public 
plugin_init()
{
    
register_plugin("Pug Mod (HLTV)","0.0.1","SmileY");
    
    
g_Host create_cvar("amx_rcon_host","192.168.237.1:27020",FCVAR_NONE,"Remote address");    
    
g_Pass create_cvar("amx_rcon_pass","2133",FCVAR_NONE,"Remote rcon password");
    
    
register_concmd("amx_rcon","RconCommand",ADMIN_RCON);
}

public 
plugin_cfg()
{
    
SocketOpen();
}

public 
plugin_end()
{
    
SocketClose();
}

public 
RconCommand(id,Level)
{
    if(
access(id,Level))
    {
        new 
Command[128];
        
read_args(Command,charsmax(Command));
        
remove_quotes(Command);
        
        
SocketSendCommand(Command);
    }
    
    return 
PLUGIN_HANDLED;
}

SocketOpen()
{
    if(
g_Socket == 0)
    {
        new 
Host[MAX_IP_WITH_PORT_LENGTH];
        
get_pcvar_string(g_Host,Host,charsmax(Host));
        
        
get_pcvar_string(g_Pass,g_Password,charsmax(g_Password));
        
        new 
IP[MAX_IP_LENGTH],Port[6];
        
strtok(Host,IP,charsmax(IP),Port,charsmax(Port),':');  
        
        if(
IP[0] && Port[0] && g_Password[0])
        {
            new 
Error;
            
g_Socket socket_open(IP,str_to_num(Port),SOCKET_UDP,Error,SOCK_NON_BLOCKING);

            if(
Error == SOCK_ERROR_OK)
            {
                if(
socket_is_writable(g_Socket,0))
                {
                    new 
Data[32];
                    
formatex(Data,sizeof(Data),"%c%c%c%cchallenge rcon",0xFF,0xFF,0xFF,0xFF);
                    
                    if(
socket_send2(g_Socket,Data,charsmax(Data)) != -1)
                    {
                        
g_ConnectionTime get_systime();
                        
                        
g_Forward register_forward(FM_StartFrame,"SocketRecvChallenge");
                    }
                }
            }
        }
    }
    
    return 
0;
}

public 
SocketRecvChallenge()
{
    if(
g_Socket)
    {
        if((
get_systime() - g_ConnectionTime) < CONNECTION_TIMEOUT)
        {
            if(
socket_is_readable(g_Socket,0))
            {
                new 
Data[128];
    
                if(
socket_recv(g_Socket,Data,charsmax(Data)) > 0)
                {
                    new 
Dummy[64];
                    
parse(Data,Dummy,charsmax(Dummy),Dummy,charsmax(Dummy),g_RconChallenge,charsmax(g_RconChallenge));
                    
                    
unregister_forward(FM_StartFrame,g_Forward);
                }
            }
        }
        else
        {
            
SocketClose();
        }
    }
}

SocketSendCommand(const Command[])
{
    if(
g_Socket)
    {
        if(
socket_is_writable(g_Socket,0))
        {
            new 
Data[128];
            
format(Data,charsmax(Data),"%c%c%c%crcon %s ^"%s^" %s",0xFF,0xFF,0xFF,0xFF,g_RconChallenge,g_Password,Command);    
            
            
socket_send2(g_Socket,Data,charsmax(Data));
        }
    }
}

SocketClose()
{
    if(
socket_close(g_Socket))
    {
        
g_Socket 0;
    }
    
    
unregister_forward(FM_StartFrame,g_Forward);

I have set up a variable to check time and disconnect if server do not recv rcon challenge at defined time, but have a way to do it directly with sockets?

Thanks
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
iNvectus
Member
Join Date: Sep 2014
Location: Bulgaria
Old 10-31-2019 , 10:51   Re: [Socket] Detect when remote host is offline
Reply With Quote #2

https://www.amxmodx.org/api/sockets/socket_open
PHP Code:
Default error codes:
No error
Error while creating socket
Couldn't resolve hostname
3 - Couldn'
t connect 
The 3rd is what you are looking for. If you cannot connect, it is offline - either port not opened or it is not connected to internet.
iNvectus is offline
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 10-31-2019 , 12:05   Re: [Socket] Detect when remote host is offline
Reply With Quote #3

Quote:
Originally Posted by iNvectus View Post
https://www.amxmodx.org/api/sockets/socket_open
PHP Code:
Default error codes:
No error
Error while creating socket
Couldn't resolve hostname
3 - Couldn'
t connect 
The 3rd is what you are looking for. If you cannot connect, it is offline - either port not opened or it is not connected to internet.
Yes i know, but even with remote host offline socket will return 0 no error.
So that is a bug or someting else in sockets module?

Ps. it is a UDP socket!
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 

Last edited by ^SmileY; 10-31-2019 at 12:06.
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
Natsheh
Veteran Member
Join Date: Sep 2012
Old 10-31-2019 , 16:51   Re: [Socket] Detect when remote host is offline
Reply With Quote #4

and how do you know the host is offline ?
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 10-31-2019 , 20:05   Re: [Socket] Detect when remote host is offline
Reply With Quote #5

Quote:
Originally Posted by Natsheh View Post
and how do you know the host is offline ?
I'm test it locally, using a hltv.
I simple run the plugin, without open hltv server.
Or just put a invalid address to it.
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
iNvectus
Member
Join Date: Sep 2014
Location: Bulgaria
Old 11-01-2019 , 05:36   Re: [Socket] Detect when remote host is offline
Reply With Quote #6

Try something simple to check if your sockets are working correctly. Here is an example plugin:

Code:
#include <amxmodx> #include <sockets> #define HOST "example.com" new g_iSocket; public plugin_init() {     register_plugin("Socket Test", "1.0", "_");     set_task("3.0", "tskTestSocket"); } public tskTestSocket() {     new iErr = 0;     g_iSocket = socket_open(HOST, 333, SOCKET_TCP, iErr);     switch(iErr) {         case 1: {             server_print("Error: Unable to create socket!");             return;         }         case 2: {             server_print("Error: Unable to resolve remote hostname!");             return;         }         case 3: {             server_print("Error: Unable to connect to host!");             return;         }     }     server_print("Connection successful!");     return PLUGIN_CONTINUE; }

Last edited by iNvectus; 11-01-2019 at 05:41. Reason: Fixed example
iNvectus is offline
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 11-01-2019 , 12:12   Re: [Socket] Detect when remote host is offline
Reply With Quote #7

i will try when i go to home. Thanks!
__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
SANTO37
Member
Join Date: Aug 2008
Old 11-08-2019 , 03:48   Re: [Socket] Detect when remote host is offline
Reply With Quote #8

Hi, I have a problem, I want to send a link to player's ip address on port 10048. If there is a (socket_send) connection, the player is connected.

Thanks to the program, I'm listening to port 10048. But I couldn't provide any success.

img https://imgyukle.com/i/adsiz.E4TIUS

waiting for help

Last edited by SANTO37; 11-08-2019 at 03:49.
SANTO37 is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 09:25.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode