Home > Backend Development > PHP Tutorial > Develop C/S structure with PHP_PHP tutorial

Develop C/S structure with PHP_PHP tutorial

WBOY
Release: 2016-07-15 13:22:17
Original
815 people have browsed it

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!n";

else

echo "socket_create() failed:".socket_strerror ($socket)." n";

$bind = socket_bind ($socket, $ip, $port); // Bind a SOCKET

if ($bind)

echo "socket_bind () successful!n";

else

echo "socket_bind() failed:".socket_strerror ($bind)."n";

$listen = socket_listen ( $socket); // Listen to SOCKET

if ($listen)

echo "socket_listen() successful!n";

else

echo "socket_listen() failed:".socket_strerror ($listen)."n";

while (true)

{

$msg = socket_accept ($socket); //Accept a SOCKET

if (!$msg)

{

echo "socket_accept() failed:".socket_strerror ($msg)."n";

break;

}

$welcome = "Welcome to PHP Server!n";

socket_write ($msg, $welcome, strlen ($welcome ));

while (true)

{

$command = strtoupper (trim (socket_read ($msg, 1024)));

if (!$command)

break;

switch ($command)

{

case "HELLO":

$writer = "Hello Everybody!";

break;

case "QUIT":

$writer = "Bye-Bye";

break;

case "HELP":

$writer = "HELLOtQUITtHELP";

break;

default:

$writer = "Error Command!";

}

socket_write ($msg, $writer, strlen ($writer));

if ($command == "QUIT")

break;

}

socket_close ($msg);

}

socket_close ($socket); // Close SOCKET

?>

Client

// Client

// Set error handling

error_reporting (E_ALL);

//Set processing time

set_time_limit (0);

$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!n";

else

echo "socket_create() failed:".socket_strerror ($socket)."n";

$conn = socket_connect ($socket, $ip, $port); // Establish a SOCKET connection

if ($conn)

echo "Success to connection![".$ip. ":".$port."]n";

else

echo "socket_connect() failed:".socket_strerror ($conn)."n";

echo socket_read ($socket, 1024);

$stdin = fopen (´php://stdin´, ´r´);

while (true)

{

$command = trim (fgets ($stdin, 1024));

socket_write ($socket, $command, strlen ($command));

$msg = trim (socket_read ($socket, 1024));

echo $msg."n";

if ($msg == "Bye-Bye")

break ;

}

fclose ($stdin);

socket_close ($socket);

?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446990.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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template