Swift中的非响应流委托 [英] Non-responsive stream delegate in Swift

查看:123
本文介绍了Swift中的非响应流委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在Swift中使用套接字并试图将应用程序与我的服务器连接起来。我让应用程序连接到服务器的IP地址,并在服务器上使用 netcat 进行测试。在执行期间,应用程序的控制台输出显示它已成功连接到服务器。但是,流委托似乎没有响应。当我输入 netcat 时,应用控制台没有打印任何内容。我已经搜索了很长一段时间,发现我的实现与其他实现非常相似。也许我在这里遗漏了一些我看不到的东西。

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!

代码如下:

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")
        }
    }
}


推荐答案

所以在很多试验和错误之后,我终于意识到stream()函数不起作用只是因为此函数的签名不正确/已过时。

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.

以下是我的用法:

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

但实际上它应该是:

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

这可能是语法转换来自以前的Swift版本到Swift 3. XCode编译器通常会检测过时的函数/语法,但遗憾的是没有发现这个。

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.

这篇关于Swift中的非响应流委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆