Home > php教程 > PHP开发 > PHP socket communication (tcp/udp) example analysis

PHP socket communication (tcp/udp) example analysis

高洛峰
Release: 2016-12-22 09:12:52
Original
1373 people have browsed it

The example in this article describes the php socket communication (tcp/udp) method. Share it with everyone for your reference, the details are as follows:

Note

1. When socket_bind, the IP address cannot be a real loopback address such as 127.0.0.1
2. When server.php is running in the background, nohup php server.php > / var/tmp/a.log 2>&1 &

One: udp method

1) server.php

<?php
//error_reporting( E_ALL );
set_time_limit( 0 );
ob_implicit_flush();
$socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
if ( $socket === false ) {
  echo "socket_create() failed:reason:" . socket_strerror( socket_last_error() ) . "\n";
}
$ok = socket_bind( $socket, &#39;202.85.218.133&#39;, 11109 );
if ( $ok === false ) {
  echo "socket_bind() failed:reason:" . socket_strerror( socket_last_error( $socket ) );
}
while ( true ) {
  $from = "";
  $port = 0;
  socket_recvfrom( $socket, $buf,1024, 0, $from, $port );
  echo $buf;
  usleep( 1000 );
}
?>
Copy after login

2) client.php

<?php
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$msg = &#39;hello&#39;;
$len = strlen($msg);
socket_sendto($sock, $msg, $len, 0, &#39;202.85.218.133&#39;, 11109);
socket_close($sock);
?>
Copy after login

Two: TCP method

1 )server.php

<?php
//error_reporting( E_ALL );
set_time_limit( 0 );
ob_implicit_flush();
$socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
socket_bind( $socket, &#39;192.168.2.143&#39;, 11109 );
socket_listen($socket);
$acpt=socket_accept($socket);
echo "Acpt!\n";
while ( $acpt ) {
  $words=fgets(STDIN);
  socket_write($acpt,$words);
  $hear=socket_read($acpt,1024);
  echo $hear;
  if("bye\r\n"==$hear){
    socket_shutdown($acpt);
    break;
  }
  usleep( 1000 );
}
socket_close($socket)
?>
Copy after login

2) client.php

<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$con=socket_connect($socket,&#39;192.168.2.143&#39;,11109);
if(!$con){socket_close($socket);exit;}
echo "Link\n";
while($con){
    $hear=socket_read($socket,1024);
    echo $hear;
    $words=fgets(STDIN);
    socket_write($socket,$words);
    if($words=="bye\r\n"){break;}
}
socket_shutdown($socket);
socket_close($sock);
?>
Copy after login

I hope this article will be helpful to everyone in PHP programming.

For more articles related to PHP socket communication (tcp/udp) example analysis, please pay attention to the PHP Chinese website!


Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template