2

Would it be possible to copy this C# program in 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());

1 Answer 1

1

Maybe this:


$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));

Sign up to request clarification or add additional context in comments.

1 Comment

Is this a solution if someone wants to communicate from PHP to an app which uses prefixed messages to specify the size of its messages? Sorry for this basic question but I'm not (yet) a professional developer...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.