Hello,
I am writing an app that connects to a Java-Server.
Declarations at the top of the class:
let ip = "192.168.1.130"
let port = 3333
var inputStream: NSInputStream?
var outputStream: NSOutputStream?
I use this code to connect to the server:
func connect() {
NSStream.getStreamsToHostWithName(ip, port: port, inputStream: &inputStream, outputStream: &outputStream)
inputStream!.delegate = self
outputStream!.delegate = self
inputStream!.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
outputStream!.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
inputStream!.open()
outputStream!.open()
}
and this to disconnect:
func disconnect() {
inputStream?.close()
outputStream?.close()
inputStream?.removeFromRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
outputStream?.removeFromRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
inputStream = nil
outputStream = nil
}
connect() is called when the user taps on the connect button
disconnect() is called when the events .ErrorOccurred and .EndEncountered happen or the user taps on the disconnect button.
But the socket seems not be closed because the Java-Server still shows it as open and connected.
In Java i would call
socket.close();
Is there a similar method for iOS?
Thanks in advance!