troubleshooting Question

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

Avatar of AlexeyTimofeev
AlexeyTimofeev asked on
PHP.NET ProgrammingVisual Basic.NET
5 Comments1 Solution481 ViewsLast Modified:
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"
        }
    }

}

}


?>

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()

I need you to translate my vb.net code into php if possible.
ASKER CERTIFIED SOLUTION
Chris Harte
2015 Top Expert (Most Article Points)

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros