connect method

Future<Socket> connect (
  1. dynamic host,
  2. int port,
  3. {dynamic sourceAddress,
  4. Duration? timeout}
)

Creates a new socket connection to the host and port and returns a Future that will complete with either a Socket once connected or an error if the host-lookup or connection failed.

host can either be a String or an InternetAddress. If host is a String, connect will perform a InternetAddress.lookup and try all returned InternetAddresses, until connected. Unless a connection was established, the error from the first failing connection is returned.

The argument sourceAddress can be used to specify the local address to bind when making the connection. The sourceAddress can either be a String or an InternetAddress. If a String is passed it must hold a numeric IP address.

The argument timeout is used to specify the maximum allowed time to wait for a connection to be established. If timeout is longer than the system level timeout duration, a timeout may occur sooner than specified in timeout. On timeout, a SocketException is thrown and all ongoing connection attempts to host are cancelled.

Implementation

static Future<Socket> connect(host, int port,
    {sourceAddress, Duration? timeout}) {
  final IOOverrides? overrides = IOOverrides.current;
  if (overrides == null) {
    return Socket._connect(host, port,
        sourceAddress: sourceAddress, timeout: timeout);
  }
  return overrides.socketConnect(host, port,
      sourceAddress: sourceAddress, timeout: timeout);
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.13.0/dart-io/Socket/connect.html