i was developing a quick TCP port scanner feature and realized that in IOS if i put a timeout less than 1000 ms, the socket fails automatically.
import net from 'net';
import { Platform } from 'react-native';
import { ConnectionOptions } from 'react-native-tcp-socket/lib/types/Socket';
export const createSocket = async (
options: ConnectionOptions
) => {
try {
const socket = new net.Socket();
return await new Promise<net.Socket>(async (resolve, reject) => {
socket?.on('error', (err: any) => {
if (socket) socket?.destroy();
reject(err)
});
socket?.on('close', () => {
if (socket) socket?.destroy();
});
socket?.on('timeout', () => {
if (socket) socket?.destroy();
});
socket?.on('connect', () => {
resolve(socket);
});
socket?.connect({
...options,
...Platform.select({
android: {
interface: 'wifi',
},
ios: {
connectTimeout: 1000, // if you set this less than 1000 will fail
},
}),
});
});
} catch (e) {
//continue
console.log(e);
}
};
On Android 13 phisical device works after 333 ms connectTimeout, is a safe treshold. in IOS everything 999 below will instantly trigger 'error' event.
i was developing a quick TCP port scanner feature and realized that in IOS if i put a timeout less than 1000 ms, the socket fails automatically.
On Android 13 phisical device works after 333 ms connectTimeout, is a safe treshold. in IOS everything 999 below will instantly trigger 'error' event.