AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [Socket] Detect when remote host is offline (https://forums.alliedmods.net/showthread.php?t=319419)

^SmileY 10-31-2019 07:56

[Socket] Detect when remote host is offline
 
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

iNvectus 10-31-2019 10:51

Re: [Socket] Detect when remote host is offline
 
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.

^SmileY 10-31-2019 12:05

Re: [Socket] Detect when remote host is offline
 
Quote:

Originally Posted by iNvectus (Post 2671482)
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!

Natsheh 10-31-2019 16:51

Re: [Socket] Detect when remote host is offline
 
and how do you know the host is offline ?

^SmileY 10-31-2019 20:05

Re: [Socket] Detect when remote host is offline
 
Quote:

Originally Posted by Natsheh (Post 2671515)
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.

iNvectus 11-01-2019 05:36

Re: [Socket] Detect when remote host is offline
 
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; }

^SmileY 11-01-2019 12:12

Re: [Socket] Detect when remote host is offline
 
i will try when i go to home. Thanks!

SANTO37 11-08-2019 03:48

Re: [Socket] Detect when remote host is offline
 
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


All times are GMT -4. The time now is 02:43.

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