-
-
Save crawc/766b5a75a31cfd3dbd6fe27b4ec04a12 to your computer and use it in GitHub Desktop.
An one-off PHP UDP server to listen on a UDP port and print the text received. Can be used as a very basic UDP log listener.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/php -f | |
| <?php | |
| 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" , 4444) ) { | |
| $errorcode = socket_last_error(); | |
| $errormsg = socket_strerror($errorcode); | |
| die("Could not bind socket : [$errorcode] $errormsg \n"); | |
| } | |
| echo "Socket bind OK \n"; | |
| //Receive some data | |
| while(1) { | |
| $r = socket_recvfrom($sock, $buf, 5120, 0, $remote_ip, $remote_port); | |
| echo "$remote_ip : $remote_port -- " . $buf; | |
| } | |
| socket_close($sock); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment