PHP Socket

server.php

<?php
// 建立server端socket
$tcp = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $tcp);
socket_bind($socket, '127.0.0.1', 10008);       //绑定要监听的端口
socket_listen($socket);       //监听端口

//初始化一个数据,和客户端通信
$buffer = "connect";
while (true) {
    // 接受一个socket连接
    $connection = socket_accept($socket);
    if(!$connection){
        echo "connect fail";
    }else{
        echo "Socket connected\n";
        // 向客户端传递一个信息数据
        if ($buffer != "") {
            echo "send data to client\n";
            socket_write($connection, $buffer . "\n");
            echo "Wrote to socket\n";
        } else {
            echo "no data in the buffer\n" ;
        }
        // 从客户端取得信息
        while ($data = @socket_read($connection, 1024, PHP_NORMAL_READ)) {
                printf("Buffer: " . $data . "\n");
                //取得信息给客户端一个反馈
                socket_write($connection, "Information Received\n");
        }
    }

    socket_close($connection);
    //关闭 socket
    printf("Closed the socket\n");
}
?>
 

client.php

<?php
// 建立客户端的socket连接
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket, '127.0.0.1', 10008);    //连接服务器端socket

while ($buffer = @socket_read($socket, 1024, PHP_NORMAL_READ)) {
	//服务端告诉客户端,自己的状态
	if (preg_match("/not connect/",$buffer)) {
		echo "don`t connect\n";
		break;
	} else {
		//服务器传来信息
		echo "Buffer Data: " . $buffer . "\n";

		echo "Writing to Socket\n";
		// 将客户的信息写到通道中,传给服务器端
		if (!socket_write($socket, "SOME DATA\n")) {
			echo "Write failed\n";
		}
		//服务器端收到信息后,给于的回应信息
		while ($buffer = socket_read($socket, 1024, PHP_NORMAL_READ)) {
			echo "sent to server: SOME DATA\n response from server was:" . $buffer . "\n";
		}

	}
}

?>
 

你可能感兴趣的:(PHP,socket)