The UDP communication of the socket of PHP

Source: Internet
Author: User
Tags sendmsg
How does the PHP socket communicate with UDP? This paper mainly introduces the UDP communication method of socket communication in PHP, and analyzes the relevant skills of PHP based on socket for UDP communication. We hope to help you.

This paper describes the UDP communication method of socket communication in PHP. Share to everyone for your reference. Specific as follows:

1. Create a simple UDP server


Server information $server = ' udp://127.0.0.1:9998 '; Message end symbol $msg _eof = "\ n"; $socket = Stream_socket_server ($server, $errno, $errstr, Stream_server_bind); if (! $socket) {die   ("$errstr ($errno)") and} do {   //receive the information sent by the client   $INMSG = Stream_socket_recvfrom ($socket, 1024, 0, $peer);   Service-side print out the relevant information   echo "Client: $peer \ n";   echo "Receive: {$INMSG}";   Send message to client   $OUTMSG = substr ($INMSG, 0, (Strrpos ($INMSG, $msg _eof)). '--'. 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; 
Related Article

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.