Socket plugin procedure in script
#1

I made application that sends some integer data to socket on server but I dont know how to read it from it.

I sent some integer in C# and im trying to use those natives in socket plugin to recieve that but i cant. I dont know should i connect socket or bind or listen, whatever i try i dont get anything.

Could someone help?
Reply
#2

You will need to listen for the connection from your C# application. To listen you will need to bind a listener to what ever port you choose to use, and then the server will wait for incoming connections.

Example:
Код:
// DEFINITIONS 
#define SOCKET_MAX_CONNECTIONS 		( 1 ) 		// Change this to what ever limit you wish to use
#define SOCKET_LISTEN_PORT 			( 8000 ) 	// Change this to what ever port you wish to use

// GLOBAL VARS
new 
	Socket:socket
;

public OnGameModeInit()
{
	socket = socket_create( TCP ); // or UDP if your using that protocol

	// Validate it
	if( is_socket_valid( socket ) )
	{
		socket_set_max_connections( socket, SOCKET_MAX_CONNECTIONS ); // Limit the connections to it
		socket_listen( socket, SOCKET_LISTEN_PORT ); // Set the listen/bind port
	}
}

public onSocketRemoteConnect( Socket:id, remote_client[], remote_clientid )
{
	// Check if the socket belongs to you ( validate that its your C# app, if not destroy it )
	// Log it?

	return 1;
}

public onSocketRemoteDisconnect( Socket:id, remote_clientid )
{
	// Log it?
	return 1;
}

public onSocketReceiveData( Socket:id, remote_clientid, data[], data_len )
{
	// Validate the data sent by the socket.
	return 1;	
}
Hopefully this will help you. Remember to close the socket after you are done with it.
Reply
#3

Thank you very much for replying. I hope i can ask you few question that im not sure.

Is any port number possible to use or do i have to use some defined? I ask that because i used FTP password from server host for c# app and socket connect in script and it worked okay but get some problems later with transfering some data.

Anyway im just trying to send to script via c# application some integer that will tell me is player conneted via app or no. But when i try to listen that from script i get some errors in server log that socket listen cannot bind, port 21 is already in use. Could you try to explain what im not getting right?

How to check that socket belongs to C# app? Im using tcp.
Reply
#4

Sorry for double post, I managed to connect and send/recieve data on samp server via custom c# application.

Now I have a question, this variable that I send to sa-mp server is global, right?

How will I make that user who use application send that variable to server, connects to it (i did that) and make it only for that playerid in script. This what I did is global.

Please, look at my code:

Код:
public onSocketReceiveData(Socket:id, remote_clientid, data[], data_len)
{
	printf("Remote client [%d] has sent: %d", remote_clientid, data); // id & data

		socket_launcher[remote_clientid] = strval(data);

	return 1;
}
Than I put under OnPlayerConnect:

Код:
new string[100];
	format(string, sizeof(string), "Var socket_launcher is set on: %d", socket_launcher[playerid]);
	SendClientMessage(playerid, COLOR_WHITE, string);
	
	if(socket_launcher[playerid] != 1){ SendClientMessage(playerid, COLOR_WHITE, "You are kicked because you didn't joined via application."); Kick(playerid);}
But than, every player that connects has socket_launcher[playerid] = 1; even if he isn't connected via application. Help.


edit: Will I do it with IP check? I send IP of user that connects to samp server, i check is it the same I connected IG and than if it is equal i will let him play, otherwise i will kick him?
Reply
#5

Can someone help? Everytime I restart server (when I make changes on script) I must change port that I listen.

Server log show this:

Код:
socket_listen(): Socket has failed to bind. (IP 0.0.0.0, Port 7600)
					The port might be already in use.
Than I have to change port and it works. Someone can help?
Reply
#6

bump, can someone help?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)