I'm trying to make a socket server application in Swift for iOS. I found this great example:
let addr = "127.0.0.1"
let port = 4000
var host :NSHost = NSHost(address: addr)
var inp :NSInputStream?
var out :NSOutputStream?
NSStream.getStreamsToHost(host, 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)
}
// buffer is a UInt8 array containing bytes of the string "Jonathan Yaniv.".
outputStream.write("Hello", maxLength: "Hello".length)
I tried to run the code in XCode and it said that NSHost didn't exist. I Googled it and I found that NSHost wasn't able for iOS. What can I use instead?
Edit
I'm now using Cocoa Async Socket for socket connections. You can find it here CocoaAsyncScket
Change getStreamsToHost to getStreamsToHostWithName
Change:
to