Latest 2.1.0
Homepage https://github.com/BiAtoms/Socket.swift
License MIT
Platforms ios 8.0, osx 10.9, tvos 9.0
Authors

Platform Linux
Platform iOS macOS tvOS
Cocoapods Compatible
Build Status - Master

A POSIX socket wrapper written in swift.

Features

  • TLS support
  • Linux, iOS, macOS and tvOS support
  • Clean and understanable code

If you consider something needs to be implemented, just open an issue or make a PR

Example

let server = try Socket(.inet, type: .stream, protocol: .tcp) // create server socket
try server.set(option: .reuseAddress, true) // set SO_REUSEADDR to 1
try server.bind(port: 8090, address: nil) // bind 'localhost:8090' address to the socket
try server.listen() // allow incoming connections

let client = try Socket(.inet, type: .stream, protocol: .tcp) // create client socket
try client.connect(port: 8090) // connect to localhost:8090

let clientAtServerside = try server.accept() // accept client connection

let helloBytes = ([UInt8])("Hello World".utf8)
try clientAtServerside.write(helloBytes) // sending bytes to the client
clientAtServerside.close()

var buffer = [UInt8](repeating: 0, count: helloBytes.count) // allocate buffer
let numberOfReadBytes = try client.read(&buffer, size: helloBytes.count)
print(numberOfReadBytes == helloBytes.count) // true
print(buffer == helloBytes) // true

client.close()
server.close()

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate Socket.swift into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
target '<Your Target Name>' do
    pod 'Socket.swift', '~> 2.1'
end

Then, run the following command:

$ pod install

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but Socket.swift does support its use on supported platforms.

Once you have your Swift package set up, adding Socket.swift as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .Package(url: "https://github.com/BiAtoms/Socket.swift.git", majorVersion: 2)
]

Manually

Just drag and drop the files in the Sources folder.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License – see the LICENSE file for details

Latest podspec

{
    "name": "Socket.swift",
    "version": "2.1.0",
    "summary": "A POSIX socket wrapper written in swift.",
    "homepage": "https://github.com/BiAtoms/Socket.swift",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Orkhan Alikhanov": "[email protected]"
    },
    "source": {
        "git": "https://github.com/BiAtoms/Socket.swift.git",
        "tag": "2.1.0"
    },
    "module_name": "SocketSwift",
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "tvos": "9.0"
    },
    "source_files": "Sources/*.swift",
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This