Swift Socket 封装库:SwiftSockets
jopen
10年前
SwiftSockets是一个基于GCD的简单Socket库,用于Swift项目开发中。
该项目包括三个目标:
- ARISockets
- ARIEchoServer
- ARIFetch
建议你从ARIEchoServer开始看.
ARISockets
A framework containing the socket classes and relevant extensions. It takes a bit of inspiration from the SOPE NGStreams library.
Server Sample:
let socket = PassiveSocket<sockaddr_in>(address: sockaddr_in(port: 4242)) .listen(dispatch_get_global_queue(0, 0), backlog: 5) { println("Wait, someone is attempting to talk to me!") $0.close() println("All good, go ahead!") }
Client Sample:
let socket = ActiveSocket<sockaddr_in>() .onRead { let (count, block, errno) = $0.read() if count < 1 { println("EOF, or great error handling \(errno).") return } println("Answer to ring,ring is: \(count) bytes: \(block)") } .connect("127.0.0.1:80") { socket.write("Ring, ring!\r\n") }
ARIEchoServer
Great echo server. This is actually a Cocoa app. Compile it, run it, then connect to it in the Terminal.app via telnet 1337
.
ARIFetch
Connects a socket to some end point, sends an HTTP/1.0 GET request with some awesome headers, then shows the results the server sends. Cocoa app.
Why HTTP/1.0? Avoids redirects on www.apple.com :-)