Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2 check if port is busy

can someone tell me how to find out in Swift2 if Port is busy?

Because I write a mac app that has a self written Tcp server, but sometimes it wont start up because it "cant bind to port". So how can I check if the port is not used, to block the start button of the Tcp server until the port is free, again?

And I do not want a new framework.

Thanks

like image 642
Michael_mhr Avatar asked Oct 12 '15 16:10

Michael_mhr


1 Answers

mainly code taken from Swifter: https://github.com/glock45/swifter

func checkTcpPortForListen(port: in_port_t) -> (Bool, descr: String){

    let socketFileDescriptor = socket(AF_INET, SOCK_STREAM, 0)
    if socketFileDescriptor == -1 {
        return (false, "SocketCreationFailed, \(descriptionOfLastError())")
    }

    var addr = sockaddr_in()
      addr.sin_len = __uint8_t(sizeof(sockaddr_in))
      addr.sin_family = sa_family_t(AF_INET)
      addr.sin_port = Int(OSHostByteOrder()) == OSLittleEndian ? _OSSwapInt16(port) : port
      addr.sin_addr = in_addr(s_addr: inet_addr("0.0.0.0"))
      addr.sin_zero = (0, 0, 0, 0, 0, 0, 0, 0)
    var bind_addr = sockaddr()
    memcpy(&bind_addr, &addr, Int(sizeof(sockaddr_in)))

    if bind(socketFileDescriptor, &bind_addr, socklen_t(sizeof(sockaddr_in))) == -1 {
        let details = descriptionOfLastError()
        release(socketFileDescriptor)
        return (false, "\(port), BindFailed, \(details)")
    }
    if listen(socketFileDescriptor, SOMAXCONN ) == -1 {
        let details = descriptionOfLastError()
        release(socketFileDescriptor)
        return (false, "\(port), ListenFailed, \(details)")
    }
    release(socketFileDescriptor)
    return (true, "\(port) is free for use") 
}

func release(socket: Int32) {
    Darwin.shutdown(socket, SHUT_RDWR)
    close(socket)
}
func descriptionOfLastError() -> String {
    return String.fromCString(UnsafePointer(strerror(errno))) ?? "Error: \(errno)"
}
like image 156
dattk Avatar answered Oct 03 '22 02:10

dattk