Thanks @eskimo
My application do support for bonjour , I got the special entitlement access for multicast usage.Can you please share the documentation or resources for how to achieve with bonjour.
Post
Replies
Boosts
Views
Activity
func findFreePort() -> UInt16 {
var port: UInt16 = 8000;
let socketFD = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if socketFD == -1 {
//print("Error creating socket: \(errno)")
return port;
}
var hints = addrinfo(
ai_flags: AI_PASSIVE,
ai_family: AF_INET,
ai_socktype: SOCK_STREAM,
ai_protocol: 0,
ai_addrlen: 0,
ai_canonname: nil,
ai_addr: nil,
ai_next: nil
);
var addressInfo: UnsafeMutablePointer<addrinfo>? = nil;
var result = getaddrinfo(nil, "0", &hints, &addressInfo);
if result != 0 {
//print("Error getting address info: \(errno)")
close(socketFD);
return port;
}
result = Darwin.bind(socketFD, addressInfo!.pointee.ai_addr, socklen_t(addressInfo!.pointee.ai_addrlen));
if result == -1 {
//print("Error binding socket to an address: \(errno)")
close(socketFD);
return port;
}
result = Darwin.listen(socketFD, 1);
if result == -1 {
//print("Error setting socket to listen: \(errno)")
close(socketFD);
return port;
}
var addr_in = sockaddr_in();
addr_in.sin_len = UInt8(MemoryLayout.size(ofValue: addr_in));
addr_in.sin_family = sa_family_t(AF_INET);
var len = socklen_t(addr_in.sin_len);
result = withUnsafeMutablePointer(to: &addr_in, {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
return Darwin.getsockname(socketFD, $0, &len);
}
});
if result == 0 {
port = addr_in.sin_port;
}
Darwin.shutdown(socketFD, SHUT_RDWR);
close(socketFD);
return port;
}
I have tried with GCDAsyncUdpSocket ( https://github.com/robbiehanson/CocoaAsyncSocket ) haven't received any callbacks.
var ip: String = "192.168.0.19"
var port: UInt16 = 137
let broadcastData: Data = "Hi".data(using: .utf8)!
var udpSocket : GCDAsyncUdpSocket!
func broadCast(){
udpSocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: .main)
do {
port = findFreePort()
try udpSocket.bind(toPort: port)
print("\nBind port \(port)")
try udpSocket.enableReusePort(true)
try udpSocket.enableBroadcast(true)
print("Enable broadcast")
try udpSocket.beginReceiving()
print("Begin receiving")
udpSocket.send(broadcastData, toHost: ip, port: port, withTimeout: -1, tag: 0)
print("Send \(String(data: broadcastData, encoding: .utf8)!) to host \(ip) on port \(port)")
} catch {
print("udp broadcast error")
}
}
func udpSocket(_ sock: GCDAsyncUdpSocket, didNotConnect error: Error?) {
print("didNotConnect")
}
func udpSocketDidClose(_ sock: GCDAsyncUdpSocket, withError error: Error?) {
print("udpSocketDidClose")
}
func udpSocket(_ sock: GCDAsyncUdpSocket, didConnectToAddress address: Data) {
print("didConnectToAddress")
}
func udpSocket(_ sock: GCDAsyncUdpSocket, didSendDataWithTag tag: Int) {
print("didSendDataWithTag")
}
func udpSocket(_ sock: GCDAsyncUdpSocket, didNotSendDataWithTag tag: Int, dueToError error: Error?) {
print("didNotSendDataWithTag")
}
func udpSocket(_ sock: GCDAsyncUdpSocket, didReceive data: Data, fromAddress address: Data, withFilterContext filterContext: Any?) {
print("didReceive data \(data)")
print("fromAddress data \(address)")
var host: NSString? = ""
var port: UInt16 = 0
GCDAsyncUdpSocket.getHost(&host, port: &port, fromAddress: address)
print("host \(host ?? "---")")
print("port \(port)")
print("address \(address)")
}