如何用PHP开发C/S结构

2021-06-20 作者:未知   |   浏览(

$ih3 = "127.0.0.1"; // IP 地址

服务器

<?php

// Server

// 设置处置时间

set_time_limit ;

// 起用缓冲

ob_implicit_flush ;

$ih3 = "127.0.0.1"; // IP地址

// 设置运行时间

set_time_limit ;

$socket = socket_create ; // 创建一个SOCKET

if

echo "socket_create successed!n";

else

echo "socket_create failed:".socket_strerror ."n";

$h3ort = 1000; // 端口号

$listen = socket_listen ; // 间听SOCKET

if

echo "socket_listen successed!n";

else

echo "socket_listen failed:".socket_strerror ."n";

while

{

// 设置错误处置

error_reporting ;

socket_close ; // 关闭SOCKET

?>

$conn = socket_connect ; // 打造SOCKET的连接

if

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

else

echo "socket_connect failed:".socket_strerror ."n";

echo socket_read ;

$stdin = fopen ;

while

{

$command = trim );

socket_write );

$msg = trim );

echo $msg."n";

if

break;

}

fclose ;

socket_close ;

?>

$socket = socket_create ; // 创建一个SOCKET

if

echo "socket_create successed!n";

else

echo "socket_create failed:".socket_strerror ."n";

$h3ort = 1000; // 端口号

推广客户端

<?php

// Client

$msg = socket_acceh3t ; // 同意一个SOCKET

if

{

echo "socket_accept failed:".socket_strerror ."n";

break;

}

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

socket_write );

while

{

$command = strtoupper ));

if

break;

switch

{

case "HELLO":

$writer = "Hello Everybody!";

break;

case "QUIT":

$writer = "Bye-Bye";

break;

case "HELP":

$writer = "HELLOtQUITtHELP";

break;

default:

$writer = "Error Command!";

}

socket_write );

if

break;

}

socket_close ;

}

$bind = socket_bind ; // 绑定一个SOCKET

if

echo "socket_bind successed!n";

else

echo "socket_bind failed:".socket_strerror ."n";

// 设置错误处置

error_reporting ;

推广客户端

<?php

// Client

// 设置错误处置

error_reporting ;

// 设置处置时间

set_time_limit ;

$ip = "127.0.0.1"; // IP 地址

$port = 1000; // 端口号

$socket = socket_create ; // 创建一个SOCKET

if

echo "socket_create successed!n";

else

echo "socket_create failed:".socket_strerror ."n";

$conn = socket_connect ; // 打造SOCKET的连接

if

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

else

echo "socket_connect failed:".socket_strerror ."n";

echo socket_read ;

$stdin = fopen ;

while

{

$command = trim );

socket_write );

$msg = trim );

echo $msg."n";

if

break;

}

fclose ;

socket_close ;

?>