1.Server端:能接收client傳來的訊息並寫入檔案且不傳訊息給client
2.Client端:不接收server傳來的訊息
Server端IP:192.168.32.102 Port:25001
檔名:ServerSocket.php ,檔案所在位置為 /home/webadmin/ServerSocket.php
<?php
$server_ip = '192.168.32.102';
$port = 25001;
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_bind($socket,$server_ip,$port);
socket_listen($socket,SOMAXCONN);
$count = 0;
do {
$msgsocket = socket_accept($socket);
if ($msgsocket < 0) {
echo "socket_accept() fail : ".socket_strerror(socket_last_error())."\n";
break;
} else {
$datetime = date('Y-m-d H:i:s',time());
$str = "Server datetime:{$datetime}\n";
$buff = socket_read($msgsocket,2048);
$str .= "Receive client message: {$buff}"." count: ".$count."\n\n";
echo $str;
$str01 = $buff." ".$datetime."\n";
if ($str01!="") {
$file = fopen("/home/webadmin/html/ServerSocket", "a+");
fwrite($file, $str01);
fclose($file);
}
}
$count++;
socket_close($msgsocket);
} while ($count <= 5);
socket_close($socket);
?>
執行 ServerSocket.php 的指令
$php ServerSocket.php
$php ServerSocket.php
Client端:IP:192.168.32.182
檔名:client.php
傳送$in 的內容給 Server 端
<?php
$server_ip = '192.168.32.102';
$port = 25001;
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_connect($socket,$server_ip,$port);
$in = "\$\$H167,864292040653451,AAA,35\n";
socket_write($socket,$in,strlen($in));
socket_close($socket);
1.php socket 初步實作Server 與 Client
2.[PHP]開啟及寫入檔案
沒有留言:
張貼留言