我希望你能帮助我。我对Xcode和Swift非常陌生,但我正在努力提高技能。我正在将基础语言的学习与一些真实的例子混合在一起。我到达那里但我正在努力完成一个小项目。
基本上,我正在编写一个与基于套接字的服务器建立连接的小套接字客户端。在浏览网页后,我终于找到了我认为正确的方法 - 利用NSStream功能。
在我附加的视图控制器代码(附加到带有几个标签的基本视图)中,代码可以工作,因为它连接并可以从服务器接收数据;流函数中的println()调用有效。
我遇到的问题是,只要我的应用程序收到任何数据,它就会阻止用户界面。我确实在“所有输出”窗口中看到了数据,但我的界面不再有效。例如,我有一个按钮(作为导航控制器的一部分),直到我收到数据......
我使用以下命令在OS X计算机上创建服务器:
nc -k -l 2444
...这是我的视图控制器代码
import UIKit
class VCMainView: UIViewController, NSStreamDelegate{
let serverAddress: String = "127.0.0.1"
let serverPort: Int = 2444
var inputStream: NSInputStream?
var outputStream: NSOutputStream?
@IBOutlet weak var lblScreenLabel1: UILabel!
@IBOutlet weak var lblScreenLabel2: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.lblScreenLabel1.text = "Connecting"
self.lblScreenLabel2.text = "Please Wait..."
self.connectSocket()
} // func
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
} // func
func connectSocket() {
NSStream.getStreamsToHostWithName(self.serverAddress, port: self.serverPort, inputStream: &self.inputStream, outputStream: &self.outputStream)
if inputStream != nil && outputStream != nil {
inputStream!.delegate = self
outputStream!.delegate = self
inputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode)
outputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode)
inputStream!.open()
outputStream!.open()
} // if
} // func
func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {
switch (eventCode) {
case NSStreamEvent.ErrorOccurred:
break
case NSStreamEvent.EndEncountered:
break
case NSStreamEvent.None:
break
case NSStreamEvent.HasBytesAvailable:
var buffer = [UInt8](count: 4096, repeatedValue: 0)
if ( aStream == inputStream) {
let hasBytes = self.inputStream!.hasBytesAvailable
while ( hasBytes ) {
var len = self.inputStream!.read(&buffer, maxLength: buffer.count)
if(len > 0){
var output = NSString(bytes: &buffer, length: buffer.count, encoding: NSUTF8StringEncoding)
if ("" != output){
println(output)
} // if
} // if
} // while
} // if
break
case NSStreamEvent.allZeros:
break
case NSStreamEvent.OpenCompleted:
break
case NSStreamEvent.HasSpaceAvailable:
break
default:
break
} // switch
} // func
} // class
如果有人可以帮我指出正确的方向,我真的很感激......
提前致谢! 乔恩
答案 0 :(得分:1)
因此,在发布我的问题几个小时之后,即使我在发布数小时之前进行了搜索,我也发现了我做错了什么。我在另一篇文章Is calling read:maxLength: once for every NSStreamEventHasBytesAvailable correct?
中找到了Greg Parker的答案我的问题是我在循环中调用HasBytesAvailable,导致阻塞。事实证明,如果你没有读取所有可用的数据(因此缓冲区比可用的数据短),那么委托只会获得另一个事件。删除此循环修复了我的阻止问题。这是我更新的视图控制器类:
import UIKit
class VCMainView: UIViewController, NSStreamDelegate{
let serverAddress: String = "127.0.0.1"
let serverPort: Int = 2444
var inputStream: NSInputStream?
var outputStream: NSOutputStream?
@IBOutlet weak var lblScreenLabel1: UILabel!
@IBOutlet weak var lblScreenLabel2: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.lblScreenLabel1.text = "Connecting"
self.lblScreenLabel2.text = "Please Wait..."
self.connectSocket()
} // func
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
} // func
func connectSocket() {
NSStream.getStreamsToHostWithName(self.serverAddress, port: self.serverPort, inputStream: &self.inputStream, outputStream: &self.outputStream)
if inputStream != nil && outputStream != nil {
inputStream!.delegate = self
outputStream!.delegate = self
inputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode)
outputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode)
inputStream!.open()
outputStream!.open()
} // if
} // func
func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {
switch (eventCode) {
case NSStreamEvent.ErrorOccurred:
break
case NSStreamEvent.EndEncountered:
break
case NSStreamEvent.None:
break
case NSStreamEvent.HasBytesAvailable:
var buffer = [UInt8](count: 4096, repeatedValue: 0)
if ( aStream == inputStream) {
if ( self.inputStream!.hasBytesAvailable ) {
var bytesRead = self.inputStream!.read(&buffer, maxLength: 4096)
var output = NSString(bytes: &buffer, length: buffer.count, encoding: NSUTF8StringEncoding)
println(output)
} // if
} // if
break
case NSStreamEvent.allZeros:
break
case NSStreamEvent.OpenCompleted:
break
case NSStreamEvent.HasSpaceAvailable:
break
default:
break
} // switch
} // func
} // class
现在,这并不是说我的其余代码都很好,就像我在最初的问题中所说的那样,我正在学习Swift并且是一个初学者。如果有人确实看到任何错误,我当然会欢迎任何反馈。
谢谢, 乔恩