ios - 带身份验证的 Swift Telnet 流

标签 ios swift authentication tcp telnet

以下代码在使用 TCP 连接到许多设备时效果很好。但是尝试连接到要求用户名和密码的设备时,却没有。

当通过 telnet 连接 putty 时,像 Cisco 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
    }
  }
}

最佳答案

telnet 协议(protocol)不仅仅是套接字上的纯文本。您可以在 RFC854 中找到详细信息

更新:有一个更新的规范 RFC5198如果不需要 Unicode,则可能不需要实现它。

关于ios - 带身份验证的 Swift Telnet 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45719381/

相关文章:

ios - ScrollToItem 无法正常工作

ios - swift 线程 1 : EXC_BAD_INSTRUCTION when I try to change a uilabel. 文本

本地网络上的 MySQL 远程访问出现身份验证错误

iOS - 使用分隔 View 调整垂直 View 的大小

ios - 应用程序被锁定 malloc -> OSSpinLockLock$VARIANT$mp

iOS:如何判断类型为 id 的对象是类还是协议(protocol)

ios - 当应用程序已打开时,Segue 不执行快速操作(Xcode 9)

swift - 使用 Coco/R 翻译源代码后如何测试输入条件?

session - couchdb 通过重写的公共(public)接口(interface)身份验证

javascript - 无法使用 TwitterHelper 在 Twitter 上发布状态消息