File tree 1 file changed +19
-0
lines changed 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -46,6 +46,25 @@ func GetFreePort() (Port, error) {
46
46
return Port (l .Addr ().(* net.TCPAddr ).Port ), nil
47
47
}
48
48
49
+ // GetNextFreePort - returns the immediate next port if it's available. The maximum allowed input is 65534, since we increment the value by 1.
50
+ func GetNextFreePort (port string ) (Port , error ) {
51
+ if port == "" || port == "0" {
52
+ return 0 , errors .New ("invalid starting port" )
53
+ }
54
+ p , err := strconv .Atoi (port )
55
+ if err != nil || p <= 0 || p >= 65535 {
56
+ return 0 , errors .New ("invalid port number (must be between 1 and 65534)" )
57
+ }
58
+ nextPort := p + 1
59
+ addr := & net.TCPAddr {IP : net .ParseIP ("127.0.0.1" ), Port : nextPort }
60
+ l , err := net .ListenTCP ("tcp" , addr )
61
+ if err != nil {
62
+ return 0 , err
63
+ }
64
+ defer l .Close ()
65
+ return Port (l .Addr ().(* net.TCPAddr ).Port ), nil
66
+ }
67
+
49
68
// ParsePort - parses string into Port
50
69
func ParsePort (s string ) (p Port , err error ) {
51
70
switch s {
You can’t perform that action at this time.
0 commit comments