Phpsocket communication (tcp/udp) instance analysis, socketudp_PHP tutorial

Source: Internet
Author: User
Tags usleep
Phpsocket communication (tcpudp) instance analysis, socketudp. Phpsocket communication (tcpudp) instance analysis, socketudp this article describes the phpsocket communication (tcpudp) method. For your reference, the details are as follows: 1. Analysis of php socket communication (tcp/udp) instances during socket_bind, socketudp

This example describes the php socket communication (tcp/udp) method. We will share this with you for your reference. The details are as follows:

Note:

1. when socket_bind is used, the IP address cannot be a real loopback address, for example, 127.0.0.1.
2. nohup php server. php>/var/tmp/a. log 2> & 1 &

I. udp

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, '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 );}?>

2) client. php

<?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);?>

II. TCP mode

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, '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)?>

2) client. php

<?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);?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.