Link to home
Start Free TrialLog in
Avatar of AlexeyTimofeev
AlexeyTimofeev

asked on

Convert some parts of the code from VB.NET to PHP.

Hi there, I have a PHP code that connects to the server. The problem is that the server I need to connect to needs a specific authentication.

PHP CODE:

 
<?php
class IRC{
var $server_host = "96.127.149.202";
var	$server_chan = "";
var	$server_port = 11031;
var $nick = 'Rejanu';
var $server;

function Comando($cmd){
    fwrite($this->server['SOCKET'], $cmd, strlen($cmd)); //sends the command to the server
    echo "[SEND] $cmd <br>"; //displays it on the screen
	}
function Entrar(){
set_time_limit(0);
 $this->server = array(); //we will use an array to store all the server data.
    //Open the socket connection to the IRC server
    $this->server['SOCKET'] = fsockopen($this->server_host, $this->server_port, $errno, $errstr, 2);
    if($this->server['SOCKET'])
    {
        //Ok, we have connected to the server, now we have to send the login commands.
        $this->Comando("PASS NOPASS\n\r"); //Sends the password not needed for most servers
          $this->Comando("NICK $this->nick\n\r"); //sends the nickname
          $this->Comando("USER $this->nick USING PHP IRC\n\r"); //sends the user must have 4 paramters
        while(!@feof($server['SOCKET'])) //while we are connected to the server
        {
            $this->server['READ_BUFFER'] = fgets($this->server['SOCKET'], 1024); //get a line of data from the server
            echo "[RECIVE] ".$this->server['READ_BUFFER']."<br>\n\r"; //display the recived data from the server

            /*
            IRC Sends a "PING" command to the client which must be anwsered with a "PONG"
            Or the client gets Disconnected
            */
            //Now lets check to see if we have joined the server
            if(strpos($this->server['READ_BUFFER'], "422")) //422 is the message number of the MOTD for the server (The last thing displayed after a successful connection)
            {
                //If we have joined the server

                $this->Comando("JOIN $this->server_chan\n\r"); //Join the chanel
            }
            if(substr($this->server['READ_BUFFER'], 0, 6) == "PING :") //If the server has sent the ping command
            {
                $this->Comando("PONG :".substr($this->server['READ_BUFFER'], 6)."\n\r"); //Reply with pong
                //As you can see i dont have it reply with just "PONG"
                //It sends PONG and the data recived after the "PING" text on that recived line
                //Reason being is some irc servers have a "No Spoof" feature that sends a key after the PING
                //Command that must be replied with PONG and the same key sent.
            }
            flush(); //This flushes the output buffer forcing the text in the while loop to be displayed "On demand"
        }
    }

}

}


?>

Open in new window


VB.NET CODE (CORRECT ONE)

 
Private _nw As BinaryWriter
    Public s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    Private account_id As String = Nothing
    Private cookie As String = Nothing
    Private ip As String = Nothing
    Private auth_hash As String = Nothing
    Private chat_url As String = Nothing

   Dim remoteEP As New IPEndPoint(IPAddress.Parse(chat_url), &H2B17)
            s.Connect(remoteEP)
            DoLog("Connected!")
            Dim stream As New NetworkStream(s)
            Me._nw = New BinaryWriter(New BufferedStream(stream))
            DoLog("Sending login information to: masterserver...")
            Me._nw.Write(CShort(&HC00))
            Me._nw.Write(CUInt(account_id))
            Me._nw.Write(Encoding.UTF8.GetBytes(cookie & vbNullChar))
            Me._nw.Write(Encoding.UTF8.GetBytes(ip & vbNullChar))
            Me._nw.Write(Encoding.UTF8.GetBytes(auth_hash & vbNullChar))
            Me._nw.Write(CUInt(19))
            Me._nw.Write(CByte(&H1))
            Me._nw.Write(CUInt(0))
 Me._nw.Flush()

Open in new window


I need you to translate my vb.net code into php if possible.
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

I have just had a go at this. The port is open and I can connect to it but there is nothing there.

fread returns an empty string. Have you got your server set up correctly?
Avatar of AlexeyTimofeev
AlexeyTimofeev

ASKER

Its not my server. Server ip and port are correct however
I need to send i special authentication package
When connecting to it. Look at vb.net code to see what i need to send
ASKER CERTIFIED SOLUTION
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Account_id is also sent as an int. In vb cuint is a convert to integer function.
Never mind. I think what I need cannot be done in php anyway. Thanks for help.

Although if you are any good at VB.NET you may want to look on my other question:

https://www.experts-exchange.com/questions/27422160/Reading-Packet-Id-from-the-Byte-array-in-the-networkstream.html?anchorAnswerId=37059221#a37059221