fsockopen 


php128 apg

OPENS Internet or Unix domain socket connection.



<?php

res
|false fsockopen str $hostname 
                                 
int $port = -
                              
int &$error_code null 
                              
str &$error_message null 
                      
float|null $timeout null )


where,

$hostname The remote TCP/IP to connect

$port 
The port number
             
&$error_code The error system-level 

&$error_message The error message

$timeout 
The connection timeout

?>

 $hostname 


If OpenSSL support is installed, you may prefix the $hostname with either ssl:// or tls:// to use an SSL or TLS client connection over TCP/IP to connect to the remote host.



 $port 


The port number that cam be omitted and skipped to -1 for transports that do not use ports, such as unix://.



 &$error_code 


The number of that occurred in the system-level in the connetion call.

If &$error_code = 0 and the function returs FALSE indicates that an error occurred before the connetion call, this is an indication of a problem with the initializing the socket.



 &$error_message 


The error message.



 $timeout 


The connection timeout, in seconds.





This function returns a file pointer as RESOURCE on SUCCESS.

This function returns a FALSE on ERROR.

This function returns E_WARNING if $hostname is not a valid domain.

This file pointer may be used together with fgets, fgetss, fwrite, fclose and feof.

Since the PHP 8.0.0 $timout is nullable.



  1 EXERCISE   

<?php

echo "Open a server socket<br>";

for (
$i=0$i<100$i++) {
  
$port rand(1000065000);
  
/* Setup socket server */
  
$server = @stream_socket_server("tcp://127.0.0.1:$port");
  if (
$server) {
    break;
  }
}

// Initialise all required variables
$hostname 'tcp://127.0.0.1'
// loopback address
$errno null;
$errstr null;
$timeout 1.5;

echo 
"<br><br>Calling fsockopen() with all possible arguments:<br>";
$client fsockopen($hostname$port$errno$errstr$timeout);
var_dump($client);
fclose($client);

echo 
"<br><br>Calling fsockopen() with mandatory arguments:<br>";
$second_client fsockopen($hostname$port);
var_dump($second_client);
fclose($second_client);

echo 
"<br><br>Calling fsockopen() with address and port in same string:<br>";
$address $hostname ':' $port;
$third_client fsockopen($address);
var_dump($third_client);
fclose($third_client);

?>

  2 EXERCISE   

<?php

echo "Open a server socket<br>";
for (
$i=0$i<100$i++) {
  
$port rand(1000065000);
  
/* Setup socket server */
  
$server = @stream_socket_server("tcp://127.0.0.1:$port");
  if (
$server) {
    break;
  }
}

echo 
"<br><br>Calling fsockopen() 
        without a protocol in the hostname string:<br>"
;
$hostname '127.0.0.1';
$client fsockopen($hostname$port);
var_dump($client);
fclose($client);

echo 
"<br><br>Calling fsockopen() 
             with address and port in same string, without a protocol:<br>"
;
$address $hostname ':' $port;
$second_client fsockopen($address);
var_dump($second_client);
fclose($second_client);

echo 
"Done";
?>

  3 EXERCISE   

<?php

$hostname 
'udp://127.0.0.1';
$port '31337';

echo 
"Open a server socket<br>";
$server stream_socket_server($hostname ':' 
$port$errno$errstrSTREAM_SERVER_BIND);

echo 
"<br><br>Calling fsockopen():<br>";
$client fsockopen($hostname$port);
var_dump($client);

echo 
"<br><br>Pass some data between the sockets:<br>";
fwrite($client"0123456789");
var_dump(fread($server10));
fclose($client);

echo 
"<br><br>Calling fsockopen() with 
              address and port in same string:<br>"
;
$address $hostname ':' $port;
$second_client fsockopen($address);
var_dump($second_client);

echo 
"<br><br>Pass some data between the sockets:<br>";
fwrite($second_client"0123456789");
var_dump(fread($server10));
fclose($second_client);

?>

  4 EXERCISE   

<?php

// error condition

echo "Attempting to connect to a non-existent socket<br>";
$hostname 'tcp://127.0.0.1'
// loopback address
$port 31337;
$errno null;
$errstr null;
$timeout 1.5;
var_dumpfsockopen($hostname$port$errno$errstr$timeout) );
var_dump($errstr);

echo 
"<br><br>Attempting to connect using an invalid protocol<br>";
$hostname 'invalid://127.0.0.1'
// loopback address
$port 31337;
$errno null;
$errstr null;
$timeout 1.5;
var_dumpfsockopen($hostname$port$errno$errstr$timeout) );
var_dump($errstr);

?>