Home >Backend Development >PHP Tutorial >Develop C/S structure with PHP (a simple example)_PHP tutorial
Server
// Server
// Set error handling
error_reporting (E_ALL);
// Set running time
set_time_limit (0);
/ / Enable buffering
ob_implicit_flush ();
$ip = "127.0.0.1"; // IP address
$port = 1000; // Port number
$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // Create a SOCKET
if ($socket)
echo "socket_create() successful!
";
else
echo "socket_create() failed:".socket_strerror ($socket)."
";
$bind = socket_bind ($socket, $ip, $port); // Bind a SOCKET
if ($bind)
echo "socket_bind() successful!
";
else
echo "socket_bind() failed:".socket_strerror ($bind)."
";
$listen = socket_listen ($socket); // Listen to SOCKET
if ($listen)
echo "socket_listen() successful!
";
else
echo "socket_listen() failed:".socket_strerror ($listen)."
";
while (true)
{
$msg = socket_accept ($socket); // Accept a SOCKET
if (!$msg)
{
echo "socket_accept( ) failed: ".socket_strerror ($msg)."
";
break;
}
$welcome = "Welcome to PHP Server!
";
socket_write ($msg, $welcome, strlen ($welcome));
while (true)