一个用 swift 编写的 POSIX 套接字包装器。
如果您认为需要实现某些功能,请打开一个 issue 或提交 PR
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()
CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
要将 Socket.swift 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
target '<Your Target Name>' do
pod 'Socket.swift', '~> 2.4.0'
end
然后,运行以下命令
$ pod install
Carthage 是一个去中心化的依赖管理器,它可以构建您的依赖项并为您提供二进制框架。
您可以使用 Homebrew 使用以下命令安装 Carthage
$ brew update
$ brew install carthage
要将 Socket.swift 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "BiAtoms/Socket.swift" ~> 2.4.0
运行 carthage update
以构建框架,并将构建的 SocketSwift.framework
拖到您的 Xcode 项目中。
Swift Package Manager 是一个用于自动化 Swift 代码分发的工具,并已集成到 swift
编译器中。它还处于早期开发阶段,但 Socket.swift 确实支持在受支持的平台上使用它。
一旦您设置好 Swift 包,将 Socket.swift 添加为依赖项就像将其添加到 Package.swift
的 dependencies
值中一样简单。
dependencies: [
.package(url: "https://github.com/BiAtoms/Socket.swift.git", from: "2.4.0")
]
只需拖放 Sources 文件夹中的文件即可。
另请参阅参与此项目的贡献者列表。
本项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅 LICENSE 文件