Home >Backend Development >PHP Tutorial >PHP socket communication (tcp/udp) example analysis, socketudp_PHP tutorial

PHP socket communication (tcp/udp) example analysis, socketudp_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:59:261065browse

php socket communication (tcp/udp) example analysis, socketudp

This article describes the php socket communication (tcp/udp) method through examples. Share it with everyone for your reference, the details are as follows:

Attention

1. When using 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 &

1: udp method

1) server.php

<&#63;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, '202.85.218.133', 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 );
}
&#63;>

2) client.php

<&#63;php
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$msg = 'hello';
$len = strlen($msg);
socket_sendto($sock, $msg, $len, 0, '202.85.218.133', 11109);
socket_close($sock);
&#63;>

Two: TCP method

1)server.php

<&#63;php
//error_reporting( E_ALL );
set_time_limit( 0 );
ob_implicit_flush();
$socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
socket_bind( $socket, '192.168.2.143', 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)
&#63;>

2) client.php

<&#63;php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$con=socket_connect($socket,'192.168.2.143',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);
&#63;>

Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP socket usage", "Introduction to PHP basic syntax tutorial", "Summary of PHP error and exception handling methods" and "PHP common functions and techniques" Summary》

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

Articles you may be interested in:

  • How PHP uses socket to send HTTP request
  • How to use PHP to implement Socket server
  • php uses socket to send HTTP Request (GET, POST)
  • Introduction guide to PHP’s Socket network programming
  • Detailed explanation of PHP’s socket communication
  • PHP’s custom class fsocket simulates the method of post or get request
  • How to implement chat room in php html5 based on websocket
  • UDP communication example of PHP’s Socket communication
  • How to use socket post data to other web servers in php
  • Detailed explanation of PHP SOCKET programming
  • Summary of a series of socket functions in php

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1099052.htmlTechArticlephp socket communication (tcp/udp) example analysis, socketudp This article tells the example of php socket communication (tcp/udp) method. Share it with everyone for your reference, the details are as follows: Note 1. When using socket_bind...
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