Skip to main content

Socket Connect

This block establishes a TCP/IP socket connection to a remote server. Use it to initiate network connections for custom protocols.

Socket Connect Block

Overview

The Socket Connect block creates TCP/IP connections to remote servers, enabling custom protocol implementations and direct network communication.

Configuration

  • Host: Remote server hostname or IP address
  • Port: TCP port number to connect to
  • Timeout: Connection timeout in seconds
  • TLS/SSL: Enable encrypted connection

Connection Parameters

Configure connection settings:

Host

  • IP address: 192.168.1.100
  • Hostname: server.example.com
  • Use variables for dynamic hosts

Port

  • Standard ports: 80 (HTTP), 443 (HTTPS), 502 (Modbus TCP)
  • Custom application ports
  • Dynamic port selection

Timeout

  • Connection establishment timeout
  • Typical: 5-30 seconds
  • Adjust based on network conditions

Connection Flow

Typical socket connection flow:

  1. Execute Socket Connect block
  2. Wait for connection establishment
  3. On Socket Event triggers on success/failure
  4. Send data with Socket Send
  5. Receive data with On Socket Data
  6. Close connection when done

TLS/SSL Connections

Enable secure connections:

  • Encrypted data transmission
  • Server certificate verification
  • Secure protocol implementations

Required for:

  • HTTPS communication
  • Secure MQTT (port 8883)
  • Protected APIs
  • Sensitive data transmission

Connection Management

Manage socket connections:

  • One connection per block instance
  • Reuse connections for efficiency
  • Close when no longer needed
  • Reconnect on failure

Error Handling

Handle connection failures:

Common Errors

  • Connection refused: Server not listening on port
  • Timeout: Network unreachable or slow
  • DNS failure: Cannot resolve hostname
  • Certificate error: TLS validation failed

Error Recovery

  • Retry with exponential backoff
  • Try alternate servers
  • Log errors for diagnostics
  • Alert on persistent failures

Dynamic Connection

Build connection parameters dynamically:

  • Load server address from settings
  • Select port based on conditions
  • Failover to backup servers
  • Runtime configuration

Use Cases

Common socket connection scenarios:

  • Connect to Modbus TCP devices
  • Query remote databases
  • Call REST APIs over TCP
  • Custom protocol communication
  • Industrial equipment integration

Connection Pooling

For multiple connections:

  • Create separate Socket Connect blocks
  • Manage connection lifecycle
  • Implement connection pooling
  • Monitor active connections

Network Requirements

Socket connections require:

  • Active network connectivity
  • WiFi, Cellular, or Ethernet
  • DNS resolution (for hostnames)
  • Firewall rules allowing outbound connections

Protocol Examples

Connect to various services:

Modbus TCP

  • Host: PLC IP address
  • Port: 502
  • No TLS

HTTPS API

  • Host: api.example.com
  • Port: 443
  • TLS enabled

Custom Protocol

  • Host: Custom server
  • Port: Application-specific
  • TLS optional

Performance

Connection performance tips:

  • Reuse connections when possible
  • Set appropriate timeouts
  • Implement connection caching
  • Monitor connection health

Linking to Settings

Socket parameters can be linked to settings for:

  • Runtime server configuration
  • Port selection
  • Timeout adjustment
  • Environment-specific endpoints

Troubleshooting

Common connection issues:

  • Cannot connect: Check host, port, and network
  • Timeout: Increase timeout or check network latency
  • DNS error: Verify hostname spelling and DNS servers
  • TLS error: Check certificate validity and system time

See Also