以下是一些有用的Swift 3片段,用于通过Nsstream使用套接字:
var readStream: Unmanaged<CFReadStream>?var writeStream: Unmanaged<CFWriteStream>?var inputStream : inputStream? = nilvar outputStream : OutputStream? = nillet host : CFString = Nsstring(string: someHostIPString)let port : UInt32 = UInt32(somePortNumber)CFStreamCreatePairWithSocketToHost(kcfAllocatorDefault,host,port,&readStream,&writeStream)inputStream = readStream!.takeUnretainedValue()outputStream = writeStream!.takeUnretainedValue()// the enclosing class should be a StreamDelegate// to handle stream open,events,or errors,etc.inputStream!.delegate = selfoutputStream!.delegate = selfinputStream.open()outputStream.open()// the Nsstream delegate callback(s)func stream(_ aStream: Stream,handle eventCode: Stream.Event) { var n = 0 switch (eventCode) { ... case Stream.Event.hasspaceAvailable: n = self.outputStream.write(someWriteBuffer,maxLength: dataLength) ... case Stream.Event.hasBytesAvailable: n = self.inputStream.read(someReadBuffer,maxLength: bufferLength) ... // etc. }}// etc.
您可能希望使用自己的runloop在自己的线程中运行上面的代码.
总结以上是内存溢出为你收集整理的如何使用套接字编程将iOS Swift应用程序连接到硬件设备?全部内容,希望文章能够帮你解决如何使用套接字编程将iOS Swift应用程序连接到硬件设备?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)