如何在swift中将sockaddr_in转换为sockaddr

时间:2022-09-11 19:09:07

I'm trying to do some network code in swift and the type checking is giving me fits.

我正试图在swift中做一些网络代码,类型检查让我适合。

var sock: CInt = ...
var rin: sockaddr_in
var rlen = socklen_t(sizeof(sockaddr_in))
var buffer: CChar[] = CChar[](count: 128, repeatedValue: 0)

let len = recvfrom(sock, &buffer, 128, 0, &rin, &rlen)

The compiler complains (very cryptically) at the recvfrom about the fact that &rin is a pointer to sockaddr_in instead of sockaddr. I tried various ways to convert the pointer type, but I can't seem to get this right.

编译器在recvfrom上抱怨(非常密码)关于&rin是指向sockaddr_in而不是sockaddr的指针。我尝试了各种方法来转换指针类型,但我似乎无法做到这一点。

If I declare it to be a sockaddr I can get this to compile, but then I can't look at it as a sockaddr_in.

如果我声明它是一个sockaddr我可以让它编译,但后来我不能把它看作sockaddr_in。

1 个解决方案

#1


5  

Update: As of Swift 1.2 (Xcode 6.3 beta), initializing a C struct has become much simpler:

更新:从Swift 1.2(Xcode 6.3 beta)开始,初始化C结构变得更加简单:

var rin = sockaddr_in()

defines a sockaddr_in variable and initializes all elements to zero. The conversion of a address of sockaddr_in to an an address of sockaddr is done as follows:

定义sockaddr_in变量并将所有元素初始化为零。将sockaddr_in的地址转换为sockaddr的地址的过程如下:

let len = withUnsafeMutablePointer(&rin) {
    recvfrom(sock, &buffer, UInt(buffer.count), 0, UnsafeMutablePointer($0), &rlen)
}

Old answer: The first problem is that the sockaddr_in variable has to be initialized before its address can be passed to recvfrom(). Initializing complex structs in Swift seems to be clumsy, compare

旧答案:第一个问题是sockaddr_in变量必须先初始化,然后才能将其地址传递给recvfrom()。比较,在Swift中初始化复杂结构似乎很笨拙

Using the helper function from https://*.com/a/24335355/1187415:

使用https://*.com/a/24335355/1187415中的帮助程序功能:

func initStruct<S>() -> S {
    let struct_pointer = UnsafeMutablePointer<S>.alloc(1)
    let struct_memory = struct_pointer.move()
    struct_pointer.dealloc(1)
    return struct_memory
}

the socket address can be initialized as

套接字地址可以初始化为

var rin : sockaddr_in = initStruct()

To cast the sockaddr_in pointer to a sockaddr pointer, use reinterpretCast().

要将sockaddr_in指针强制转换为sockaddr指针,请使用reinterpretCast()。

let len = recvfrom(sock, &buffer, UInt(buffer.count), 0, reinterpretCast(&rin), &rlen)

#1


5  

Update: As of Swift 1.2 (Xcode 6.3 beta), initializing a C struct has become much simpler:

更新:从Swift 1.2(Xcode 6.3 beta)开始,初始化C结构变得更加简单:

var rin = sockaddr_in()

defines a sockaddr_in variable and initializes all elements to zero. The conversion of a address of sockaddr_in to an an address of sockaddr is done as follows:

定义sockaddr_in变量并将所有元素初始化为零。将sockaddr_in的地址转换为sockaddr的地址的过程如下:

let len = withUnsafeMutablePointer(&rin) {
    recvfrom(sock, &buffer, UInt(buffer.count), 0, UnsafeMutablePointer($0), &rlen)
}

Old answer: The first problem is that the sockaddr_in variable has to be initialized before its address can be passed to recvfrom(). Initializing complex structs in Swift seems to be clumsy, compare

旧答案:第一个问题是sockaddr_in变量必须先初始化,然后才能将其地址传递给recvfrom()。比较,在Swift中初始化复杂结构似乎很笨拙

Using the helper function from https://*.com/a/24335355/1187415:

使用https://*.com/a/24335355/1187415中的帮助程序功能:

func initStruct<S>() -> S {
    let struct_pointer = UnsafeMutablePointer<S>.alloc(1)
    let struct_memory = struct_pointer.move()
    struct_pointer.dealloc(1)
    return struct_memory
}

the socket address can be initialized as

套接字地址可以初始化为

var rin : sockaddr_in = initStruct()

To cast the sockaddr_in pointer to a sockaddr pointer, use reinterpretCast().

要将sockaddr_in指针强制转换为sockaddr指针,请使用reinterpretCast()。

let len = recvfrom(sock, &buffer, UInt(buffer.count), 0, reinterpretCast(&rin), &rlen)