Connect to local socket

Viewed 689

I've been trying to create a connection to a local socket file, but errno always returns -1

var socketPath = "/path/to/socket"

let sockDesc = socket(AF_LOCAL, SOCK_STREAM, 0)

var address =  sockaddr_un()
address.sun_family = UInt8(AF_LOCAL)
address.sun_len = UInt8(bitPattern:Int8(MemoryLayout<sockaddr_un>.size))

let dataPtr = UnsafeMutablePointer<Int8>(&address.sun_path.0)
strncpy(dataPtr, socketPath, socketPath.characters.count)

let result = withUnsafePointer(to: &address) {
    $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {

        connect(sockDesc, $0, socklen_t(MemoryLayout<sockaddr>.stride));
    }
}

The socket is valid, as another client, developed in Qt can happily connect to this path.

Is there an obvious mistake here, or should this work?

1 Answers
Related