日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

UDP socket programming in php

發(fā)布時間:2025/3/20 php 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UDP socket programming in php 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

http://www.binarytides.com/udp-socket-programming-in-php/

UDP socket programming in php

In a previous?article?we learnt about writing simple server and clients using TCP sockets in php. In this article we are going to use udp sockets for the same.

UDP sockets are much simpler to work with since they are connection-less. A udp server just has an socket that waits to receive some data and a socket client can send data on a socket without connection.

Server

So lets code up the server.

<?php//Reduce errors error_reporting(~E_WARNING);//Create a UDP socket if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0))) {$errorcode = socket_last_error();$errormsg = socket_strerror($errorcode);die("Couldn't create socket: [$errorcode] $errormsg \n"); }echo "Socket created \n";// Bind the source address if( !socket_bind($sock, "0.0.0.0" , 9999) ) {$errorcode = socket_last_error();$errormsg = socket_strerror($errorcode);die("Could not bind socket : [$errorcode] $errormsg \n"); }echo "Socket bind OK \n";//Do some communication, this loop can handle multiple clients while(1) {echo "Waiting for data ... \n";//Receive some data$r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);echo "$remote_ip : $remote_port -- " . $buf;//Send back the data to the clientsocket_sendto($sock, "OK " . $buf , 100 , 0 , $remote_ip , $remote_port); }socket_close($sock);

The above program opens a udp server on port 9999 of localhost.

Run the above program from the terminal and it should show

$ php server.php Socket created Socket bind OK Waiting for data ...

This udp server can handle multiple clients since it does not use a hardbound connection and simply replies to whoever came in.

Client

Now that our server is running fine, its time to code a client program that would connect to the server and communicate.

<?php/*Simple php udp socket client *///Reduce errors error_reporting(~E_WARNING);$server = '127.0.0.1'; $port = 9999;if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0))) {$errorcode = socket_last_error();$errormsg = socket_strerror($errorcode);die("Couldn't create socket: [$errorcode] $errormsg \n"); }echo "Socket created \n";//Communication loop while(1) {//Take some input to sendecho 'Enter a message to send : ';$input = fgets(STDIN);//Send the message to the serverif( ! socket_sendto($sock, $input , strlen($input) , 0 , $server , $port)){$errorcode = socket_last_error();$errormsg = socket_strerror($errorcode);die("Could not send data: [$errorcode] $errormsg \n");}//Now receive reply from server and print itif(socket_recv ( $sock , $reply , 2045 , MSG_WAITALL ) === FALSE){$errorcode = socket_last_error();$errormsg = socket_strerror($errorcode);die("Could not receive data: [$errorcode] $errormsg \n");}echo "Reply : $reply"; }

The above client asks user to input some message which is send to the server. Then whatever reply the server sends back is printed.

The output shall be like this

$ php client.php Socket created Enter a message to send : Hello Reply : OK Hello Enter a message to send : World Reply : OK World Enter a message to send :

Conclusion

So in the above demonstration we saw how udp sockets can be used in php. UDP sockets are used in those type of network communication where reliability is not very important due to a number of reasons.

The above shown examples are very basic and would need a lot of modification to make ready for suitable use. So try them out.

轉(zhuǎn)載于:https://my.oschina.net/yisenn/blog/85337

總結(jié)

以上是生活随笔為你收集整理的UDP socket programming in php的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。