Swift Network TCP受信 受信

概要

ソース

受信

import Network

print("recieve start")
let myQueue  = DispatchQueue.global()
do {
    let listener = try NWListener(using: .tcp, on: 8090)
    listener.newConnectionHandler = { (newConnection) in
        // Handle inbound connections
        print("connection ok")

        newConnection.receive(minimumIncompleteLength: 1, maximumLength: 1000)
        { (content, contentContext, isComplete, error) in
            if let error = error {
                // Handle error in reading
                print(error)
            } else {
                // Parse out body length
                print("receive")
                let receiveData = String(data: content!, encoding: String.Encoding.utf8)
                print(receiveData!)
            }
        }
        
        newConnection.start(queue: myQueue)
    }
    print("listener start")
    listener.start(queue: myQueue)
}
catch {
    print(error)
}
sleep(60)
print("end")

Swift

Posted by shi-n