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.

Saad
  • 1,155
  • 3
  • 16
  • 36
  • Possible duplicate of [How to create an empty array in PHP with predefined size?](https://stackoverflow.com/questions/5385433/how-to-create-an-empty-array-in-php-with-predefined-size) – Freggar May 15 '18 at 09:03

2 Answers2

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));
Ben Stern
  • 306
  • 1
  • 9
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";
CodeCaster
  • 147,647
  • 23
  • 218
  • 272