ThServerSocket
Implements a lightweight asynchronous server socket component for handling both TCP and UDP communications.This class is designed for use in non-blocking, event-driven applications and provides high-level methods for opening TCP listeners, sending and receiving UDP datagrams, and managing client sessions.
It includes internal socket management and event notification mechanisms to signal incoming connections or data reception.
Suitable for LAN services, message-based systems, and low-latency custom protocol servers.
Public Procedures and Functions:
Stop
Stops any running socket server (TCP listener or UDP port).This procedure shuts down all open sockets and clears the internal state.
Procedure Stop;
Connect_UDP_To
Creates and connects a UDP socket to a remote IP and port.Optionally binds the socket to a specific local network interface.
Function Connect_UDP_To(_IP: RawByteString; _Port: Integer; Bind_NIC_IP: RawByteString = ''): NativeUInt;
Parameters
- _IP: RawByteString; destination IP address to connect to.
- _Port: Integer; destination port number.
- Bind_NIC_IP: RawByteString; optional local interface IP to bind the socket.
Return Value
Returns the handle of the created socket.Returns 0 if socket creation or binding fails.
Open_UDP_Port
Opens a UDP socket on a specific local IP address and port.Function Open_UDP_Port(Bind_To_IP: RawByteString; _Port: Integer): Boolean;
Parameters
- Bind_To_IP: RawByteString; local IP address to bind to.
- _Port: Integer; local port to listen on for incoming datagrams.
Return Value
Returns True if the UDP port was successfully opened.Returns False on failure.
Listen_To_TCP
Starts listening on a specified IP and port using a TCP server socket.If an error occurs and HALT_On_Error is True, the app halts execution immediately.
Function Listen_To_TCP(Bind_To_IP: RawByteString; _Port: Integer; HALT_On_Error: Boolean): Boolean;
Parameters
- Bind_To_IP: RawByteString; local address to bind the TCP listener.
- _Port: Integer; port to listen on for incoming TCP connections.
- HALT_On_Error: Boolean; if True, calls HALT on failure instead of silent return.
Return Value
Returns True if the TCP server socket was successfully created and listening.Returns False if binding or listening fails (only when HALT_On_Error is False).
Properties:
OnTCP_NewSession
Event handler triggered when a new TCP client connects to the server.Property OnTCP_NewSession: TSocketEvent;
Parameters
- This event provides the socket handle and remote IP/port details of the new connection.
OnUDP_DataArrived
Event handler triggered when a UDP datagram is received.Property OnUDP_DataArrived: TSocketEvent;
Parameters
- This event provides the received data and sender information (IP and port).