Introduction to Net Operators
The Net Operators module provides a comprehensive set of tools for managing network connections, protocols, and data streams within the MS ecosystem. This documentation outlines the various operators available and their functionalities.
Core Operators
Connect
Establishes a network connection to a specified address and port.
Parameters:
host
(string): The hostname or IP address to connect to.port
(number): The port number to connect to.protocol
(string, optional): The protocol to use (e.g., "TCP", "UDP"). Defaults to "TCP".
Example:
Connect(host="example.com", port=80, protocol="TCP")
Listen
Starts a server that listens for incoming network connections on a specified port.
Parameters:
port
(number): The port number to listen on.protocol
(string, optional): The protocol to use (e.g., "TCP", "UDP"). Defaults to "TCP".
Example:
Listen(port=5000, protocol="TCP")
Receive
Reads data from an active network connection.
Parameters:
connection
(ConnectionObject): The active network connection object obtained fromConnect
orListen
.maxLength
(number, optional): The maximum number of bytes to receive. Defaults to a system-defined buffer size.
Example:
let client = Connect(host="localhost", port=5000);
let data = Receive(connection=client, maxLength=1024);
Send
Sends data over an active network connection.
Parameters:
connection
(ConnectionObject): The active network connection object.data
(string | bytes): The data to send.
Example:
let server = Listen(port=5000);
let client = Accept(server); // Assuming Accept operator exists
Send(connection=client, data="Hello, client!");
Close
Closes an active network connection.
Parameters:
connection
(ConnectionObject): The network connection object to close.
Example:
Close(connection=myConnection);
Advanced Operators
Accept
Accepts an incoming connection on a listening socket.
Parameters:
listener
(ListenerObject): The listener object obtained from theListen
operator.
Example:
let server = Listen(port=8080);
let newClient = Accept(listener=server);
UdpReceive
Receives data from a UDP socket.
Parameters:
socket
(UDPSocketObject): The UDP socket object.maxLength
(number, optional): The maximum number of bytes to receive.
Example:
let udpSocket = UdpCreate(); // Assuming UdpCreate operator exists
let (data, senderAddress) = UdpReceive(socket=udpSocket, maxLength=512);
UdpSend
Sends data to a specific address and port via UDP.
Parameters:
socket
(UDPSocketObject): The UDP socket object.data
(string | bytes): The data to send.address
(string): The destination IP address.port
(number): The destination port.
Example:
let udpSocket = UdpCreate();
UdpSend(socket=udpSocket, data="Hello UDP!", address="192.168.1.100", port=12345);
Error Handling
Network operations can encounter various errors. It is recommended to wrap these operations in error handling blocks. The exact syntax for error handling might depend on the broader MS scripting environment.
Data Types
Data transferred over the network is typically represented as either string
or bytes
. Ensure that data is correctly encoded and decoded as needed.
Common Use Cases
- Building client-server applications.
- Inter-process communication (IPC) over a network.
- Network monitoring and diagnostics.
- Data streaming.