ios - Alamofire 无法处理 UDP 请求吗? NSPOSIXErrorDomain 代码=47

标签 ios swift udp alamofire

以下 Swift 代码总是返回:Error Domain=NSPOSIXErrorDomain Code=47 "The operation couldn't be completed. Address family not supported by protocol family"UserInfo=0x7fd460f5ec40 {_kCFStreamErrorCodeKey=47, _kCFStreamErrorDomainKey=1}

    let mSearchData = "M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nST: ssdp:all\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)
    let url = NSURL(string: "http://239.255.255.250:1900")!
    let mutableURLRequest = NSMutableURLRequest(URL: url)
    mutableURLRequest.HTTPBody = mSearchData
    Alamofire.request(mutableURLRequest)
        .response { (request, response, data, error) in
            println(error!)
    }

据我理解错误,说明IP地址不在正常的TCP范围内,而是在UDP范围内。我找不到任何方法来告诉 Alamofire 或配置我的 iOS 应用程序以允许将请求/响应作为多播处理。

我希望避免与 GCDAsyncUdpSocket 接口(interface),因为我已经在 100% 快速实现中使用 Alamofire。

最佳答案

由于 Alamofire 是基于 NSURLSession 构建的,因此如果没有自定义 NSURLProtocol 实现和覆盖内置管理器,它无法处理 UDP 请求。

可以关注Ray Wenderlich's tutorial for NSURLProtocol ,然后将 NSURLProtocol 插入到自定义 NSURLSessionConfiguration 的协议(protocol)类中以处理 UDP 或任何自定义协议(protocol)。

使用 GCDAsyncSocket 实现这一点可能会更直接。

关于ios - Alamofire 无法处理 UDP 请求吗? NSPOSIXErrorDomain 代码=47,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29663699/

相关文章:

ios - 为什么我使用 Swift 时屏幕截图模糊?

iphone - 更改空范围 TextView 的 NSAtributedString 颜色和字体

ios - 如何在 iOS 中将 UIPickerView 文本颜色更改为白色

ios - 在 PageViewController 导航不工作

ios - 使 UIPickerView 宽度填充 View

ios - 快速解析嵌套的 JSON 属性

swift - 如何成长一个弹出 Controller

java - 向 LAN 中的每个人发送 UDP 数据包?

apache - 如何强制 Apache 服务器使用 TCP 或 UDP?

powershell - 运行时自动从 PowerShell 作业中提取数据