Created
July 2, 2020 18:06
-
-
Save tiebingzhang/c852f53cf06c29cb18a35bf8897f015c 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 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