Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of UDP Communication of Socket Communication in PHP

Shulou Source: shulou.com Published: 2022-06-03 19:41:26 10月02日 Update

This article will explain in detail the example analysis of Socket communication in PHP. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

The details are 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)");} do {/ / receive a message from the client $inMsg = stream_socket_recvfrom ($socket, 1024, 0, $peer) / / the server prints out relevant information echo "Client: $peer\ n"; echo "Receive: {$inMsg}"; / / sends a message to the 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)

two。 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; on "sample Analysis of UDP Communications in Socket Communications in PHP" this article ends here. I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Communication information client client article service example analysis more server good practical content article message knowledge symbol Jianyi reference help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi macOS MariaDB Linux Redmi