Home  >  Article  >  Backend Development  >  Detailed explanation of socket-to-server communication examples in PHP

Detailed explanation of socket-to-server communication examples in PHP

墨辰丷
墨辰丷Original
2018-05-26 15:26:091198browse

This article mainly introduces a detailed example of client-to-server communication using PHP sockets. It has certain reference value and interested friends can refer to it.

1. server.php server:

##

<?php 
error_reporting(E_ALL); 
set_time_limit(0); 
ob_implicit_flush(); 
//本地IP 
$address = &#39;localhost&#39;; 
//设置用111端口进行通信 
$port = 111; 
//创建SOCKET 
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) { 
    echo "socket创建失败原因 " . socket_strerror($sock) . "\n"; 
} 
 
if (($ret = socket_bind($sock, $address, $port)) < 0) { 
    echo "创建套接字失败原因 " . socket_strerror($ret) . "\n"; 
} 
//监听 
if (($ret = socket_listen($sock, 5)) < 0) { 
    echo "监听失败原因 " . socket_strerror($ret) . "\n"; 
} 
do { 
  //接收命令  
  if (($msgsock = @socket_accept($sock)) < 0) { 
    echo "命令接收失败原因: " . socket_strerror($msgsock) . "\n"; 
    break; 
  } 
  $msg = "\nPHP Test Server. \n" ."用quit,shutdown,sun...等命令测试.\n"; 
 
  @socket_write($msgsock, $msg, strlen($msg)); 
 
  do { 
    if (false === ($buf = @socket_read($msgsock, 2048, PHP_NORMAL_READ))) { 
        echo "socket_read() failed: reason: " . socket_strerror($ret) . "\n"; 
        break 2; 
    } 
    if (!$buf = trim($buf)) { 
        continue; 
    } 
    if ($buf == &#39;quit&#39;) { 
        break; 
    } 
    if ($buf == &#39;shutdown&#39;) { 
        socket_close($msgsock); 
        break 2; 
    } 
    if ($buf == &#39;sun&#39;) { 
        echo&#39;what are you doing?&#39;; 
    } 
    $talkback = "Backinformation : &#39;$buf&#39;.\n"; 
    socket_write($msgsock, $talkback, strlen($talkback)); 
    echo "$buf\n"; 
  } while (true); 
 
  socket_close($msgsock); 
 
} while (true); 
 
socket_close($sock); 
?>

2. client.php client:

<?php 
error_reporting(E_ALL); 
//端口111 
$service_port = 111; 
//本地 
$address = &#39;localhost&#39;; 
//创建 TCP/IP socket 
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
if ($socket < 0) { 
    echo "socket创建失败原因: " . socket_strerror($socket) . "\n"; 
} else { 
    echo "OK,HE HE.\n"; 
} 
$result = socket_connect($socket, $address, $service_port); 
if ($result < 0) { 
    echo "SOCKET连接失败原因: ($result) " . socket_strerror($result) . "\n"; 
} else { 
    echo "OK.\n"; 
} 
//发送命令 
$in = "HEAD / HTTP/1.1\r\n"; 
$in .= "Connection: Close\r\n\r\n"; 
$out = &#39;&#39;; 
echo "Send Command.........."; 
$in = "sun\n"; 
socket_write($socket, $in, strlen($in)); 
echo "OK.\n"; 
echo "Reading Backinformatin:\n\n"; 
while ($out = socket_read($socket, 2048)) { 
    echo $out; 
} 
echo "Close socket........"; 
socket_close($socket); 
echo "OK,He He.\n\n"; 
?>

These two pieces of code are a brief introduction to implementation using PHP Communication between the client and the server


For specific operations, use the PHP command to open the SERVER under DOS and put it in the listening state..


Open a DOS window again and use the PHP command Open CLIENT, and you will get a response from the server....


The above is the entire content of this article, I hope it will be helpful to everyone's learning.


Related recommendations:

How to operate nodejs using websocket

Native nodejs uses websocketCode sharing

PHP uses Socket to obtain the SSL certificate and public key of the website

The above is the detailed content of Detailed explanation of socket-to-server communication examples in PHP. For more information, please follow other related articles on the PHP Chinese website!

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