Raised This Month: $ Target: $400
 0% 

[SOLVED] socket_listen && socket_open error


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
OvidiuS
Chillaxin'
Join Date: Dec 2009
Location: Serbia
Old 06-12-2012 , 19:50   [SOLVED] socket_listen && socket_open error
Reply With Quote #1

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 );
}

Last edited by OvidiuS; 06-13-2012 at 12:46.
OvidiuS is offline
Send a message via Skype™ to OvidiuS
 



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 06:15.


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