add tag
Anonymous 1317
I am trying to open a TCP Client Connection to a remote server that is connected to an iOS device via WiFi.

I found some standard Objective-C Code to open a TCP connection at [this StackOverflow question][1] 
In my app's info.plist file, I set the following properties:  
  
Required device capabilities:    Networking Features  
Application uses WiFi  :         true  

The remote server is a measuring device that has the IP address 192.168.0.1, and is a WiFi hotspot.


When my iPad was connected to 192.168.0.1, I set the Gateway on this device to 192.168.0.1 via its web interface.

On my iPad, I set a manual IP address of 192.168.0.2, and a subnet mask 255.255.255.0 for this WiFi connection.
As far as I understand, this means the measuring device will accept a connect attempt from 192.168.0.2.    


This manual IP address setting is a requirement by the way, which I can't change, due to other requirements of myApp.  The IP address must be fixed.

My code tries to get read and write streams for IP Address 192.168.0.1, port 80.
It fails every time with the error message "Network is down", despite the iPad showing a WLAN connection to the server (measuring device).

The iPad logs are below. 
As far as I can see from the log, the iPad is trying to connect over its (disabled) mobile connection, not the WiFi.

~~~
09:38:01 myiPad myApp[25061] <Notice>: myObjCcode: Start opening streams…     
09:38:01 myiPad myApp(libnetwork.dylib)[25061] <Notice>:networkd_settings_read_from_file initialized networkd settings by reading plist directly     
09:38:01 myiPad myApp(CFNetwork)[25061] <Notice>: TCP Conn [1:0x283725080] using empty proxy configuration    
09:38:01 myiPad myApp(CFNetwork)[25061] <Notice>: Stream client bypassing proxies on TCP Conn [1:0x283725080]     
09:38:01 myiPad myApp(CFNetwork)[25061] <Notice>: TCP Conn 0x283725080 started     
09:38:01 myiPad myApp(libnetwork.dylib)[25061] <Notice>: [C1
 0A23703E-6245-4EED-9C8B-5F25D5300D9F Hostname#00ff9b80:80 tcp,
 legacy-socket] start    
.
09:38:01 myiPad myApp(libnetwork.dylib)[25061] <Notice>:
 nw_connection_report_state_with_handler_on_nw_queue [C1] reporting
 state waiting     
09:38:01 myiPad myApp(libnetwork.dylib)[25061] <Notice>: nw_connection_report_state_with_handler_on_nw_queue [C1]
 reporting state preparing     
09:38:01 myiPad mDNSResponder[120] <Notice>: [R29744] DNSServiceCreateConnection START PID[25061](myApp) 
09:38:01 myiPad mDNSResponder[120] <Notice>: [R29745] DNSServiceGetAddrInfo(C000D000, -1, 0, <private>) START PID[25061](myApp)     
09:38:01 myiPad mDNSResponder[120] <Notice>: [R29745->Q0] GenerateNegativeResponse: Generating negative response for question <private> (AAAA)     
09:38:01 myiPad mDNSResponder[120] <Notice>: <private>     
09:38:01 myiPad mDNSResponder[120] <Notice>: [R29745->Q0] GenerateNegativeResponse: Generating negative response for question <private> (Addr)    
09:38:01 myiPad mDNSResponder[120] <Notice>: <private>     
09:38:01 myiPad mDNSResponder[120] <Notice>: [R29745->Q0] GenerateNegativeResponse: Generating negative response for question <private> (AAAA)     
09:38:01 myiPad mDNSResponder[120] <Notice>: [R29745->Q0] DNSServiceGetAddrInfo(<private>, AAAA) RESULT ADD interface -1: (mortal)<private>     
09:38:01 myiPad mDNSResponder[120] <Notice>: [R29745->Q0] GenerateNegativeResponse: Generating negative response for question <private> (Addr)    
09:38:01 myiPad mDNSResponder[120] <Notice>: [R29745->Q0] DNSServiceGetAddrInfo(<private>, Addr) RESULT ADD interface -1: (mortal)<private>   
 
09:38:01 myiPad myApp(libnetwork.dylib)[25061] <Notice>: nw_connection_report_state_with_handler_on_nw_queue [C1] reporting state failed error Network is down     
09:38:01 myiPad mDNSResponder[120] <Notice>: [R29744] DNSServiceCreateConnection STOP PID[25061](myApp)     
09:38:01 myiPad myApp(CFNetwork)[25061] <Notice>: TCP Conn 0x283725080 event 3. err: 50     
09:38:01 myiPad mDNSResponder[120] <Notice>: [R29745] DNSServiceGetAddrInfo(<private>) STOP PID[25061](myApp)     
09:38:01 myiPad myApp(libnetwork.dylib)[25061] <Error>: nw_connection_get_connected_socket [C1] Client called nw_connection_get_connected_socket on unconnected nw_connection   
09:38:01 myiPad myApp(CFNetwork)[25061] <Notice>: TCP Conn 0x283725080 complete. fd: -1, err: 50     
09:38:01 myiPad myApp(CFNetwork)[25061] <Error>: TCP Conn 0x283725080 Failed : error 0:50 [50]     
09:38:01 myiPad CommCenter[80] <Notice>: #I connectionFailureAlertHandler: network_config_cellular_failed_observer canactivate: f(kDataNotAllowed{Data is not Allowed or Blocked: PDP Context canot be made active while Data is disabled(1)  [ allowed=false blocked=false netAvailable=false home=true ]})     
09:38:01 myiPad CommCenter[80] <Notice>: #I run activate mechanism for failing socket     
09:38:01 myiPad CommCenter[80] <Notice>: #I Settings all interfaces availability to false due to kDataPlan     
09:38:01 myiPad CommCenter[80] <Notice>: #I setting interface availability, pdp: 0, source: kDataPlan available: 0     
09:38:01 myiPad CommCenter[80] <Notice>: #I map for pdp:0 {  (kAirplaneMode:1) (kDataEnabled:1) (kDataDisabledButton:1) (kDataPlan:0) (kRoaming:1) (kThrottling:1) (kCellularPlan:1) }     
09:38:01 myiPad CommCenter[80] <Notice>: #I changing interface availability, pdp: 0, available: 0   
09:38:01 myiPad CommCenter(NetworkExtension)[80] <Notice>: NEPathEvent Cellular Failed for com.myCompany.myAppId by PID     
09:38:01 myiPad CommCenter[80] <Notice>: #I  kSCPropNetCommCenterAvailable Value = 0, retreival result = true     
09:38:01 myiPad CommCenter[80] <Notice>: #I (PDPManagerInterface::ifstatus = 2, Boolean newstate =0     
09:38:01 myiPad CommCenter[80] <Notice>: #I returning value 0    
09:38:01 myiPad CommCenter[80] <Notice>: #I setting interface availability, pdp: 1, source: kDataPlan available: 0     
09:38:01 myiPad CommCenter[80] <Notice>: #I map for pdp:1 {  (kAirplaneMode:1) (kDataEnabled:1) (kDataDisabledButton:1) (kDataPlan:0) (kCellularPlan:1) }     
09:38:01 myiPad CommCenter[80] <Notice>: #I changing interface availability, pdp: 1, available: 0   
09:38:01 myiPad CommCenter[80] <Notice>: #I  kSCPropNetCommCenterAvailable Value = 0, retreival result = true     
09:38:01 myiPad CommCenter[80] <Notice>: #I (PDPManagerInterface::ifstatus = 2, Boolean newstate =0 
09:38:01 myiPad CommCenter[80] <Notice>: #I returning value 0    
09:38:01 myiPad CommCenter[80] <Notice>: #I setting interface availability, pdp: 2, source: kDataPlan available: 0     
09:38:01 myiPad CommCenter[80] <Notice>: #I map for pdp:2 {  (kAirplaneMode:1) (kDataEnabled:1) (kDataDisabledButton:1) (kDataPlan:0) (kCellularPlan:1) }     
09:38:01 myiPad CommCenter[80] <Notice>: #I changing interface availability, pdp: 2, available: 0     > 09:38:01 myiPad CommCenter[80] <Notice>: #I  kSCPropNetCommCenterAvailable Value = 0, retreival result = true     
09:38:01 myiPad CommCenter[80] <Notice>: #I (PDPManagerInterface::ifstatus = 2, Boolean newstate =0 
09:38:01 myiPad CommCenter[80] <Notice>: #I returning value 0    
09:38:01 myiPad CommCenter[80] <Notice>: #I setting interface availability, pdp: 3, source: kDataPlan available: 0     
09:38:01 myiPad CommCenter[80] <Notice>: #I map for pdp:3 {  (kAirplaneMode:1) (kDataEnabled:1) (kDataDisabledButton:1) (kDataPlan:0) (kCellularPlan:1) }    .     
09:38:01 myiPad myApp[25061] <Notice>: myObjCcode: In Stream event handler…     
09:38:01 myiPad CommCenter[80] <Notice>: #I changing interface availability, pdp: 3, available: 0   
09:38:01 myiPad CommCenter[80] <Notice>: #I  kSCPropNetCommCenterAvailable Value = 0, retreival result = true    
09:38:01 myiPad CommCenter[80] <Notice>: #I (PDPManagerInterface::ifstatus = 2, Boolean newstate =0  
09:38:01 myiPad CommCenter[80] <Notice>: #I returning value 0     
09:38:01 myiPad CommCenter[80] <Notice>: #I setting interface availability, pdp: 4, source: kDataPlan available: 0     
09:38:01 myiPad CommCenter[80] <Notice>: #I map for pdp:4 {  (kAirplaneMode:1) (kDataEnabled:1) (kDataDisabledButton:1) (kDataPlan:0) (kCellularPlan:1) }     
09:38:01 myiPad CommCenter[80] <Notice>: #I changing interface availability, pdp: 4, available: 0    
09:38:01 myiPad myApp[25061] <Notice>: myObjCcode: stream event 8    
09:38:01 myiPad myApp[25061] <Notice>: myObjCcode: The operation couldn't be completed. Network is down
~~~

Is my guess correct, that it's trying to connect over the wrong network, and if yes, then how can I force it to connect over WiFi to 192.168.0.1?

  [1]: https://stackoverflow.com/questions/19003063/ios-simple-tcp-connection-example
Top Answer
Anonymous 1317
It turned out that there were two problems.

Firstly, it was not possible to telnet to the measuring device while the gateway was set to the device's IP address.
When I changed the gateway value to "0.0.0.0" on the measuring device, it was possible to telnet to the device.

An attempt to connect using my Objective-C code then resulted in `kCFErrorDomainCFNetwork error 2.`

This error code is associated with DNS problems.  But my code passed an IP address to `CFStreamCreatePairWithSocketToHost`, not a URL.  The code should not have been trying to make DNS lookup requests.

The problem lay in the `NSString*` parameter being passed as the address.

I changed the address parameter in the function call to this:


```
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)@"192.168.0.1", port, &readStream, &writeStream);
```

and it is accepted as a valid IP address.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.