Skip to content

Timeout less than 1000 ms will fail on IPhone 17-IOS 26 #231

@guspaz0

Description

@guspaz0

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions