UDP socket programming in php
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ORACLE10g安装
- 下一篇: awk求和