Last active
August 7, 2024 07:37
-
-
Save Junch/e8f09864902db3cdac617fdec89cffb9 to your computer and use it in GitHub Desktop.
swift echo server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // main.swift | |
| // Echo | |
| // | |
| // Created by juchen3 on 1/29/21. | |
| // | |
| // http://www.alwaysrightinstitute.com/network-framework/ | |
| // https://www.appcoda.com.tw/network-framework-http/ | |
| import Foundation | |
| import Network | |
| extension NWListener { | |
| fileprivate func startListeningToIncomingConnections(newConnectionHandler: @escaping(NWConnection) -> Void){ | |
| self.stateUpdateHandler = { state in | |
| switch state { | |
| case .setup: | |
| print("Listener: setup") | |
| case .ready: | |
| print("Listener: receiving new connection") | |
| case .waiting(let error): | |
| print("Listener: waiting (\(error))") | |
| case .failed(let error): | |
| print("Listener: failed (\(error))") | |
| case .cancelled: | |
| print("Listener: cancelled") | |
| @unknown default: | |
| fatalError() | |
| } | |
| } | |
| self.newConnectionHandler = { connection in | |
| print("Listener: new connection received:", connection) | |
| newConnectionHandler(connection) | |
| } | |
| start(queue: .global()) | |
| } | |
| } | |
| extension NWConnection { | |
| fileprivate func receiveRequest() | |
| { | |
| func readData() | |
| { | |
| receive(minimumIncompleteLength: 1, maximumLength: 1024) | |
| { | |
| data, context, isComplete, error in | |
| error.map { print("Connection: receiving request error: \($0)") } | |
| guard error == nil, let data = data else { | |
| return self.cancel() | |
| } | |
| print("Received:", data) | |
| self.send(content : data, | |
| completion: .idempotent) | |
| readData() | |
| } | |
| } | |
| start(queue: .global()) | |
| readData() | |
| } | |
| } | |
| func startServer() | |
| { | |
| let listener = try! NWListener(using: .tcp, on: 8000) | |
| listener.startListeningToIncomingConnections() | |
| { | |
| connection in | |
| print("Someone tries to talk to us!:", connection) | |
| connection.receiveRequest() | |
| } | |
| } | |
| startServer() | |
| dispatchMain() // keep the tool running |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // main.swift | |
| // Echo | |
| // | |
| // Created by juchen3 on 1/29/21. | |
| // | |
| // http://www.alwaysrightinstitute.com/network-framework/ | |
| import Foundation | |
| import Network | |
| let listener = try NWListener(using: .tcp, on: 8000) | |
| listener.newConnectionHandler = { | |
| connection in | |
| print("Someone tries to talk to us!:", connection) | |
| func readData() { | |
| connection.receive(minimumIncompleteLength: 1, maximumLength: 1024) | |
| { | |
| data, context, isComplete, error in | |
| guard error == nil, let data = data else { | |
| return connection.cancel() | |
| } | |
| print("Received:", data) | |
| connection.send(content : data, | |
| completion: .idempotent) | |
| readData() // continue reading | |
| } | |
| } | |
| connection.start(queue: .main) | |
| readData() | |
| } | |
| listener.start(queue: .main) | |
| dispatchMain() // keep the tool running |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment