我尝试使用inputStream.read方法接收数据消息缺少某些字符。我的消息太长了。例如,如果我的消息是30000字符,则输出字符串是2000个字符。缺少1000个字符。
var buffer = [UInt8](repeating:0, count:bufferSize)
var output:String = ""
while(inputStream!.hasBytesAvailable){
let bytesRead:Int = inputStream!.read(&buffer, maxLength: buffer.count)
if bytesRead > 0 {
var outputString:String=(NSString(bytes:buffer, length:bytesRead, encoding:String.Encoding.utf8.rawValue) as? String)!
if(output != "" ){
if(output.hasPrefix("ping")){ }else{
output = output + outputString
}
}else{
output=outputString
}
}else{
print("# Error Reading")
connect()
login()
}
usleep(100000)
}