$ip="****"; //Set the TCP IP Address to connect too
$port="8088"; //Set the TCP PORT to connect too
$command="hi"; //Command to run
$req['path'] = $path;
$post = json_encode($req);
//Connect to Server
$socket = stream_socket_client("tcp://{$ip}:{$port}", $errno, $errstr, 30);
if($socket) {
//Start SSL
stream_set_blocking ($socket, true);
stream_socket_enable_crypto ($socket, true, STREAM_CRYPTO_METHOD_SSLv3_CLIENT);
stream_set_blocking ($socket, false);
//Send a command
fwrite($socket, $post);
$buf = null;
//Receive response from server. Loop until the response is finished
while (!feof($socket)) {
$buf .= fread($socket, 20240);
}
//close connection
fclose($socket);
echo "<pre>";
print_r($buf); exit;
//echo our command response
return json_decode($buf);
}
This is my code. this code is working fine for below 8192 bytes value. but it can't get the above 8192 bytes what i need to get above this bytes. because i need get the more bytes of data here Please provide one example
Thanks in advance
You have set the socket in non-blocking mode:
stream_set_blocking ($socket, false);
In non-blocking mode, you should wait for data availability on the socket before trying to perform read operations. PHP provides stream_select
function for this purpose.
So you should whether make the socket blocking, or handle events with stream_select
.
When you write to a network stream, a single call to fwrite()
does not necessary writes the whole data.
There is a note at http://php.net/manual/en/function.fwrite.php which says:
Writing to a network stream may end before the whole string is written. Return value of fwrite() may be checked:
<?php
function fwrite_stream($fp, $string) {
for ($written = 0; $written < strlen($string); $written += $fwrite) {
$fwrite = fwrite($fp, substr($string, $written));
if ($fwrite === false) {
return $written;
}
}
return $written;
}
?>
You may wonder, where does this 8192 number come from.
It seems like this is the default chunk size for the stream. You can check and change chunk size via stream_set_chunk_size() function