Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of UDP Communication of Socket Communication in PHP

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report