This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

Very simple Socket_hz data recieve problem


Post New Thread Reply   
 
Thread Tools Display Modes
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 02-01-2015 , 13:24   Re: Very simple Socket_hz data recieve problem
Reply With Quote #11

I think what you (or exolent) are missing in that code is the socket_change() check before accepting a connection. You are constantly trying to accept a connection when there is not one there waiting to be accepted.

If you use AMX-X Studio, use the built-in socket terminal (Tools -> Socket Terminal) to play with this along with the server console.
PHP Code:
#include <amxmodx>
#include <sockets_hz>

new const VERSION[] = "0.1";

const 
SERVER_SOCKETS 50;

const 
TASK_LISTEN 4345;
const 
TASK_SERVER 4346;

new 
g_ServerSERVER_SOCKETS ];
new 
g_ListenSocket;

public 
plugin_init() 
{
    
register_plugin"Sockets server demo" VERSION "bugsy" );
    
    
register_concmd"test" "Test" );
    
register_concmd"broadcast" "SayMsg" );
}

public 
plugin_end()
{
    
socket_closeg_ListenSocket );
    
    for ( new 
SERVER_SOCKETS i++ )
    {
        if ( 
g_Server] )
        {
            
socket_closeg_Server] );
        }
    }
}

public 
Test()
{
    new 
iError;
    
    if ( ( 
g_ListenSocket socket_listen"127.0.0.1" 2323 SOCKET_TCP iError ) ) > )
    {
        
socket_unblockg_ListenSocket );
        
        
set_task0.25 "Listen" , .id=TASK_LISTEN , .flags="b" );
        
set_task0.25 "Server" , .id=TASK_SERVER , .flags="b" );
        
        
server_print"Now listening for connections..." );
    }
}

public 
Listen()
{
    if ( 
socket_changeg_ListenSocket ) )
    {
        
server_print"Connection requested" );
        
        new 
iSocket GetOpenSocket();
        
        if ( 
iSocket == -)
        {
            
server_print"Declined connection request, all server sockets are full!" );
        }
        else
        {
            if ( ( 
g_ServeriSocket ] = socket_acceptg_ListenSocket ) ) > )
            {
                
server_print"Connection request successfully accepted!" );
                
                for ( new 
SERVER_SOCKETS i++ )
                {
                    if ( 
g_Server] )
                    {
                        
socket_sendg_Server] , "New connection to the server!" 30 );
                    }
                }
            }
        }
    }
}

public 
Server()
{
    static 
szRecv128 ];
    
    for ( new 
iCheck iCheck SERVER_SOCKETS iCheck++ )
    {
        if ( 
g_ServeriCheck ] && socket_changeg_ServeriCheck ] ) )
        {
            if ( 
socket_recvg_ServeriCheck ] , szRecv charsmaxszRecv ) ) )
            {
                
server_print"[%d] %s" iCheck szRecv );
                
                for ( new 
SERVER_SOCKETS i++ )
                {
                    if ( 
g_Server] )
                    {
                        
socket_sendg_Server] , szRecv charsmaxszRecv ) );
                    }
                }
            }
            else
            {
                
server_print"[%d] disconnected!" iCheck );
                
socket_closeg_ServeriCheck ] );
                
g_ServeriCheck ] = 0;
            }
        }
    }
}

GetOpenSocket()
{
    new 
iFound = -1;
    
    for ( new 
SERVER_SOCKETS i++ )
    {
        if ( 
g_Server] == )
        {
            
iFound i;
            break;
        }
    }
    
    return 
iFound;
}

public 
SayMsg()
{
    for ( new 
SERVER_SOCKETS i++ )
    {
        if ( 
g_Server] )
        {
            
socket_sendg_Server] , "[SERVER] Hello clients!" 24 );
        }
    }

__________________

Last edited by Bugsy; 02-01-2015 at 13:49.
Bugsy is offline
Guxi
Junior Member
Join Date: Aug 2014
Old 02-01-2015 , 17:15   Re: Very simple Socket_hz data recieve problem
Reply With Quote #12

Quote:
Originally Posted by Bugsy View Post
I think what you (or exolent) are missing in that code is the socket_change() check before accepting a connection. You are constantly trying to accept a connection when there is not one there waiting to be accepted.

If you use AMX-X Studio, use the built-in socket terminal (Tools -> Socket Terminal) to play with this along with the server console.
PHP Code:
#include <amxmodx>
#include <sockets_hz>

new const VERSION[] = "0.1";

const 
SERVER_SOCKETS 50;

const 
TASK_LISTEN 4345;
const 
TASK_SERVER 4346;

new 
g_ServerSERVER_SOCKETS ];
new 
g_ListenSocket;

public 
plugin_init() 
{
    
register_plugin"Sockets server demo" VERSION "bugsy" );
    
    
register_concmd"test" "Test" );
    
register_concmd"broadcast" "SayMsg" );
}

public 
plugin_end()
{
    
socket_closeg_ListenSocket );
    
    for ( new 
SERVER_SOCKETS i++ )
    {
        if ( 
g_Server] )
        {
            
socket_closeg_Server] );
        }
    }
}

public 
Test()
{
    new 
iError;
    
    if ( ( 
g_ListenSocket socket_listen"127.0.0.1" 2323 SOCKET_TCP iError ) ) > )
    {
        
socket_unblockg_ListenSocket );
        
        
set_task0.25 "Listen" , .id=TASK_LISTEN , .flags="b" );
        
set_task0.25 "Server" , .id=TASK_SERVER , .flags="b" );
        
        
server_print"Now listening for connections..." );
    }
}

public 
Listen()
{
    if ( 
socket_changeg_ListenSocket ) )
    {
        
server_print"Connection requested" );
        
        new 
iSocket GetOpenSocket();
        
        if ( 
iSocket == -)
        {
            
server_print"Declined connection request, all server sockets are full!" );
        }
        else
        {
            if ( ( 
g_ServeriSocket ] = socket_acceptg_ListenSocket ) ) > )
            {
                
server_print"Connection request successfully accepted!" );
                
                for ( new 
SERVER_SOCKETS i++ )
                {
                    if ( 
g_Server] )
                    {
                        
socket_sendg_Server] , "New connection to the server!" 30 );
                    }
                }
            }
        }
    }
}

public 
Server()
{
    static 
szRecv128 ];
    
    for ( new 
iCheck iCheck SERVER_SOCKETS iCheck++ )
    {
        if ( 
g_ServeriCheck ] && socket_changeg_ServeriCheck ] ) )
        {
            if ( 
socket_recvg_ServeriCheck ] , szRecv charsmaxszRecv ) ) )
            {
                
server_print"[%d] %s" iCheck szRecv );
                
                for ( new 
SERVER_SOCKETS i++ )
                {
                    if ( 
g_Server] )
                    {
                        
socket_sendg_Server] , szRecv charsmaxszRecv ) );
                    }
                }
            }
            else
            {
                
server_print"[%d] disconnected!" iCheck );
                
socket_closeg_ServeriCheck ] );
                
g_ServeriCheck ] = 0;
            }
        }
    }
}

GetOpenSocket()
{
    new 
iFound = -1;
    
    for ( new 
SERVER_SOCKETS i++ )
    {
        if ( 
g_Server] == )
        {
            
iFound i;
            break;
        }
    }
    
    return 
iFound;
}

public 
SayMsg()
{
    for ( new 
SERVER_SOCKETS i++ )
    {
        if ( 
g_Server] )
        {
            
socket_sendg_Server] , "[SERVER] Hello clients!" 24 );
        }
    }

I've sent "rcon test" to the console.
Then i tried to sent send a tcp packet from and it establishes connection like before but i dont get any message on the server as it was supposed to appear.

This never gets activated, same like any other server_print command except for "Now listening for connections".
PHP Code:
public Listen()
{
    if ( 
socket_changeg_ListenSocket ) )
    {
        
server_print"Connection requested" );
    ... 
  } 
Am i missing to do something or?
Thanks.
Guxi is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 02-01-2015 , 17:18   Re: Very simple Socket_hz data recieve problem
Reply With Quote #13

Im not sure but it works for me.

Do 'test' command which starts the listen server, then connect to the server with a client. I can take screen shots of it working if you want. You must be doing something wrong.
__________________
Bugsy is offline
Guxi
Junior Member
Join Date: Aug 2014
Old 02-01-2015 , 18:01   Re: Very simple Socket_hz data recieve problem
Reply With Quote #14

Quote:
Originally Posted by Bugsy View Post
Im not sure but it works for me.

Do 'test' command which starts the listen server, then connect to the server with a client. I can take screen shots of it working if you want. You must be doing something wrong.
What client are you using to connect?
I'd like to see some screenshot's if i may.
Guxi is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 02-01-2015 , 18:17   Re: Very simple Socket_hz data recieve problem
Reply With Quote #15

As said above, using the console built into AMX-X studio along with hlds
__________________
Bugsy is offline
Guxi
Junior Member
Join Date: Aug 2014
Old 02-01-2015 , 19:01   Re: Very simple Socket_hz data recieve problem
Reply With Quote #16

Quote:
Originally Posted by Bugsy View Post
As said above, using the console built into AMX-X studio along with hlds
I was using 2 different clients. Socket terminal from the AMX-X studio & another 'packet sender' program.
I've successfully called a function by "rcon test", but nothing else was printed.
Guxi is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 02-03-2015 , 19:59   Re: Very simple Socket_hz data recieve problem
Reply With Quote #17

The sockets console in AMX-X studio is a little weird..

Once a client connects, server sends "New connection to the server!", which didn't appear until I said from the console "Hello server". From server console I then did "broadcast" command, which sends "[SERVER] Hello clients!", but that did not appear until I sent ".." from sockets console. Not really relevant to your issue, but just so you have an idea of whats going in in my screen shot.
__________________

Last edited by Bugsy; 02-03-2015 at 20:07.
Bugsy 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 10:06.


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