Skip to content

Instantly share code, notes, and snippets.

@mathroc
Created September 17, 2014 13:54
Show Gist options
  • Save mathroc/7ab55a88162ac59cd299 to your computer and use it in GitHub Desktop.
Save mathroc/7ab55a88162ac59cd299 to your computer and use it in GitHub Desktop.
Find unused port (cli - php)
#!/usr/bin/php
<?php
$address = '127.0.0.1';
// Create a new socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// Bind the source address
socket_bind($sock, $address);
socket_getsockname($sock, $address, $port);
echo $port;
socket_close($sock);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment