PeelTechnologies / react-native-tcp

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

tcp server won't connect to client packets on iOS device #78

Closed fwaveVT closed 6 years ago

fwaveVT commented 6 years ago

I have been able to get a tcp server to listen for connections and receive data on Android devices and the iOS simulator. However, I can't seem to get my iPhone (11.3.1) to respond no matter how I try to configure server.listen(PORT, HOST). I notice that my Android device will listen for an IPv4 client, and my iOS device an IPv6 client, but I don't think this should matter. Using RN 48.1 and Packet Sender as a test client.

acb commented 6 years ago

I think this is a duplicate of https://github.com/PeelTechnologies/react-native-tcp/issues/72

I was able to get the server to listen by setting the host to be the device's IP address, no idea why that's not the default. Code looks like

import TCP from 'react-native-tcp';
import { NetworkInfo } from 'react-native-network-info';

componentDidMount = () => {
        NetworkInfo.getIPV4Address(ip => {
            this.setState({myIpAddress: ip});
        });
}
...
let server = TCP.createServer((socket) => {}).listen(PORT, this.state.myIpAddress);

Although even with that I'm having issues on iOS where socket.write() just isn't writing the data, so that's pretty cool.

fwaveVT commented 6 years ago

Yes, that works great. For iOS devices, I guess you must discover your device's IPv4 address (e.g. using network-info) and then use that when specifying the port and host. I did it like this:

// Get IPv4 IP NetworkInfo.getIPV4Address(ipv4 => { console.log(ipv4); HOST = String(ipv4); });

then

const serverLeft = net.createServer((socket) => { / socket stuff / }) .listen({port: serverPortSensorLeft, host: HOST}, () => { console.log('ServerLeft listening to ' + JSON.stringify(serverLeft.address())); });

Thank you.