Non-responsive stream delegate in Swift

会有一股神秘感。 提交于 2019-12-21 04:30:20

问题


So I was playing around with sockets in Swift and trying to connect the app with my server. I was having the app connect to the IP address of the server and used netcat on the server for testing. During execution, the console output from the app showed it had successfully connected to the server. However, the stream delegate does not seem to be responsive. When I typed into netcat, the app console did not print anything. I have searched for quite a while and found that my implementation is pretty similar to others. Perhaps I am missing something here that I do not see.

Any thought to this problem would be greatly appreciated!

Code is attached below:

import UIKit

class ViewController: UIViewController, StreamDelegate {

    let addr:String = "52.34.56.78"
    let port:Int = 1234

    var inputStream: InputStream?
    var outputStream: OutputStream?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.connect(host: addr, port: port)
    }

    func connect(host: String, port: Int) {

        Stream.getStreamsToHost(withName: host, port: port, inputStream: &inputStream, outputStream: &outputStream)

        if inputStream != nil && outputStream != nil {

            inputStream!.delegate = self
            outputStream!.delegate = self

            inputStream!.schedule(in: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode)
            outputStream!.schedule(in: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode)

            inputStream!.open()
            outputStream!.open()

            print("successfully connected")
        }
        else {
            print("connection unsuccessful")
        }
    }

    func stream(aStream: Stream, handleEvent eventCode: Stream.Event) {

        if aStream === inputStream {
            switch eventCode {
            case Stream.Event.errorOccurred:
                print("input: ErrorOccurred: \(aStream.streamError?.localizedDescription)")
                break
            case Stream.Event.openCompleted:
                print("input: OpenCompleted")
                break
            case Stream.Event.hasBytesAvailable:
                print("input: HasBytesAvailable")
                break
            default:
                break
            }
        }
        else {
            print("unknown stuff happened")
        }
    }
}

回答1:


So after a lot of trials and errors, I finally realized the stream() function did not work just because the signature of this function is incorrect/obsolete.

Here is what I was using:

func stream(aStream: Stream, handleEvent eventCode: Stream.Event)

But really it should be:

func stream(_ aStream: Stream, handle eventCode: Stream.Event)

This is likely a syntax conversion from previous Swift version to Swift 3. The XCode compiler usually detects obsolete functions/syntax, but sadly did not catch this one.

Hopefully my answer could help out those who are still suffering from this problem.



来源:https://stackoverflow.com/questions/41132915/non-responsive-stream-delegate-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!