PHP network communication over UDP socket

Petr Faltus development

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

net_udp_receiver.php

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

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

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

<?php

set_time_limit(0);

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

echo "UDP IPv4 datagram socket receiver".PHP_EOL;

$socket = @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
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;

$stop = false;
while ($stop == false)
{
  $msg = "";
  $peer_address = "";
  $peer_port = 0;

  $received_length = @socket_recvfrom($socket, $msg, BUFFER_SIZE, 0, $peer_address, $peer_port);
  if ($received_length === false)
  {
    exit("- socket_recvfrom() failed: ".socket_strerror(socket_last_error($socket)).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 "- message ".$received_length."B received from ".$peer_address.":".$peer_port." on port ".$local_port.PHP_EOL;
  echo "|".$msg."|".PHP_EOL;
      
  if ($msg == "stop")
  {
    // received message "stop" to stop the receiver
    $stop = true;
  }
}

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

?>

net_udp_sender.php

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

<?php

set_time_limit(0);

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

echo "UDP IPv4 datagram socket sender".PHP_EOL;

$socket = @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($socket === false)
{
  exit("- socket_create() failed: ".socket_strerror(socket_last_error()).PHP_EOL);
}
echo "- socket created".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); 
socket_sendto($socket, $msg, $msg_length, 0, RECEIVER_ADDRESS, RECEIVER_PORT);

$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 "- message ".$msg_length."B sent to ".RECEIVER_ADDRESS.":".RECEIVER_PORT." on port ".$local_port.PHP_EOL;

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

?>

Development tools

Developer ASCII table
Characters and HTML entities in the UTF-8 table
 
Predefined web CSS colors
CSS px to rem converter

 

  

🤝 Your IP address is 3.147.89.50 (ec2-3-147-89-50.us-east-2.compute.amazonaws.com)