PeelTechnologies / react-native-tcp

node's net api in react-native
MIT License
284 stars 212 forks source link

TCP server on iOS only listens on the loopback address #99

Open jjv360 opened 5 years ago

jjv360 commented 5 years ago

Hi, when trying to create a TCP server with this code:

server.listen({ port: 1234, host: '0.0.0.0' })

... it ends up rejecting all connections from other devices. I also tried leaving out the host option, but there's a default of 0.0.0.0 in TcpServer.js:70 so it's the same result...

Looking at the native code, this line here converts the address 0.0.0.0 to localhost which seems wrong , since localhost would prevent outside connections... If I change it to null rather, it works correctly, same as on android...

https://github.com/PeelTechnologies/react-native-tcp/blob/b056904809b24eac2595a4c631121372edf15b71/ios/TcpSocketClient.m#L124-L127 ... change to ...

 if ([@"0.0.0.0" isEqualToString: host]) { 
     host = nil; 
 } 

Using nil seems correct, since the headers for CocoaAsyncSocket say this:

The interface may be specified by name (e.g. "en1" or "lo0") or by IP address (e.g. "192.168.4.34"). You may also use the special strings "localhost" or "loopback" to specify that the socket only accept connections from the local machine.

To accept connections on any interface pass nil, or simply use the acceptOnPort:error: method.

This is probably also the cause of these other related issues: https://github.com/PeelTechnologies/react-native-tcp/issues/78 https://github.com/PeelTechnologies/react-native-tcp/issues/72