Socket.st
changeset 4764 358345478edc
parent 4738 f915f985a54c
child 4765 62ddeead622b
--- a/Socket.st	Mon Nov 12 14:48:54 2018 +0100
+++ b/Socket.st	Mon Nov 12 16:24:46 2018 +0100
@@ -1424,6 +1424,22 @@
 
 !Socket class methodsFor:'queries'!
 
+getRandomAvailablePortOnHost:aHostnameOrAdress
+"Attempts binding to port 0 on a specified host, thereby getting assigned a random available port which we return. We then release the socket."
+|socket freePort hostAdress|
+aHostnameOrAdress = nil ifTrue: [ hostAdress := '0.0.0.0' ] ifFalse: [ hostAdress := aHostnameOrAdress ].
+socket := Socket newTCP.
+socket bindTo:0 address:hostAdress reuseAddress:true.
+    freePort := socket port.
+socket abortAndClose.
+^freePort                          
+
+
+
+
+
+!
+
 peerFromDomain:domain name:peerName port:port
     |addrClass|