PHP network sockets

Can I copy this PHP program to PHP?

        var chatsrv = new IPEndPoint(IPAddress.Parse(_charUrl), _chatPort);
        _chatsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        _chatsocket.Connect(chatsrv);
        var ns = new NetworkStream(_chatsocket);
        _nw = new BinaryWriter(new BufferedStream(ns));
        var nr = new BinaryReader(ns);

        _nw.Write((byte)(0xff));
        _nw.Write(acc.AccountId);
        _nw.WriteUtf8String(acc.AuthCookie);
        _nw.Write((UInt32)2);
        _nw.Flush();

        var response = nr.ReadBytes(nr.ReadInt32());

      

+2


source to share


1 answer


Perhaps it:




$chatsocket = fsockopen('IP address', $chatPort);
if ($chatsocket == FALSE) die('connection failed');

fwrite($chatsocket, "\xff");
fwrite($chatsocket, $accountId);
fwrite($chatsocket, utf8_encode($authCookie));
// memdump of 2 as little-endian 32-bits unsigned int (intel representation)
fwrite($chatsocket, "\x02\x00\x00\x00");

$bytes = fread($chatsocket, 4);

// convert little-endiang 32-bits unsigned int into PHP number
$bytes = ord($bytes[0]) | (ord($bytes[1]) << 8) | (ord($bytes[2] << 16) | (ord($bytes[3] << 24);


$response = '';
// wait for the entire block
while (!feof($chatsocket) && (strlen($response) < $bytes))
$response .= fread($chatsocket, $bytes - strlen($response));


      

+1


source







All Articles