gpt4 book ai didi

swift-nio - SwiftNIO : Send and receive UDP broadcast

转载 作者:行者123 更新时间:2023-12-03 22:03:07 40 4
gpt4 key购买 nike

我正在尝试使用 SwiftNIO 构建 TCP 服务器。服务器在网络中启动,但客户端不知道 IP 地址。因此,我也想启动一个 UDP 服务器,如果客户端出现,他会向网络发送一条广播消息。服务器将接收并回答,以便客户端现在知道 IP 地址。

是否可以使用 SwiftNIO 构建类似的东西?

最佳答案

是的,这也是可能的,SwiftNIO 中也没有太多支持来简化这件事。
请参阅下面的注释示例,该示例将发送 HELLO WORLD每秒一次到 en0的广播地址和端口 37020。

import NIO

let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
try! group.syncShutdownGracefully()
}

let matchingInterfaces = try System.enumerateInterfaces().filter {
// find an IPv4 interface named en0 that has a broadcast address.
$0.name == "en0" && $0.broadcastAddress != nil
}

guard let en0Interface = matchingInterfaces.first, let broadcastAddress = en0Interface.broadcastAddress else {
print("ERROR: No suitable interface found. en0 matches \(matchingInterfaces)")
exit(1)
}

// let's bind the server socket
let server = try! DatagramBootstrap(group: group)
// enable broadast
.channelOption(ChannelOptions.socket(SOL_SOCKET, SO_BROADCAST), value: 1)
.bind(to: en0Interface.address)
.wait()
print("bound to \(server.localAddress!)")

var buffer = server.allocator.buffer(capacity: 32)
buffer.writeString("HELLO WORLD!")

var destAddr = broadcastAddress
destAddr.port = 37020 // we're sending to port 37020

// now let's just send the buffer once a second.
group.next().scheduleRepeatedTask(initialDelay: .seconds(1),
delay: .seconds(1),
notifying: nil) { task in
server.writeAndFlush(AddressedEnvelope(remoteAddress: destAddr,data: buffer)).map {
print("message sent to \(destAddr)")
}.whenFailure { error in
print("ERROR: \(error)")
// and stop if there's an error.
task.cancel()
server.close(promise: nil)
}
}

try server.closeFuture.wait()
如果您想绑定(bind)到 0.0.0.0并发送至 255.255.255.255你可以用这个
import NIO

let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
try! group.syncShutdownGracefully()
}

// let's bind the server socket
let server = try! DatagramBootstrap(group: group)
// enable broadast
.channelOption(ChannelOptions.socket(SOL_SOCKET, SO_BROADCAST), value: 1)
.bind(to: .init(ipAddress: "0.0.0.0", port: 0))
.wait()
print("bound to \(server.localAddress!)")

var buffer = server.allocator.buffer(capacity: 32)
buffer.writeString("HELLO WORLD!")

// we're sending to port 37020
let destPort = 37020
let destAddress = try SocketAddress(ipAddress: "255.255.255.255", port: destPort)

// now let's just send the buffer once a second.
group.next().scheduleRepeatedTask(initialDelay: .seconds(1),
delay: .seconds(1),
notifying: nil) { task in
server.writeAndFlush(AddressedEnvelope(remoteAddress: destAddress, data: buffer)).map {
print("message sent to \(destAddress)")
}.whenFailure { error in
print("ERROR: \(error)")
// and stop if there's an error.
task.cancel()
server.close(promise: nil)
}
}
try server.closeFuture.wait()

关于swift-nio - SwiftNIO : Send and receive UDP broadcast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58548813/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com