Swift Linux套接字错误:无法为类型'UnsafePointer <sockaddr>'

时间:2018-07-17 20:47:57

标签: swift linux sockets glibc

我在Linux上学习Swift,并使用glibc Sockets。我试图绑定套接字并接收

error: cannot invoke initializer for type 'UnsafePointer<sockaddr>' with an argument list of type '(sockaddr_in)' print(bind(sock, UnsafePointer<sockaddr>(sockaddrIn), MemoryLayout<sockaddr_in>.size))

我了解我通过sockaddr_in而不是sockaddr传递的类型,但是我如何正确传递sockaddr呢?这是代码:

    let sock: Int32 = socket(AF_INET, SOCK_STREAM, 0)

    if (sock == -1)
    {
        print("Could not create socket.")
        return false
    }

    print("Socket successfully created.")

    var sockaddrIn = sockaddr_in(
        sin_family: sa_family_t(AF_INET),
        sin_port: in_port_t(8080),
        sin_addr: in_addr(s_addr: INADDR_ANY),
        sin_zero: (0,0,0,0,0,0,0,0)
    )

    print(bind(sock, UnsafePointer<sockaddr>(sockaddrIn), MemoryLayout<sockaddr_in>.size))

0 个答案:

没有答案