I have a seemingly basic swift TCP socket program that is sending a base64 string to a TCPlistener. To test i'm running netcat on mac os x listening on an open port and also another program using C# in that instance to check the string data as a secondary check thinking that maybe somehow i wasn't using netcat properly. The problem is that the output string doesn't seem to be as long as the actual string i'm trying to send.
Swift code:
let addr = "127.0.0.1"
let port = 10001
var host :NSHost = NSHost(address: addr)
var inp :NSInputStream?
var out :NSOutputStream?
NSStream.getStreamsToHostWithName(addr, port: port, inputStream: &inp, outputStream: &out)
let inputStream = inp!
let outputStream = out!
inputStream.open()
outputStream.open()
var readByte :UInt8 = 0
while inputStream.hasBytesAvailable {
inputStream.read(&readByte, maxLength: 1)
}
print("Actual base64string number of characters: ", (base64String.characters.count))
let OStreamCount = outputStream.write(base64String, maxLength: (base64String.characters.count))
print("Transmitted number of characters: ", OStreamCount)Sometimes it works fine and the transmitted number of characters is equal to the number of characters of the base64string but most of the time it doesn't.
In netcat i simply listen on the same port and write the input to a text file and then check the number of characters in that file to see if the number of characters are equal to the transmitted number of characters which it does.
So why doesn't the outputStream always send the full string? Also as a side note this code works fine for small strings it's only for large strings that it seems to cut it off.
Netcat:
nc -l 127.0.0.1 10001 > base64stringnc.txt
wc base64stringnc.txt