bind static method
Create and bind a new SHSP socket to a specific address and port
This factory method:
- Validates the port number (must be between 0 and 65535)
- Binds the socket to the specified local address and port
- Initializes the message callback map
- Sets up all event listeners (read, close, error, etc.)
Parameters:
address: The local InternetAddress to bind to (e.g., InternetAddress.anyIPv4)port: The local port number to listen on (0-65535)
Returns: A Future that resolves to a new ShspSocket instance
Throws:
ShspValidationExceptionif port is invalidShspNetworkExceptionif binding fails
Example:
final socket = await ShspSocket.bind(InternetAddress.anyIPv4, 8000);
Implementation
static Future<ShspSocket> bind(InternetAddress address, int port) async {
// Validate port range
if (port < 0 || port > 65535) {
throw ShspValidationException(
'Port must be between 0 and 65535',
field: 'port',
value: port,
);
}
RawDatagramSocket? rawSocket = await RawDatagramSocket.bind(address, port);
final callbacks = MessageCallbackMap();
final socket = ShspSocket.internal(rawSocket, callbacks);
socket._localAddress = address;
socket._localPort = rawSocket.port; // Read actual port from OS, not parameter
socket._invokeOnListening();
return socket;
}