PHP Socket communication-UDP communication instance, phpsocketudp instance _ PHP Tutorial

Source: Internet
Author: User
Tags sendmsg
PHP Socket communication-UDP communication instance, phpsocketudp instance. PHP Socket communication-UDP communication instance, phpsocketudp instance this article describes the PHP Socket communication-UDP communication method. Share it with you for your reference. Details: 1. create a simple PHP Socket communication UDP communication instance, phpsocketudp instance

This article describes the UDP communication method of PHP Socket communication. Share it with you for your reference. The details are as follows:

1. create a simple UDP server

// Server information $ server = 'udp: // 127.0.0.1: 8080'; // message end symbol $ msg_eof = "\ n"; $ socket = stream_socket_server ($ server, $ errno, $ errstr, STREAM_SERVER_BIND); if (! $ Socket) {die ("$ errstr ($ errno)");} do {// receive information sent from the client $ inMsg = stream_socket_recvfrom ($ socket, 1024, 0, $ peer); // The server prints the relevant information echo "Client: $ peer \ n"; echo "Receive: {$ inMsg }"; // send the message $ outMsg = substr ($ inMsg, 0, (strrpos ($ inMsg, $ msg_eof) to the client ))). '--'. date ("d m j H: I: s Y \ r \ n"); stream_socket_sendto ($ socket, $ outMsg, 0, $ peer);} while ($ inMsg! = False );

2. simple client

function udpGet($sendMsg = '', $ip = '127.0.0.1', $port = '9998'){   $handle = stream_socket_client("udp://{$ip}:{$port}", $errno, $errstr);   if( !$handle ){     die("ERROR: {$errno} - {$errstr}\n");   }   fwrite($handle, $sendMsg."\n");   $result = fread($handle, 1024);   fclose($handle);   return $result; } $result = udpGet('Hello World'); echo $result;

I hope this article will help you with php programming.

Listen This article describes the UDP communication method of PHP Socket communication. Share it with you for your reference. Details: 1. create a simple project...

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.