Home >Backend Development >PHP Tutorial >Develop C/S structure with PHP (a simple example)_PHP tutorial

Develop C/S structure with PHP (a simple example)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 11:00:181035browse

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)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631815.htmlTechArticleServer?php // 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 $...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn