Petr Faltus development

PHP network communication over TCP socket

Petr Faltus development

Example source code in PHP how to communicate in IPv4 over network TCP socket. The sender and the receiver.

net_tcp_receiver.php

GitHub repository: network-socket-communication-source-codes

git clone https://github.com/petrfaltus/network-socket-communication-source-codes.git

Directories to net_tcp_receiver.php in the repository: php/src

<?php

set_time_limit(0);

const RECEIVER_ADDRESS = "0.0.0.0";
const RECEIVER_PORT = 10000;
const RECEIVED_MESSAGES_MAX = 10;
const BUFFER_SIZE = 4096;

echo "TCP IPv4 stream socket receiver".PHP_EOL;

$socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false)
{
  exit("- socket_create() failed: ".socket_strerror(socket_last_error()).PHP_EOL);
}
echo "- socket created".PHP_EOL;

$bound = @socket_bind($socket, RECEIVER_ADDRESS, RECEIVER_PORT);
if ($bound === false)
{
  exit("- socket_bind() failed: ".socket_strerror(socket_last_error($socket)).PHP_EOL); 
}
echo "- socket bound on ".RECEIVER_ADDRESS.":".RECEIVER_PORT.PHP_EOL;

$listening = @socket_listen($socket, RECEIVED_MESSAGES_MAX);
if ($listening === false)
{
  exit("- socket_listen() failed: ".socket_strerror(socket_last_error($socket)).PHP_EOL); 
}
echo "- socket is listening for max ".RECEIVED_MESSAGES_MAX." messages".PHP_EOL;

$stop = false;
while ($stop == false)
{
  $msgsock = @socket_accept($socket);
  if ($msgsock === false)
  {
    exit("- socket_accept() failed: ".socket_strerror(socket_last_error($socket)).PHP_EOL);
  }
  echo "- socket accepted request".PHP_EOL;

  $peer_address = "";
  $peer_port = 0;

  $peer = @socket_getpeername($msgsock, $peer_address, $peer_port);
  if ($peer === false)
  {
    exit("- socket_getpeername() failed: ".socket_strerror(socket_last_error($msgsock)).PHP_EOL);
  }

  $local_address = "";
  $local_port = 0;

  $local = @socket_getsockname($msgsock, $local_address, $local_port);
  if ($local === false)
  {
    exit("- socket_getsockname() failed: ".socket_strerror(socket_last_error($msgsock)).PHP_EOL);
  }
  echo "- peer connect from ".$peer_address.":".$peer_port." on ".$local_address.":".$local_port.PHP_EOL;

  $msg = "";

  $received_length = @socket_recv($msgsock, $msg, BUFFER_SIZE, 0);
  if ($received_length === false)
  {
    exit("- socket_recv() failed: ".socket_strerror(socket_last_error($msgsock)).PHP_EOL);
  }
  echo "- message ".$received_length."B received".PHP_EOL;
  echo "|".$msg."|".PHP_EOL;
      
  if ($msg == "stop")
  {
    // received message "stop" to stop the receiver
    $stop = true;
  }
   
  socket_close($msgsock); 
  echo "- socket closed request".PHP_EOL;
}

socket_close($socket);
echo "- socket closed".PHP_EOL;

?>

net_tcp_sender.php

Directories to net_tcp_sender.php in the repository: php/src

<?php

set_time_limit(0);

const RECEIVER_ADDRESS = "127.0.0.1";
const RECEIVER_PORT = 10000;

echo "TCP IPv4 stream socket sender".PHP_EOL;

$socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false)
{
  exit("- socket_create() failed: ".socket_strerror(socket_last_error()).PHP_EOL);
}
echo "- socket created".PHP_EOL;

$result = @socket_connect($socket, RECEIVER_ADDRESS, RECEIVER_PORT);
if ($result === false)
{
  exit("- socket_connect() failed: ".socket_strerror(socket_last_error($socket)).PHP_EOL);
}
echo "- connected to ".RECEIVER_ADDRESS.":".RECEIVER_PORT.PHP_EOL;

$peer_address = "";
$peer_port = 0;

$peer = @socket_getpeername($socket, $peer_address, $peer_port);
if ($peer === false)
{
  exit("- socket_getpeername() failed: ".socket_strerror(socket_last_error($msgsock)).PHP_EOL);
}

$local_address = "";
$local_port = 0;

$local = @socket_getsockname($socket, $local_address, $local_port);
if ($local === false)
{
  exit("- socket_getsockname() failed: ".socket_strerror(socket_last_error($msgsock)).PHP_EOL);
}
echo "- for ".$peer_address.":".$peer_port." bound on ".$local_address.":".$local_port.PHP_EOL;

if (isset($argv[1]))
{
  $msg = $argv[1]; // message is the first parameter, for example "stop" to stop the receiver
}
else
{
  $msg = "This is PHP message sent at ".date("j.n.Y")." in ".date("G:i:s");
}
echo "|".$msg."|".PHP_EOL;

$msg_length = strlen($msg); 
$sent_length = @socket_send($socket, $msg, $msg_length, 0);
if ($sent_length === false)
{
  exit("- socket_send() failed: ".socket_strerror(socket_last_error($socket)).PHP_EOL);
}
echo "- message ".$sent_length."B of ".$msg_length."B sent".PHP_EOL;

socket_close($socket);
echo "- socket closed".PHP_EOL;

?>

  Previous example All examples Following example  

  

🤝 Your IP address is 3.238.98.39


💻💽💾 Useful examples and tools for home and professional programming.


© Petr Faltus - 2021