In one plugin i'm creating TCP listen socket on local 127.0.0.1 ip address. (port: 1337)
Also in that plugin i'm trying to create new socket via socket_open to send data on local port.
Problem is that when i use socket_open for sending i get connection error.
I tried using socket_open_non_blocking and then socket is created, data is sent, but listen socket doesn't receive anything.
Here are the func:
SEND Data:
Code:
SendData( const sOne[ ], const sTwo[ ], iNum )
{
new iError, hSocket = socket_open_non_blocking( "127.0.0.1", 1337, SOCKET_TCP, iError );
if( hSocket <= 0 || iError )
{
switch( iError )
{
case 1: client_print( 0, print_chat, "Error creating TCP socket for 127.0.0.1:1337" );
case 2: client_print( 0, print_chat, "Error resolving the hostname for 127.0.0.1:1337" );
case 3: client_print( 0, print_chat, "Error connecting a TCP socket for 127.0.0.1:1337" );
}
return;
}
new sData[ 256 ];
formatex( sData, charsmax( sData ), "%s %s %i", sOne, sTwo, iNum );
socket_send( hSocket, sData, strlen( sData ) );
socket_close( hSocket );
}
Listen & Receive socket
Code:
public plugin_init( )
{
new iError;
g_hSocket = socket_listen( "127.0.0.1", 1377, SOCKET_TCP, iError );
if( g_hSocket <= 0 || iError )
{
switch( iError )
{
case 1: log_amx( "Error creating TCP socket for port 1377" );
case 2: log_amx( "Error resolving the hostname for port 1377" );
case 3: log_amx( "Error connecting a TCP socket for port 1377" );
}
return;
}
socket_unblock( g_hSocket );
g_iSockEnt = create_entity( "info_target" );
if( is_valid_ent( g_iSockEnt ) )
{
new const sClassName[ ] = "ListenSocket";
entity_set_string( g_iSockEnt, EV_SZ_classname, sClassName );
entity_set_float( g_iSockEnt, EV_FL_nextthink, get_gametime( ) + 0.1 );
register_think( sClassName, "fwd_SocketThink" );
}
else
{
log_amx( "Could not create socket reader entity." );
plugin_end( );
}
}
public fwd_SocketThink( iEntity )
{
if( g_iSockEnt != iEntity ) return;
new hSocket = socket_accept( g_hSocket )
if( hSocket > 0 )
{
client_print( 0, print_chat, "Received data" ); // This isn't printed when i use SendData
set_task( 1.0, "Task_ReceiveData", hSocket );
}
entity_set_float( iEntity, EV_FL_nextthink, get_gametime( ) + 0.1 );
}