0
以下代碼在與許多設備連接時很有用。但試圖連接到要求輸入用戶名和密碼的設備時,它不會。具有身份驗證的Swift Telnet流
當通過telnet連接膩子時,思科SX 80等設備在連接後返回登錄:提示。
但是當使用下面的代碼時,會發生OpenCompleted事件,然後發生HasBytesAvaialable,但緩衝區爲空。
將socketSecurityLevel設置爲其他任何內容會引發錯誤,因爲它是普通的telnet,而不是SSL。
關於我失蹤的任何想法?
func openTcpStream(){
if !tcpHost.isEmpty{
Stream.getStreamsToHost(withName: tcpHost, port: tcpPort, inputStream: &inputStream, outputStream: &outputStream)
if inputStream != nil{
let input: InputStream = inputStream!
input.delegate = self
input.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
self.inputStream!.setProperty(StreamSocketSecurityLevel.none, forKey: Stream.PropertyKey.socketSecurityLevelKey)
input.open()
}
if outputStream != nil{
let output: OutputStream = outputStream!
output.delegate = self
output.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
self.outputStream!.setProperty(StreamSocketSecurityLevel.none, forKey: Stream.PropertyKey.socketSecurityLevelKey)
output.open()
}
}
}
func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
var event = ""
switch eventCode {
case Stream.Event.openCompleted:
if writeQueue != ""{
writeTcpStream(bufferToWrite: writeQueue)
}
case Stream.Event.hasBytesAvailable:
if inputStream != nil{
readTcpStream(inputStream: inputStream)
}
default:()
}
}
func readTcpStream(inputStream: InputStream?){
if inputStream != nil{
let input = inputStream!
var readBuffer: NSString = ""
var readByte = [UInt8](repeating:0, count: 512)
while input.hasBytesAvailable{
input.read(&readByte, maxLength: readByte.count)
let tempString = NSString(bytes: readByte, length: readByte.count, encoding: String.Encoding.utf8.rawValue)
if tempString?.length != nil{
readBuffer = (readBuffer as String) + (tempString! as String) as NSString
}
}
}
}