0
//how to initialize byte[10240] in php  
var svr = new TcpClient();
svr.Connect(127.0.0.1, 8081);
var ns = svr.GetStream();
var outputBuffer = new byte[10240];
var pkgLen = 11
ns.Write(outputBuffer, 0, pkgLen);

I have the above C# code an I am trying to convert the code to PHP. Here is my code in php:

<?php    
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

$message = //empty array of byte    
socket_send($socket, $message);

socket_close($socket)
?>

My question is how to create an empty array of byte of length 10240 in php? Also the socket_send function takes a string not an array.

1

2 Answers 2

0

You need to generate a string with a zero byte in it, and then you need to essentially multiply it.

$zero = chr(0);
$message = str_repeat($zero, 10240);

You could also use str_pad:

$message = str_pad('', 10240, chr(0));
Sign up to request clarification or add additional context in comments.

Comments

0

What protocol requires you to write 10240 null bytes?

Anyway, the Write() overload you call only writes the first 11 (pkgLen) bytes of that array. So you need to write 11 zeroes.

You can do that hardcoded:

$message = "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0";

Comments

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.