Hello,
I have a problem, I needed a change from C++ to C#. help anyone? I made it from this: (

This is script in C++

using namespace System;
using namespace System::Text;
using namespace System::Net;
using namespace System::Net::Sockets;
String^ chr(String^ str)	{
	Char^ c = Convert::ToChar(Convert::ToInt32(str));
	return Convert::ToString(c);
}
Int64^ ord(String^ value)	{
	System::Char^ c = System::Char(value[0]);
    return Convert::ToInt64(c);
}
ref class RemoteAdminException : Exception	{
public :
	String^ Exception;
	RemoteAdminException(String^ ex)	{
		Exception = ex;
	}
	String^ GetError(void)	{
		return Exception;
	}
};
public ref class RemoteAdmin	{
	public:
		String^ add;
		String^ pw;
		int port;
		UdpClient^ udpClient;
		property String^ Adress	{
			String^ get()	{
				return add;
			}
			void set(String^ str)	{
				add=str;
			}
		}
		property String^ RconPassword	{
			String^ get()	{
				return pw;
			}
			void set(String^ str)	{
				pw=str;
			}
		}
		property int Port	{
			int get()	{
				return port;
			}
		    void set(int i)	{
				port=i;
			}
		}
		RemoteAdmin(String^ IP,int Porta,String^ Password)
		{
			udpClient = gcnew UdpClient;
			IPAddress^ address;
			if(!IPAddress::TryParse(IP,address))
				throw gcnew RemoteAdminException("Invalid IP-Adress");
			this->Adress = IP;
			this->Port = Porta;
			this->RconPassword = Password;
			try	{
				udpClient->Connect(this->Adress,this->Port);
			}
			catch(SocketException^ e)	{
				throw gcnew RemoteAdminException("Cannot Connect to Server");
			}
			catch(ArgumentOutOfRangeException^ e)	{
				throw gcnew RemoteAdminException("Port out of Range");
			}
			if(this->SendCommand("echo NatKingCole's Remote Admin Connected to Server") == "Invalid RCON password.")
				throw gcnew RemoteAdminException("Invalid Rcon Password");
	    }
		String^ SendCommand(String^ cmd)	{
			array<String^>^ ips = this->Adress->Split('.');
			String^ packet = "SAMP";
			for each(String^ ip in ips)	{
				packet += chr(ip);
			}
			packet += chr(Convert::ToString(this->Port & 0xFF))+chr(Convert::ToString(this->Port >> 8 & 0xFF));
			packet += "x";
			packet += chr(Convert::ToString(this->RconPassword->Length & 0xFF))+chr(Convert::ToString(this->RconPassword->Length >> 8 & 0xFF))+this->RconPassword;
			packet += chr(Convert::ToString(cmd->Length & 0xFF))+chr(Convert::ToString(cmd->Length >> 8 & 0xFF))+cmd;
		    array<Byte>^sendBytes = Encoding::ASCII->GetBytes( packet );
			String^ returnData;
			try	{
			udpClient->Send(sendBytes, sendBytes->Length );
			IPEndPoint^ RemoteIpEndPoint = gcnew IPEndPoint( IPAddress::Any,0);
		    array<Byte>^receiveBytes = this->udpClient->Receive( RemoteIpEndPoint );
		    returnData = Encoding::ASCII->GetString( receiveBytes );
			}
			catch(Exception^ e)	{
				throw gcnew RemoteAdminException(e->Message);
			}
			String^ retval = returnData->ToString();
			return retval->Remove(0,13);
		}
		int toInteger(String^ Data)
 		{
 		int ret = 0;
		ret += ( Convert::ToInt32(ord(Convert::ToString( Data[ 0 ] ))));
 		ret += ( Convert::ToInt32(ord(Convert::ToString( Data[ 1 ] ))) << 8 );
 		ret += ( Convert::ToInt32(ord(Convert::ToString( Data[ 2 ] ))) << 16 );
 		ret += ( Convert::ToInt32(ord(Convert::ToString( Data[ 3 ] ))) << 24 );
 		if( ret >= 4294967294 )
 			ret -= 4294967296;
 		return ret;
 		}
		array<String^,2>^ Playerlist()	{
			array<String^>^info =  GetInfo();
			int APlayers = Convert::ToInt32(info[1]);
			if(APlayers > 0)	{
				array<String^,2>^ ret = gcnew array<String^,2>(APlayers,4);
				this->SendPacket("d");
				IPEndPoint^ RemoteIpEndPoint = gcnew IPEndPoint( IPAddress::Any,0 );
				array<Byte>^receiveBytes;
				receiveBytes = this->udpClient->Receive( RemoteIpEndPoint );
				int i = 0;
				while(i != APlayers)	{
					ret[i,0] = Convert::ToString(ord(Encoding::ASCII->GetString(receiveBytes,11,4)));
					int name_lenght = Convert::ToInt32(ord(Encoding::ASCII->GetString(receiveBytes,14,2)));
					ret[i,1] = Encoding::ASCII->GetString(receiveBytes,15,name_lenght);
					ret[i,2] = Convert::ToString(toInteger(Encoding::ASCII->GetString(receiveBytes,15+name_lenght,4)));
					ret[i,3] = Convert::ToString(ord(Encoding::ASCII->GetString(receiveBytes,19+name_lenght,4)));
					i++;
				}
				return ret;
			}
			else	{
				return nullptr;
			}
 
		}
		void SendPacket(String^ pack)	{
			array<String^>^ ips = this->Adress->Split('.');
			String^ packet = "SAMP";
			for each(String^ ip in ips)	{
				packet += chr(ip);
			}
			packet += chr(Convert::ToString(this->Port & 0xFF))+chr(Convert::ToString(this->Port >> 8 & 0xFF));
			packet += pack;
			array<Byte>^sendBytes = Encoding::ASCII->GetBytes( packet );
			udpClient->Send(sendBytes, sendBytes->Length );
		}
		array<String^>^ GetInfo()	{
			array<String^>^ ret = gcnew array<String^>(6);
			this->SendPacket("i");
			IPEndPoint^ RemoteIpEndPoint = gcnew IPEndPoint( IPAddress::Any,0 );
			array<Byte>^receiveBytes;
			receiveBytes = udpClient->Receive( RemoteIpEndPoint );
			ret[0] = Convert::ToString(ord(Encoding::ASCII->GetString(receiveBytes,11,11)));
			ret[1] = Convert::ToString(ord(Encoding::ASCII->GetString(receiveBytes,12,12)));
			ret[2] = Convert::ToString(ord(Encoding::ASCII->GetString(receiveBytes,14,14)));
			int hostname_length = Convert::ToInt32(ord(Encoding::ASCII->GetString(receiveBytes,16,16) ) );
			ret[3] = Encoding::ASCII->GetString(receiveBytes,20,hostname_length ) ;
			int script_length = Convert::ToInt32(ord(Convert::ToString(Encoding::ASCII->GetString(receiveBytes,20+hostname_length,2 ) )));
			ret[4] = Encoding::ASCII->GetString(receiveBytes,24+hostname_length,script_length ) ;
			int map_length = Convert::ToInt32(ord(Convert::ToString(Encoding::ASCII->GetString(receiveBytes,24+hostname_length+script_length,2 ) )));
			ret[5] = Encoding::ASCII->GetString(receiveBytes,28+hostname_length+script_length,map_length ) ;
			return ret;
		}
		array<String^>^ GetRules()	{
			array<String^>^ ret = gcnew array<String^>(7);
			this->SendPacket("r");
			IPEndPoint^ RemoteIpEndPoint = gcnew IPEndPoint( IPAddress::Any,0 );
			array<Byte>^receiveBytes;
			udpClient->Receive( RemoteIpEndPoint );
			receiveBytes = udpClient->Receive( RemoteIpEndPoint );
			//Gravity
			int grav_lg = Convert::ToInt32( ord( Encoding::ASCII->GetString(receiveBytes,13,2) ) );
			int grav_val_lg = Convert::ToInt32( ord( Encoding::ASCII->GetString(receiveBytes,14+grav_lg,2) ) );
			ret[0] = Encoding::ASCII->GetString(receiveBytes,14,grav_lg)+":"+Encoding::ASCII->GetString(receiveBytes,15+grav_lg,grav_val_lg);
			//Instagib
			int offset_insta = 15+grav_val_lg+grav_lg;
			int instagib_lg = Convert::ToInt32( ord( Encoding::ASCII->GetString(receiveBytes,offset_insta,2) ) );
			int instagib_val_lg = Convert::ToInt32( ord( Encoding::ASCII->GetString(receiveBytes,offset_insta+instagib_lg+1,2) ) );
			ret[1] = Encoding::ASCII->GetString(receiveBytes,offset_insta+1,instagib_lg)+":"+Encoding::ASCII->GetString(receiveBytes,offset_insta+instagib_lg+2,instagib_val_lg);
			//Mapname
			int offset_mapname = offset_insta+instagib_val_lg+instagib_lg+2;
			int mapname_lg = Convert::ToInt32( ord( Encoding::ASCII->GetString(receiveBytes,offset_mapname,2) ) );
			int mapname_val_lg = Convert::ToInt32( ord( Encoding::ASCII->GetString(receiveBytes,offset_mapname+mapname_lg+1,2) ) );
			ret[2] = Encoding::ASCII->GetString(receiveBytes,offset_mapname+1,mapname_lg)+":"+Encoding::ASCII->GetString(receiveBytes,offset_mapname+mapname_lg+2,mapname_val_lg);
			//Version
			int offset_version = offset_mapname+mapname_val_lg+mapname_lg+2;
			int version_lg = Convert::ToInt32( ord( Encoding::ASCII->GetString(receiveBytes,offset_version,2) ) );
			int version_val_lg = Convert::ToInt32( ord( Encoding::ASCII->GetString(receiveBytes,offset_version+version_lg+1,2) ) );
			ret[3] = Encoding::ASCII->GetString(receiveBytes,offset_version+1,version_lg)+":"+Encoding::ASCII->GetString(receiveBytes,offset_version+version_lg+2,version_val_lg);
			//Wetter
			int offset_weather = offset_version+version_val_lg+version_lg+2;
			int weather_lg = Convert::ToInt32( ord( Encoding::ASCII->GetString(receiveBytes,offset_weather,2) ) );
			int weather_val_lg = Convert::ToInt32( ord( Encoding::ASCII->GetString(receiveBytes,offset_weather+weather_lg+1,2) ) );
			ret[4] = Encoding::ASCII->GetString(receiveBytes,offset_weather+1,weather_lg)+":"+Encoding::ASCII->GetString(receiveBytes,offset_weather+weather_lg+2,weather_val_lg);
			//Weburl
			int offset_weburl = offset_weather+weather_val_lg+weather_lg+2;
			int weburl_lg = Convert::ToInt32( ord( Encoding::ASCII->GetString(receiveBytes,offset_weburl,2) ) );
			int weburl_val_lg = Convert::ToInt32( ord( Encoding::ASCII->GetString(receiveBytes,offset_weburl+weburl_lg+1,2) ) );
			ret[5] = Encoding::ASCII->GetString(receiveBytes,offset_weburl+1,weburl_lg)+":"+Encoding::ASCII->GetString(receiveBytes,offset_weburl+weburl_lg+2,weburl_val_lg);
			//worldtime
			int offset_worldtime = offset_weburl+weburl_val_lg+weburl_lg+2;
			int worldtime_lg = Convert::ToInt32( ord( Encoding::ASCII->GetString(receiveBytes,offset_worldtime,2) ) );
			int worldtime_val_lg = Convert::ToInt32( ord( Encoding::ASCII->GetString(receiveBytes,offset_worldtime+worldtime_lg+1,2) ) );
			ret[6] = Encoding::ASCII->GetString(receiveBytes,offset_worldtime+1,worldtime_lg)+":"+Encoding::ASCII->GetString(receiveBytes,offset_worldtime+worldtime_lg+2,worldtime_val_lg);
			return ret;
		}
 
};

AND this is my change to c# :D ..where problem ?

void GetServerInfo(string socketIP, int socketPORT, string pack)
        {
            String packet = "SAMP";
            String[] ret = new String[6];

            foreach (String ip in socketIP) { packet += chr(ip); }
            packet += chr(Convert.ToString(socketPORT & 0xFF)) + chr(Convert.ToString(socketPORT >> 8 & 0xFF));
            packet += pack;
            Byte[] bufferTemp = Encoding.ASCII.GetBytes(packet);
            Byte[] bufferSend = new Byte[bufferTemp.Length + 5];
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            client.Connect(IPAddress.Parse(socketIP), socketPORT);
            client.Send(bufferSend, SocketFlags.None); 
            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
            Byte[] bufferRec = new Byte[64999];
            client.Receive(bufferRec);

            ret[0] = Convert.ToString(ord(Encoding.ASCII.GetString(bufferRec, 11, 11)));
            ret[1] = Convert.ToString(ord(Encoding.ASCII.GetString(bufferRec, 12, 12)));
            ret[2] = Convert.ToString(ord(Encoding.ASCII.GetString(bufferRec, 14, 14)));
			
            int hostname_length = Convert.ToInt32(ord(Encoding.ASCII.GetString(bufferRec,16,16) ) );
			ret[3] = Encoding.ASCII.GetString(bufferRec,20,hostname_length ) ;
			int script_length = Convert.ToInt32(ord(Convert.ToString(Encoding.ASCII.GetString(bufferRec,20+hostname_length,2 ) )));
			ret[4] = Encoding.ASCII.GetString(bufferRec,24+hostname_length,script_length ) ;
            int map_length = Convert.ToInt32(ord(Convert.ToString(Encoding.ASCII.GetString(bufferRec, 24 + hostname_length + script_length, 2))));
            ret[5] = Encoding.ASCII.GetString(bufferRec, 28 + hostname_length + script_length, map_length);
            
            MessageBox.Show(" "+ret+" ", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

AND in PHP code ..:

$ip = "93.190.49.104";
$port = "7777";
$fp = @fsockopen('udp://' . $ip, $port);
if (!$fp)
{
//AddMinitableItem("Stav serveru", "offline");
fclose($fp);
}
else
{
$packet = 'SAMP';
$packet .= chr(strtok($ip, '.'));
$packet .= chr(strtok('.'));
$packet .= chr(strtok('.'));
$packet .= chr(strtok('.'));
$packet .= chr($port & 0xFF);
$packet .= chr($port >> 8 & 0xFF);

fwrite($fp, $packet.'i');
@fread($fp, 11);

stream_set_blocking($fp, TRUE);
stream_set_timeout($fp,0.5);
$info = stream_get_meta_data($fp);

$is_passworded = ord(fread($fp, 1));
$plr_count     = ord(fread($fp, 2));
$max_plrs      = ord(fread($fp, 2));
$strlen        = ord(fread($fp, 4));
$hostname      = htmlentities(@fread($fp, $strlen));
$strlen        = ord(fread($fp, 4));
$gamemode      = htmlentities(@fread($fp, $strlen));
$strlen        = ord(fread($fp, 4));

Thanks all..

Edited 6 Years Ago by KozZZak: add PHP script

This article has been dead for over six months. Start a new discussion instead.