gpt4 book ai didi

python - Socket编程 readStream!.takeUnretainedValue() 耗时无限

转载 作者:可可西里 更新时间:2023-11-01 01:37:45 25 4
gpt4 key购买 nike

我正在尝试在前端语言中使用 Swift,在后端语言中使用 Python 来实现基于套接字的聊天应用程序。我按照教程 http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server并在终端中实现套接字并使用 telnet localhost 80 连接并成功连接到服务器套接字。但是当我尝试从 iPhone 应用程序与服务器套接字通信时,它永远不会连接。我的代码是

var readStream:  Unmanaged<CFReadStream>?
var writeStream: Unmanaged<CFWriteStream>?
CFStreamCreatePairWithSocketToHost(nil, "localhost", 80, &readStream, &writeStream)
if readStream != nil && writeStream != nil {
inputStream = readStream!.takeRetainedValue()// program halts here and not going to next line
outputStream = writeStream!.takeUnretainedValue()
}
self.inputStream.delegate = self
self.outputStream.delegate = self
self.inputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
self.outputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
self.inputStream.open()
self.outputStream.open()

代码执行到该行时停止

inputStream = readStream!.takeRetainedValue()

那么如何才能正确执行程序并调用服务器呢?

最佳答案

我自己找到了解决方案。我在用

var inputStream = NSInputStream()
var outputStream = NSOutputStream()

代替

var inputStream: NSInputStream!
var outputStream: NSOutputStream!

替换它们解决了我的问题。

关于python - Socket编程 readStream!.takeUnretainedValue() 耗时无限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34349222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com