Receiving Error Domain=kCFErrorDomainCFNetwork Code=2 when attempting to read from ReadStream

后端 未结 3 982
忘了有多久
忘了有多久 2021-01-17 11:37

I\'m attempting to synchronously read from a CFReadStream objected created by CFStreamCreatePairWithSocketToHost. The stream opened fine but when I

相关标签:
3条回答
  • 2021-01-17 12:13

    I was able to eliminate this error by removing https:// from the host String.

    NSStream.getStreamsToHostWithName("https://example.com" ...

    You may need to set the appropriate security level:

     inputStream!.setProperty(NSStreamSocketSecurityLevelTLSv1, forKey: NSStreamSocketSecurityLevelKey)
    outputStream!.setProperty(NSStreamSocketSecurityLevelTLSv1, forKey: NSStreamSocketSecurityLevelKey)
    
    0 讨论(0)
  • 2021-01-17 12:24

    I was able to fix this by putting in Google's DNS servers (8.8.8.8,8.8.4.4) in the wifi connection in the Settings app. The issue was that our devices were on a network that first required you to agree to some terms of service on a proxy login web page, much like hotels and coffee shops do. Safari worked fine after agreeing, but the app didn't, even after agreeing in Safari. Switching to alternate DNS worked (so did putting in the IP address instead of the DNS entry of our server, but I didn't want to hard-code an IP address).

    0 讨论(0)
  • 2021-01-17 12:29

    The short story: Probably a DNS resolution failure.

    The docs say "The streams do not open a connection to the specified host until one of the streams is opened", and for kCFGetAddrInfoFailureKey,

    Querying this key returns the last error code returned by getaddrinfo(3) in response to a DNS lookup. To interpret the results, look up the error code in /usr/include/netdb.h.

    netdb.h says

    #define EAI_NONAME  8  /* hostname nor servname provided, or not known */
    
    0 讨论(0)
提交回复
热议问题