Raised This Month: $ Target: $400
 0% 

[SOLVED] socket_listen && socket_open error


Post New Thread Reply   
 
Thread Tools Display Modes
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
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 06-12-2012 , 21:23   Re: socket_listen && socket_open error
Reply With Quote #2

You are listening on port 1377 and sending on port 1337. I haven't dealt with sockets too much, but I would assume that is your problem.

Edit: Nevermind, see Bugsy's post.

Last edited by Emp`; 06-12-2012 at 22:41.
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-12-2012 , 21:28   Re: socket_listen && socket_open error
Reply With Quote #3

Did you just copy this and modify it?
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-12-2012 , 22:04   Re: socket_listen && socket_open error
Reply With Quote #4

There shouldn't be a problem with listening on one socket and connecting\sending with a different socket on the same port, you just cannot have multiple sockets listening on the same port. I don't have time to test this for you but one thing I see is you should check socket_change() on the listening socket before calling socket_accept(); this indicates that there is a connection request. Take a look at the sockets_forwards in the .inc file, it might help you solve this. Did you add some logs\[clientserver\console]_print's to confirm the entity is thinking and each piece of code is getting reached?
__________________

Last edited by Bugsy; 06-12-2012 at 22:05.
Bugsy is offline
OvidiuS
Chillaxin'
Join Date: Dec 2009
Location: Serbia
Old 06-13-2012 , 04:17   Re: socket_listen && socket_open error
Reply With Quote #5

Quote:
Originally Posted by Exolent[jNr] View Post
Did you just copy this and modify it?
Yup, i used server comm plugin, and mainly your socket sender plugin.
Check this thread http://forums.alliedmods.net/showthread.php?t=80413 and answer me if you can
I'm testing socket functions and transfering strings from one server to another server, but because i have only one server, i'm sending data to same ip/port on which is listening socket.

Quote:
Originally Posted by Bugsy View Post
There shouldn't be a problem with listening on one socket and connecting\sending with a different socket on the same port, you just cannot have multiple sockets listening on the same port. I don't have time to test this for you but one thing I see is you should check socket_change() on the listening socket before calling socket_accept(); this indicates that there is a connection request. Take a look at the sockets_forwards in the .inc file, it might help you solve this. Did you add some logs\[clientserver\console]_print's to confirm the entity is thinking and each piece of code is getting reached?
I don't have multiple listening sockets. I added logs, listening socket is called with "id" 7, and entity is working. SendSocket is also created, and data is sent. Just that line in think function isn't printed when i use sending function. Problem must be in socket_accept. I added debug line before socket_accept, and after entity check, and it's printed.

About socket_forward.inc, i checked almost "all socket codes" here, and noticed when using TCP socket, there is no need for socket_change check.

I forgot to mention, before puting all this in one plugin, i used 2 plugins, one for reading, one for sending, and it worked like a charm. Server is hosted on linux platform, and i'm currently using socket_hz module, modified by joaquimandrade ( Link: Here )

EDIT: Solved, i made a small tipo, check first post ( listen func )
EDIT2: joaquimandrade socket_open_non_blocking doesn't return error on connection fail, i switched back to original socket_hz.

Last edited by OvidiuS; 06-13-2012 at 12:33.
OvidiuS is offline
Send a message via Skype™ to OvidiuS
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-13-2012 , 11:25   Re: socket_listen && socket_open error
Reply With Quote #6

Quote:
Originally Posted by OvidiuS View Post
Yup, i used server comm plugin, and mainly your socket sender plugin.
Check this thread http://forums.alliedmods.net/showthread.php?t=80413 and answer me if you can
I'm testing socket functions and transfering strings from one server to another server, but because i have only one server, i'm sending data to same ip/port on which is listening socket.


I don't have multiple listening sockets. I added logs, listening socket is called with "id" 7, and entity is working. SendSocket is also created, and data is sent. Just that line in think function isn't printed when i use sending function. Problem must be in socket_accept. I added debug line before socket_accept, and after entity check, and it's printed.

About socket_forward.inc, i checked almost "all socket codes" here, and noticed when using TCP socket, there is no need for socket_change check.

I forgot to mention, before puting all this in one plugin, i used 2 plugins, one for reading, one for sending, and it worked like a charm. Server is hosted on linux platform, and i'm currently using socket_hz module, modified by joaquimandrade ( Link: Here )

EDIT: Solved, i made a small tipo, check first post ( listen func )
I wasn't implying that you were using multiple listening sockets on the same port, I was just making a statement that it cannot be done.

What do you mean there is no need for a socket_change() check? socket_change() calls the select() function which determines if any activity (data-arrival, socket-writeability, connection request, remote party disconnect) has occurred on a given socket so you can then react to it accordingly. Please explain why this isn't needed, or shouldn't be used.
__________________

Last edited by Bugsy; 06-13-2012 at 11:36.
Bugsy is offline
OvidiuS
Chillaxin'
Join Date: Dec 2009
Location: Serbia
Old 06-13-2012 , 12:23   Re: socket_listen && socket_open error
Reply With Quote #7

Quote:
Originally Posted by Bugsy View Post
I wasn't implying that you were using multiple listening sockets on the same port, I was just making a statement that it cannot be done.

What do you mean there is no need for a socket_change() check? socket_change() calls the select() function which determines if any activity (data-arrival, socket-writeability, connection request, remote party disconnect) has occurred on a given socket so you can then react to it accordingly. Please explain why this isn't needed, or shouldn't be used.
I misunderstood you.

About socket_change, i said that i noticed that it isn't used on TCP socket listen.
I saw that from tutorials on this forum. You are probably right, but i haven't seen that anyone is using it in TCP socket. I'm relativly new to sockets, used them once/twice before and thats it.

Examples which i found:
https://forums.alliedmods.net/showpo...50&postcount=1
http://forums.alliedmods.net/showthread.php?t=150519 - api

One more question
I send strings which are not related to other strings which are also sent.
Can i do this with UDP socket?
Important thing is:
  • Data must be received
  • No duplicated data
  • Received data must be in same form as data which was sent.

Last edited by OvidiuS; 06-13-2012 at 12:28.
OvidiuS is offline
Send a message via Skype™ to OvidiuS
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-13-2012 , 12:40   Re: socket_listen && socket_open error
Reply With Quote #8

I would stick with TCP:

TCP
  • Connection based
  • Guaranteed reliable and ordered
  • Automatically breaks up your data into packets for you
  • Makes sure it doesn’t send data too fast for the internet connection to handle (flow control)
  • Easy to use, you just read and write data like its a file
UDP
  • No concept of connection, you have to code this yourself
  • No guarantee of reliability or ordering of packets, they may arrive out of order, be duplicated, or not arrive at all!
  • You have to manually break your data up into packets and send them
  • You have to make sure you don’t send data too fast for your internet connection to handle
  • If a packet is lost, you need to devise some way to detect this, and resend that data if necessary
__________________
Bugsy is offline
OvidiuS
Chillaxin'
Join Date: Dec 2009
Location: Serbia
Old 06-13-2012 , 12:45   Re: socket_listen && socket_open error
Reply With Quote #9

Thank you bugsy fo helping
OvidiuS is offline
Send a message via Skype™ to OvidiuS
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 06:15.


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