utils.go 336 B

123456789101112131415161718
  1. package webserv
  2. import (
  3. "net"
  4. )
  5. // IsPortInUse checks if a port is in use.
  6. func IsPortInUse(port string) bool {
  7. listener, err := net.Listen("tcp", "localhost:"+port)
  8. if err != nil {
  9. // If there was an error, the port is in use.
  10. return true
  11. }
  12. defer listener.Close()
  13. // No error means the port is available.
  14. return false
  15. }