{"id":13871932,"url":"https://github.com/swiftstack/network","last_synced_at":"2025-07-16T01:32:21.839Z","repository":{"id":45813287,"uuid":"179594171","full_name":"swiftstack/network","owner":"swiftstack","description":"Non-blocking Socket, Server, Client, DNS","archived":false,"fork":false,"pushed_at":"2024-01-20T12:04:48.000Z","size":158,"stargazers_count":18,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"dev","last_synced_at":"2024-08-06T23:51:23.759Z","etag":null,"topics":["aio","async","dns","socket","swift","tcp","udp","unix"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/swiftstack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-04-04T23:57:33.000Z","updated_at":"2024-03-06T08:02:20.000Z","dependencies_parsed_at":"2024-01-20T13:23:54.532Z","dependency_job_id":null,"html_url":"https://github.com/swiftstack/network","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swiftstack%2Fnetwork","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swiftstack%2Fnetwork/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swiftstack%2Fnetwork/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swiftstack%2Fnetwork/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/swiftstack","download_url":"https://codeload.github.com/swiftstack/network/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226090029,"owners_count":17572113,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aio","async","dns","socket","swift","tcp","udp","unix"],"created_at":"2024-08-05T23:00:30.661Z","updated_at":"2024-11-23T19:31:27.570Z","avatar_url":"https://github.com/swiftstack.png","language":"Swift","funding_links":[],"categories":["Swift"],"sub_categories":[],"readme":"# AIO\n\nAsynchronous non-blocking io with syncronous API. **No callbacks.**\n\n## Package.swift\n\n ```swift\n.package(url: \"https://github.com/swiftstack/aio.git\", .branch(\"dev\"))\n```\n\n# Network\n\nZero-cost socket abstraction designed for cooperative multitasking.\n\n## Usage\n\nYou can find this code and more in [examples](https://github.com/swiftstack/examples).\n\n### Sync\n```swift\nlet socket = try Socket()\n```\n\n### Async\n```swift\nimport Async\nimport Network\n\nasync {\n    let socket = try Socket()\n    // use non-blocking api\n}\n\nloop.run()\n```\n\n```swift\nlet hello = [UInt8](\"Hello, World!\".utf8)\nlet empty = [UInt8](repeating: 0, count: hello.count + 1)\n```\n\n### TCP\n```swift\nasync {\n    let socket = try Socket()\n        .bind(to: \"127.0.0.1\", port: 1111)\n        .listen()\n\n    let client = try socket.accept()\n    _ = try client.send(bytes: hello)\n}\n\nasync {\n    let socket = try Socket()\n        .connect(to: \"127.0.0.1\", port: 1111)\n\n    var buffer = empty\n    _ = try socket.receive(to: \u0026buffer)\n}\n```\n\n### UDP\n```swift\nlet udpServerAddress = try Socket.Address(\"127.0.0.1\", port: 2222)\n\nasync {\n    let socket = try Socket(type: .datagram)\n        .bind(to: udpServerAddress)\n\n    var buffer = empty\n    var client: Socket.Address? = nil\n    _ = try socket.receive(to: \u0026buffer, from: \u0026client)\n    _ = try socket.send(bytes: hello, to: client!)\n}\n\nasync {\n    let socket = try Socket(type: .datagram)\n\n    var buffer = empty\n    _ = try socket.send(bytes: hello, to: udpServerAddress)\n    _ = try socket.receive(to: \u0026buffer)\n}\n```\n\n### TCP IPv6\n```swift\nasync {\n    let socket = try Socket(family: .inet6)\n        .bind(to: \"::1\", port: 3333)\n        .listen()\n\n    let client = try socket.accept()\n    _ = try client.send(bytes: hello)\n}\n\nasync {\n    let socket = try Socket(family: .inet6)\n        .connect(to: \"::1\", port: 3333)\n\n    var buffer = empty\n    _ = try socket.receive(to: \u0026buffer)\n}\n```\n\n### UNIX\n```swift\n#if os(Linux)\nlet type: Socket.SocketType = .sequenced\n#else\nlet type: Socket.SocketType = .stream\n#endif\n\nunlink(\"/tmp/socketexample.sock\")\n\nasync {\n    let socket = try Socket(family: .unix, type: type)\n        .bind(to: \"/tmp/socketexample.sock\")\n        .listen()\n\n    let client = try socket.accept()\n    _ = try client.send(bytes: hello)\n}\n\nasync {\n    let socket = try Socket(family: .unix, type: type)\n        .connect(to: \"/tmp/socketexample.sock\")\n\n    var buffer = empty\n    _ = try socket.receive(to: \u0026buffer)\n}\n```\n\n## Socket API\n\n```swift\nfinal class Socket {\n    enum Family {\n        case inet, inet6, unspecified, unix\n    }\n\n    enum SocketType {\n        case stream, datagram, sequenced\n    }\n\n    enum Address {\n        init(_: String, port: UInt16? = nil) throws\n        init(ip4: String, port: UInt16) throws\n        init(ip6: String, port: UInt16) throws\n        init(unix: String) throws\n    }\n\n    init(family: Family = .tcp, type: SocketType = .stream) throws\n\n    func bind(to: Address) throws -\u003e Socket\n    func listen() throws -\u003e Socket\n\n    func accept(deadline: Time = .distantFuture) throws -\u003e Socket\n    func connect(to: Address, deadline: Time = .distantFuture) throws -\u003e Socket\n\n    func close() throws\n\n    func send(bytes: UnsafeRawPointer, count: Int, deadline: Time = .distantFuture) throws -\u003e Int\n    func send(bytes: UnsafeRawPointer, count: Int, to: Address, deadline: Time = .distantFuture) throws -\u003e Int\n\n    func receive(to: UnsafeMutableRawPointer, count: Int, deadline: Time = .distantFuture) throws -\u003e Int\n    func receive(to: UnsafeMutableRawPointer, count: Int, from: inout Address?, deadline: Time = .distantFuture) throws -\u003e Int\n}\n\nextension Socket {\n    func bind(to: String, port: UInt16) throws -\u003e Socket\n    func bind(to: String) throws -\u003e Socket\n\n    func connect(to: String, port: UInt16, deadline: Time = .distantFuture) throws -\u003e Socket\n    func connect(to: String, deadline: Time = .distantFuture) throws -\u003e Socket\n\n    func send(bytes: [UInt8], deadline: Time = .distantFuture) throws -\u003e Int\n    func send(bytes: [UInt8], to: Address, deadline: Time = .distantFuture) throws -\u003e Int\n\n    func receive(to: inout [UInt8], deadline: Time = .distantFuture) throws -\u003e Int\n    func receive(to: inout [UInt8], from: inout Address?, deadline: Time = .distantFuture) throws -\u003e Int\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswiftstack%2Fnetwork","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswiftstack%2Fnetwork","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswiftstack%2Fnetwork/lists"}