crystal-sctp

Crystal bindings for SCTP

Crystal SCTP

Crystal bindings and high-level API for libusrsctp, providing SCTP (Stream Control Transmission Protocol) support for macOS and Linux.

Features

  • ๐Ÿš€ High-level Socket-like API for SCTP
  • ๐Ÿ”ง Complete bindings to libusrsctp
  • ๐ŸŒ Multi-homing support (bind/connect to multiple addresses)
  • ๐Ÿ“ฆ Multi-streaming support
  • ๐Ÿ”’ Type-safe Crystal interface
  • ๐Ÿงช Comprehensive test suite
  • ๐ŸŽ macOS and Linux support

Installation

  1. Install libusrsctp:

    macOS:

    brew install usrsctp
    

    Linux (Ubuntu/Debian):

    sudo apt-get install libusrsctp-dev
    
  2. Add this to your application's shard.yml:

    dependencies:
      sctp:
        github: yourusername/crystal-sctp
    
  3. Run shards install

Usage

Basic Server

require "sctp"

# Create and bind an SCTP socket
server = SCTP::Socket.new
server.bind("127.0.0.1", 3000)
server.listen

puts "SCTP server listening on port 3000"

# Accept connections
loop do
  client = server.accept
  spawn do
    message = client.read_string(1024)
    puts "Received: #{message}"
    client.write("Echo: #{message}")
    client.close
  end
end

Basic Client

require "sctp"

# Connect to SCTP server
client = SCTP::Socket.new
client.connect("127.0.0.1", 3000)

client.write("Hello, SCTP!")
response = client.read_string(1024)
puts "Server response: #{response}"

client.close

Multi-streaming

require "sctp"

socket = SCTP::Socket.new(streams: 10)
socket.connect("127.0.0.1", 3000)

# Send on different streams
socket.write("Stream 0 data", stream: 0)
socket.write("Stream 1 data", stream: 1)
socket.write("Stream 5 data", stream: 5)

socket.close

Multi-homing (Redundant Addresses)

require "sctp"

# Server binds to multiple addresses for redundancy
server = SCTP::Socket.new
server.bind(["192.168.1.100", "10.0.0.100"], 3000)
server.listen

# Client connects to multiple addresses
client = SCTP::Socket.new
client.connect(["192.168.1.100", "10.0.0.100"], 3000)

# SCTP automatically handles failover between addresses
client.write("Data with redundant paths")
client.close

API Overview

The library provides several classes:

  • SCTP::Socket - High-level socket interface (similar to TCPSocket/UDPSocket)
  • SCTP::Server - High-level server interface (similar to TCPServer)
  • SCTP::Address - Represents SCTP addresses with multi-homing support
  • SCTP::Stream - Represents individual SCTP streams
  • LibUsrSCTP - Low-level C bindings

Development

# Run tests
crystal spec

# Run examples
crystal run examples/server.cr
crystal run examples/client.cr

Contributing

  1. Fork it (https://github.com/yourusername/crystal-sctp/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

MIT License - see LICENSE file for details

Resources

Repository

crystal-sctp

Owner
Statistic
  • 1
  • 0
  • 0
  • 0
  • 0
  • 2 months ago
  • December 27, 2025
License

MIT License

Links
Synced at

Tue, 27 Jan 2026 10:23:43 GMT

Languages