Server constructor

Server(
  1. {String name,
  2. int multicastPort,
  3. IPVersion ipVersion,
  4. CustomAdvertisement customAdvertisement,
  5. DeviceDiscoveryListener deviceDiscoveryListener,
  6. ConnectionListener connectionListener,
  7. int serverSocketPort,
  8. bool enableDiscovery: true,
  9. ListenOn listenOn}
)

listenOn Allows you to provide the IP address the server should listen on for Client connections. If none is provided, the Server will listen on all IP address found for the supplied ipVersion

The optional customAdvertisement allows you to specify a custom advertisement message to be sent to the multicast group and received using DeviceDiscoveryListener.onAdvertisement

enableDiscovery is used to inform the Server to broadcast advertisements to a multicast group which will be joined by Clients to allow dynamic port discovery. This means, you would not need to supply a value for serverSocketPort because the Server will ask the operating system to provide an available port which it will communicate with the discovered Clients and which the Clients will use for socket connection with the Server.

If serverSocketPort is specified, the Server will listen on the port specified else it will ask the operating system for a free port which will be used. You should normally specify a serverSocketPort if enableDiscovery is set to false.

See Host for explanation on other common options.

Implementation

Server({String name, int multicastPort, IPVersion ipVersion, CustomAdvertisement customAdvertisement,
  DeviceDiscoveryListener deviceDiscoveryListener, ConnectionListener connectionListener,
  int serverSocketPort, bool enableDiscovery = true, ListenOn listenOn})
    : super(name: name ?? "Server", multicastPort: multicastPort,
      ipVersion: ipVersion, deviceDiscoveryListener : deviceDiscoveryListener,
      connectionListener : connectionListener){
  _socketPort = serverSocketPort ?? 0;
  _enableDiscovery = enableDiscovery;
  _listenOn = listenOn;
  _multicastSockets = [];
  _customAdvertisement = customAdvertisement;
  // check that the port is within a valid range
  if( _socketPort != 0 ){
    assert(_socketPort > 1024);
    assert(_socketPort < 65535);
  }

  // initialize the needed params
  _init();
}