UDP Sockets 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.
UDP 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 client socket_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.
UDP 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 send echo 'Enter a message to send : '; $input = fgets(STDIN); //Send the message to the server if( ! 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 it if(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.
To learn about the basics of socket programming Php check out this post:
PHP Socket programming Tutorial - How to code Client and Server
Tengo error:
No se podía recibir datos: [10045] El intento de operación no es compatible con el tipo de objeto referenciado.
I got error:
Could not receive data: [10045] The attempted operation is not supported for the type of object referenced.
Thanks very much.
I find this a long time T____T
Let me rephrase the question. How do I make this example work if I have a server with static IP.
How do I make this work with and Server who has a static IP
Address
I had a problem using this socket example on a windows env. I cannot receive message back from the server, it was stuck on error
die(“Could not receive data: [$errorcode] $errormsg n”);
To solve that, in client, on socket_recv use MSG_PEEK instead MSG_WAIT_ALL
thank you so much from italy! ;)
Amazing code, thanks a lot!!!
How to run this code? in browser or in cmd? It causes error.
“Socket created
Enter a message to send :
Notice: Use of undefined constant STDIN – assumed ‘STDIN’ in C:xampphtdocstcpindex.php on line 28
Could not send data: [10061] No connection could be made because the target machine actively refused it.”
Please guide me. I’m new to PHP and also new to socket programming.
Thanks.
https://github.com/swoole/swoole-src
Asynchronous & concurrent & distributed networking framework for PHP.
Thank u : ),
how can we send a packet that includes different types of data over udp ?
Clear and straight to the point.good job
Great code helped a lot – had trouble rifling through the php documentation.
really nice
Thank you very much, really helped me, greetings from Paraguay