套接字编程readStream!.takeUnretainedValue()需要无限时间

时间:2015-12-18 05:46:07

标签: python ios iphone swift sockets

我正在尝试使用前端语言的Swift和后端的Python实现基于Socket的聊天应用程序。我按照教程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()

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

0 个答案:

没有答案